classdef.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. """Transform class definitions from the mypy AST form to IR."""
  2. from __future__ import annotations
  3. import typing_extensions
  4. from abc import abstractmethod
  5. from typing import Callable, Final
  6. from mypy.nodes import (
  7. AssignmentStmt,
  8. CallExpr,
  9. ClassDef,
  10. Decorator,
  11. ExpressionStmt,
  12. FuncDef,
  13. Lvalue,
  14. MemberExpr,
  15. NameExpr,
  16. OverloadedFuncDef,
  17. PassStmt,
  18. RefExpr,
  19. StrExpr,
  20. TempNode,
  21. TypeInfo,
  22. is_class_var,
  23. )
  24. from mypy.types import ENUM_REMOVED_PROPS, Instance, UnboundType, get_proper_type
  25. from mypyc.common import PROPSET_PREFIX
  26. from mypyc.ir.class_ir import ClassIR, NonExtClassInfo
  27. from mypyc.ir.func_ir import FuncDecl, FuncSignature
  28. from mypyc.ir.ops import (
  29. NAMESPACE_TYPE,
  30. BasicBlock,
  31. Branch,
  32. Call,
  33. InitStatic,
  34. LoadAddress,
  35. LoadErrorValue,
  36. LoadStatic,
  37. MethodCall,
  38. Register,
  39. Return,
  40. SetAttr,
  41. TupleSet,
  42. Value,
  43. )
  44. from mypyc.ir.rtypes import (
  45. RType,
  46. bool_rprimitive,
  47. dict_rprimitive,
  48. is_none_rprimitive,
  49. is_object_rprimitive,
  50. is_optional_type,
  51. object_rprimitive,
  52. )
  53. from mypyc.irbuild.builder import IRBuilder
  54. from mypyc.irbuild.function import (
  55. gen_property_getter_ir,
  56. gen_property_setter_ir,
  57. handle_ext_method,
  58. handle_non_ext_method,
  59. load_type,
  60. )
  61. from mypyc.irbuild.util import dataclass_type, get_func_def, is_constant, is_dataclass_decorator
  62. from mypyc.primitives.dict_ops import dict_new_op, dict_set_item_op
  63. from mypyc.primitives.generic_ops import py_hasattr_op, py_setattr_op
  64. from mypyc.primitives.misc_ops import (
  65. dataclass_sleight_of_hand,
  66. not_implemented_op,
  67. py_calc_meta_op,
  68. pytype_from_template_op,
  69. type_object_op,
  70. )
  71. def transform_class_def(builder: IRBuilder, cdef: ClassDef) -> None:
  72. """Create IR for a class definition.
  73. This can generate both extension (native) and non-extension
  74. classes. These are generated in very different ways. In the
  75. latter case we construct a Python type object at runtime by doing
  76. the equivalent of "type(name, bases, dict)" in IR. Extension
  77. classes are defined via C structs that are generated later in
  78. mypyc.codegen.emitclass.
  79. This is the main entry point to this module.
  80. """
  81. ir = builder.mapper.type_to_ir[cdef.info]
  82. # We do this check here because the base field of parent
  83. # classes aren't necessarily populated yet at
  84. # prepare_class_def time.
  85. if any(ir.base_mro[i].base != ir.base_mro[i + 1] for i in range(len(ir.base_mro) - 1)):
  86. builder.error("Multiple inheritance is not supported (except for traits)", cdef.line)
  87. if ir.allow_interpreted_subclasses:
  88. for parent in ir.mro:
  89. if not parent.allow_interpreted_subclasses:
  90. builder.error(
  91. 'Base class "{}" does not allow interpreted subclasses'.format(
  92. parent.fullname
  93. ),
  94. cdef.line,
  95. )
  96. # Currently, we only create non-extension classes for classes that are
  97. # decorated or inherit from Enum. Classes decorated with @trait do not
  98. # apply here, and are handled in a different way.
  99. if ir.is_ext_class:
  100. cls_type = dataclass_type(cdef)
  101. if cls_type is None:
  102. cls_builder: ClassBuilder = ExtClassBuilder(builder, cdef)
  103. elif cls_type in ["dataclasses", "attr-auto"]:
  104. cls_builder = DataClassBuilder(builder, cdef)
  105. elif cls_type == "attr":
  106. cls_builder = AttrsClassBuilder(builder, cdef)
  107. else:
  108. raise ValueError(cls_type)
  109. else:
  110. cls_builder = NonExtClassBuilder(builder, cdef)
  111. for stmt in cdef.defs.body:
  112. if isinstance(stmt, OverloadedFuncDef) and stmt.is_property:
  113. if isinstance(cls_builder, NonExtClassBuilder):
  114. # properties with both getters and setters in non_extension
  115. # classes not supported
  116. builder.error("Property setters not supported in non-extension classes", stmt.line)
  117. for item in stmt.items:
  118. with builder.catch_errors(stmt.line):
  119. cls_builder.add_method(get_func_def(item))
  120. elif isinstance(stmt, (FuncDef, Decorator, OverloadedFuncDef)):
  121. # Ignore plugin generated methods (since they have no
  122. # bodies to compile and will need to have the bodies
  123. # provided by some other mechanism.)
  124. if cdef.info.names[stmt.name].plugin_generated:
  125. continue
  126. with builder.catch_errors(stmt.line):
  127. cls_builder.add_method(get_func_def(stmt))
  128. elif isinstance(stmt, PassStmt):
  129. continue
  130. elif isinstance(stmt, AssignmentStmt):
  131. if len(stmt.lvalues) != 1:
  132. builder.error("Multiple assignment in class bodies not supported", stmt.line)
  133. continue
  134. lvalue = stmt.lvalues[0]
  135. if not isinstance(lvalue, NameExpr):
  136. builder.error(
  137. "Only assignment to variables is supported in class bodies", stmt.line
  138. )
  139. continue
  140. # We want to collect class variables in a dictionary for both real
  141. # non-extension classes and fake dataclass ones.
  142. cls_builder.add_attr(lvalue, stmt)
  143. elif isinstance(stmt, ExpressionStmt) and isinstance(stmt.expr, StrExpr):
  144. # Docstring. Ignore
  145. pass
  146. else:
  147. builder.error("Unsupported statement in class body", stmt.line)
  148. # Generate implicit property setters/getters
  149. for name, decl in ir.method_decls.items():
  150. if decl.implicit and decl.is_prop_getter:
  151. getter_ir = gen_property_getter_ir(builder, decl, cdef, ir.is_trait)
  152. builder.functions.append(getter_ir)
  153. ir.methods[getter_ir.decl.name] = getter_ir
  154. setter_ir = None
  155. setter_name = PROPSET_PREFIX + name
  156. if setter_name in ir.method_decls:
  157. setter_ir = gen_property_setter_ir(
  158. builder, ir.method_decls[setter_name], cdef, ir.is_trait
  159. )
  160. builder.functions.append(setter_ir)
  161. ir.methods[setter_name] = setter_ir
  162. ir.properties[name] = (getter_ir, setter_ir)
  163. # TODO: Generate glue method if needed?
  164. # TODO: Do we need interpreted glue methods? Maybe not?
  165. cls_builder.finalize(ir)
  166. class ClassBuilder:
  167. """Create IR for a class definition.
  168. This is an abstract base class.
  169. """
  170. def __init__(self, builder: IRBuilder, cdef: ClassDef) -> None:
  171. self.builder = builder
  172. self.cdef = cdef
  173. self.attrs_to_cache: list[tuple[Lvalue, RType]] = []
  174. @abstractmethod
  175. def add_method(self, fdef: FuncDef) -> None:
  176. """Add a method to the class IR"""
  177. @abstractmethod
  178. def add_attr(self, lvalue: NameExpr, stmt: AssignmentStmt) -> None:
  179. """Add an attribute to the class IR"""
  180. @abstractmethod
  181. def finalize(self, ir: ClassIR) -> None:
  182. """Perform any final operations to complete the class IR"""
  183. class NonExtClassBuilder(ClassBuilder):
  184. def __init__(self, builder: IRBuilder, cdef: ClassDef) -> None:
  185. super().__init__(builder, cdef)
  186. self.non_ext = self.create_non_ext_info()
  187. def create_non_ext_info(self) -> NonExtClassInfo:
  188. non_ext_bases = populate_non_ext_bases(self.builder, self.cdef)
  189. non_ext_metaclass = find_non_ext_metaclass(self.builder, self.cdef, non_ext_bases)
  190. non_ext_dict = setup_non_ext_dict(
  191. self.builder, self.cdef, non_ext_metaclass, non_ext_bases
  192. )
  193. # We populate __annotations__ for non-extension classes
  194. # because dataclasses uses it to determine which attributes to compute on.
  195. # TODO: Maybe generate more precise types for annotations
  196. non_ext_anns = self.builder.call_c(dict_new_op, [], self.cdef.line)
  197. return NonExtClassInfo(non_ext_dict, non_ext_bases, non_ext_anns, non_ext_metaclass)
  198. def add_method(self, fdef: FuncDef) -> None:
  199. handle_non_ext_method(self.builder, self.non_ext, self.cdef, fdef)
  200. def add_attr(self, lvalue: NameExpr, stmt: AssignmentStmt) -> None:
  201. add_non_ext_class_attr_ann(self.builder, self.non_ext, lvalue, stmt)
  202. add_non_ext_class_attr(
  203. self.builder, self.non_ext, lvalue, stmt, self.cdef, self.attrs_to_cache
  204. )
  205. def finalize(self, ir: ClassIR) -> None:
  206. # Dynamically create the class via the type constructor
  207. non_ext_class = load_non_ext_class(self.builder, ir, self.non_ext, self.cdef.line)
  208. non_ext_class = load_decorated_class(self.builder, self.cdef, non_ext_class)
  209. # Save the decorated class
  210. self.builder.add(
  211. InitStatic(non_ext_class, self.cdef.name, self.builder.module_name, NAMESPACE_TYPE)
  212. )
  213. # Add the non-extension class to the dict
  214. self.builder.call_c(
  215. dict_set_item_op,
  216. [
  217. self.builder.load_globals_dict(),
  218. self.builder.load_str(self.cdef.name),
  219. non_ext_class,
  220. ],
  221. self.cdef.line,
  222. )
  223. # Cache any cacheable class attributes
  224. cache_class_attrs(self.builder, self.attrs_to_cache, self.cdef)
  225. class ExtClassBuilder(ClassBuilder):
  226. def __init__(self, builder: IRBuilder, cdef: ClassDef) -> None:
  227. super().__init__(builder, cdef)
  228. # If the class is not decorated, generate an extension class for it.
  229. self.type_obj: Value | None = allocate_class(builder, cdef)
  230. def skip_attr_default(self, name: str, stmt: AssignmentStmt) -> bool:
  231. """Controls whether to skip generating a default for an attribute."""
  232. return False
  233. def add_method(self, fdef: FuncDef) -> None:
  234. handle_ext_method(self.builder, self.cdef, fdef)
  235. def add_attr(self, lvalue: NameExpr, stmt: AssignmentStmt) -> None:
  236. # Variable declaration with no body
  237. if isinstance(stmt.rvalue, TempNode):
  238. return
  239. # Only treat marked class variables as class variables.
  240. if not (is_class_var(lvalue) or stmt.is_final_def):
  241. return
  242. typ = self.builder.load_native_type_object(self.cdef.fullname)
  243. value = self.builder.accept(stmt.rvalue)
  244. self.builder.call_c(
  245. py_setattr_op, [typ, self.builder.load_str(lvalue.name), value], stmt.line
  246. )
  247. if self.builder.non_function_scope() and stmt.is_final_def:
  248. self.builder.init_final_static(lvalue, value, self.cdef.name)
  249. def finalize(self, ir: ClassIR) -> None:
  250. attrs_with_defaults, default_assignments = find_attr_initializers(
  251. self.builder, self.cdef, self.skip_attr_default
  252. )
  253. ir.attrs_with_defaults.update(attrs_with_defaults)
  254. generate_attr_defaults_init(self.builder, self.cdef, default_assignments)
  255. create_ne_from_eq(self.builder, self.cdef)
  256. class DataClassBuilder(ExtClassBuilder):
  257. # controls whether an __annotations__ attribute should be added to the class
  258. # __dict__. This is not desirable for attrs classes where auto_attribs is
  259. # disabled, as attrs will reject it.
  260. add_annotations_to_dict = True
  261. def __init__(self, builder: IRBuilder, cdef: ClassDef) -> None:
  262. super().__init__(builder, cdef)
  263. self.non_ext = self.create_non_ext_info()
  264. def create_non_ext_info(self) -> NonExtClassInfo:
  265. """Set up a NonExtClassInfo to track dataclass attributes.
  266. In addition to setting up a normal extension class for dataclasses,
  267. we also collect its class attributes like a non-extension class so
  268. that we can hand them to the dataclass decorator.
  269. """
  270. return NonExtClassInfo(
  271. self.builder.call_c(dict_new_op, [], self.cdef.line),
  272. self.builder.add(TupleSet([], self.cdef.line)),
  273. self.builder.call_c(dict_new_op, [], self.cdef.line),
  274. self.builder.add(LoadAddress(type_object_op.type, type_object_op.src, self.cdef.line)),
  275. )
  276. def skip_attr_default(self, name: str, stmt: AssignmentStmt) -> bool:
  277. return stmt.type is not None
  278. def get_type_annotation(self, stmt: AssignmentStmt) -> TypeInfo | None:
  279. # We populate __annotations__ because dataclasses uses it to determine
  280. # which attributes to compute on.
  281. ann_type = get_proper_type(stmt.type)
  282. if isinstance(ann_type, Instance):
  283. return ann_type.type
  284. return None
  285. def add_attr(self, lvalue: NameExpr, stmt: AssignmentStmt) -> None:
  286. add_non_ext_class_attr_ann(
  287. self.builder, self.non_ext, lvalue, stmt, self.get_type_annotation
  288. )
  289. add_non_ext_class_attr(
  290. self.builder, self.non_ext, lvalue, stmt, self.cdef, self.attrs_to_cache
  291. )
  292. super().add_attr(lvalue, stmt)
  293. def finalize(self, ir: ClassIR) -> None:
  294. """Generate code to finish instantiating a dataclass.
  295. This works by replacing all of the attributes on the class
  296. (which will be descriptors) with whatever they would be in a
  297. non-extension class, calling dataclass, then switching them back.
  298. The resulting class is an extension class and instances of it do not
  299. have a __dict__ (unless something else requires it).
  300. All methods written explicitly in the source are compiled and
  301. may be called through the vtable while the methods generated
  302. by dataclasses are interpreted and may not be.
  303. (If we just called dataclass without doing this, it would think that all
  304. of the descriptors for our attributes are default values and generate an
  305. incorrect constructor. We need to do the switch so that dataclass gets the
  306. appropriate defaults.)
  307. """
  308. super().finalize(ir)
  309. assert self.type_obj
  310. add_dunders_to_non_ext_dict(
  311. self.builder, self.non_ext, self.cdef.line, self.add_annotations_to_dict
  312. )
  313. dec = self.builder.accept(
  314. next(d for d in self.cdef.decorators if is_dataclass_decorator(d))
  315. )
  316. self.builder.call_c(
  317. dataclass_sleight_of_hand,
  318. [dec, self.type_obj, self.non_ext.dict, self.non_ext.anns],
  319. self.cdef.line,
  320. )
  321. class AttrsClassBuilder(DataClassBuilder):
  322. """Create IR for an attrs class where auto_attribs=False (the default).
  323. When auto_attribs is enabled, attrs classes behave similarly to dataclasses
  324. (i.e. types are stored as annotations on the class) and are thus handled
  325. by DataClassBuilder, but when auto_attribs is disabled the types are
  326. provided via attr.ib(type=...)
  327. """
  328. add_annotations_to_dict = False
  329. def skip_attr_default(self, name: str, stmt: AssignmentStmt) -> bool:
  330. return True
  331. def get_type_annotation(self, stmt: AssignmentStmt) -> TypeInfo | None:
  332. if isinstance(stmt.rvalue, CallExpr):
  333. # find the type arg in `attr.ib(type=str)`
  334. callee = stmt.rvalue.callee
  335. if (
  336. isinstance(callee, MemberExpr)
  337. and callee.fullname in ["attr.ib", "attr.attr"]
  338. and "type" in stmt.rvalue.arg_names
  339. ):
  340. index = stmt.rvalue.arg_names.index("type")
  341. type_name = stmt.rvalue.args[index]
  342. if isinstance(type_name, NameExpr) and isinstance(type_name.node, TypeInfo):
  343. lvalue = stmt.lvalues[0]
  344. assert isinstance(lvalue, NameExpr)
  345. return type_name.node
  346. return None
  347. def allocate_class(builder: IRBuilder, cdef: ClassDef) -> Value:
  348. # OK AND NOW THE FUN PART
  349. base_exprs = cdef.base_type_exprs + cdef.removed_base_type_exprs
  350. if base_exprs:
  351. bases = [builder.accept(x) for x in base_exprs]
  352. tp_bases = builder.new_tuple(bases, cdef.line)
  353. else:
  354. tp_bases = builder.add(LoadErrorValue(object_rprimitive, is_borrowed=True))
  355. modname = builder.load_str(builder.module_name)
  356. template = builder.add(
  357. LoadStatic(object_rprimitive, cdef.name + "_template", builder.module_name, NAMESPACE_TYPE)
  358. )
  359. # Create the class
  360. tp = builder.call_c(pytype_from_template_op, [template, tp_bases, modname], cdef.line)
  361. # Immediately fix up the trait vtables, before doing anything with the class.
  362. ir = builder.mapper.type_to_ir[cdef.info]
  363. if not ir.is_trait and not ir.builtin_base:
  364. builder.add(
  365. Call(
  366. FuncDecl(
  367. cdef.name + "_trait_vtable_setup",
  368. None,
  369. builder.module_name,
  370. FuncSignature([], bool_rprimitive),
  371. ),
  372. [],
  373. -1,
  374. )
  375. )
  376. # Populate a '__mypyc_attrs__' field containing the list of attrs
  377. builder.call_c(
  378. py_setattr_op,
  379. [
  380. tp,
  381. builder.load_str("__mypyc_attrs__"),
  382. create_mypyc_attrs_tuple(builder, builder.mapper.type_to_ir[cdef.info], cdef.line),
  383. ],
  384. cdef.line,
  385. )
  386. # Save the class
  387. builder.add(InitStatic(tp, cdef.name, builder.module_name, NAMESPACE_TYPE))
  388. # Add it to the dict
  389. builder.call_c(
  390. dict_set_item_op, [builder.load_globals_dict(), builder.load_str(cdef.name), tp], cdef.line
  391. )
  392. return tp
  393. # Mypy uses these internally as base classes of TypedDict classes. These are
  394. # lies and don't have any runtime equivalent.
  395. MAGIC_TYPED_DICT_CLASSES: Final[tuple[str, ...]] = (
  396. "typing._TypedDict",
  397. "typing_extensions._TypedDict",
  398. )
  399. def populate_non_ext_bases(builder: IRBuilder, cdef: ClassDef) -> Value:
  400. """Create base class tuple of a non-extension class.
  401. The tuple is passed to the metaclass constructor.
  402. """
  403. is_named_tuple = cdef.info.is_named_tuple
  404. ir = builder.mapper.type_to_ir[cdef.info]
  405. bases = []
  406. for cls in cdef.info.mro[1:]:
  407. if cls.fullname == "builtins.object":
  408. continue
  409. if is_named_tuple and cls.fullname in (
  410. "typing.Sequence",
  411. "typing.Iterable",
  412. "typing.Collection",
  413. "typing.Reversible",
  414. "typing.Container",
  415. "typing.Sized",
  416. ):
  417. # HAX: Synthesized base classes added by mypy don't exist at runtime, so skip them.
  418. # This could break if they were added explicitly, though...
  419. continue
  420. # Add the current class to the base classes list of concrete subclasses
  421. if cls in builder.mapper.type_to_ir:
  422. base_ir = builder.mapper.type_to_ir[cls]
  423. if base_ir.children is not None:
  424. base_ir.children.append(ir)
  425. if cls.fullname in MAGIC_TYPED_DICT_CLASSES:
  426. # HAX: Mypy internally represents TypedDict classes differently from what
  427. # should happen at runtime. Replace with something that works.
  428. module = "typing"
  429. if builder.options.capi_version < (3, 9):
  430. name = "TypedDict"
  431. if builder.options.capi_version < (3, 8):
  432. # TypedDict was added to typing in Python 3.8.
  433. module = "typing_extensions"
  434. # TypedDict is not a real type on typing_extensions 4.7.0+
  435. name = "_TypedDict"
  436. if isinstance(typing_extensions.TypedDict, type):
  437. raise RuntimeError(
  438. "It looks like you may have an old version "
  439. "of typing_extensions installed. "
  440. "typing_extensions>=4.7.0 is required on Python 3.7."
  441. )
  442. else:
  443. # In Python 3.9 TypedDict is not a real type.
  444. name = "_TypedDict"
  445. base = builder.get_module_attr(module, name, cdef.line)
  446. elif is_named_tuple and cls.fullname == "builtins.tuple":
  447. if builder.options.capi_version < (3, 9):
  448. name = "NamedTuple"
  449. else:
  450. # This was changed in Python 3.9.
  451. name = "_NamedTuple"
  452. base = builder.get_module_attr("typing", name, cdef.line)
  453. else:
  454. cls_module = cls.fullname.rsplit(".", 1)[0]
  455. if cls_module == builder.current_module:
  456. base = builder.load_global_str(cls.name, cdef.line)
  457. else:
  458. base = builder.load_module_attr_by_fullname(cls.fullname, cdef.line)
  459. bases.append(base)
  460. if cls.fullname in MAGIC_TYPED_DICT_CLASSES:
  461. # The remaining base classes are synthesized by mypy and should be ignored.
  462. break
  463. return builder.new_tuple(bases, cdef.line)
  464. def find_non_ext_metaclass(builder: IRBuilder, cdef: ClassDef, bases: Value) -> Value:
  465. """Find the metaclass of a class from its defs and bases."""
  466. if cdef.metaclass:
  467. declared_metaclass = builder.accept(cdef.metaclass)
  468. else:
  469. if cdef.info.typeddict_type is not None and builder.options.capi_version >= (3, 9):
  470. # In Python 3.9, the metaclass for class-based TypedDict is typing._TypedDictMeta.
  471. # We can't easily calculate it generically, so special case it.
  472. return builder.get_module_attr("typing", "_TypedDictMeta", cdef.line)
  473. elif cdef.info.is_named_tuple and builder.options.capi_version >= (3, 9):
  474. # In Python 3.9, the metaclass for class-based NamedTuple is typing.NamedTupleMeta.
  475. # We can't easily calculate it generically, so special case it.
  476. return builder.get_module_attr("typing", "NamedTupleMeta", cdef.line)
  477. declared_metaclass = builder.add(
  478. LoadAddress(type_object_op.type, type_object_op.src, cdef.line)
  479. )
  480. return builder.call_c(py_calc_meta_op, [declared_metaclass, bases], cdef.line)
  481. def setup_non_ext_dict(
  482. builder: IRBuilder, cdef: ClassDef, metaclass: Value, bases: Value
  483. ) -> Value:
  484. """Initialize the class dictionary for a non-extension class.
  485. This class dictionary is passed to the metaclass constructor.
  486. """
  487. # Check if the metaclass defines a __prepare__ method, and if so, call it.
  488. has_prepare = builder.call_c(
  489. py_hasattr_op, [metaclass, builder.load_str("__prepare__")], cdef.line
  490. )
  491. non_ext_dict = Register(dict_rprimitive)
  492. true_block, false_block, exit_block = BasicBlock(), BasicBlock(), BasicBlock()
  493. builder.add_bool_branch(has_prepare, true_block, false_block)
  494. builder.activate_block(true_block)
  495. cls_name = builder.load_str(cdef.name)
  496. prepare_meth = builder.py_get_attr(metaclass, "__prepare__", cdef.line)
  497. prepare_dict = builder.py_call(prepare_meth, [cls_name, bases], cdef.line)
  498. builder.assign(non_ext_dict, prepare_dict, cdef.line)
  499. builder.goto(exit_block)
  500. builder.activate_block(false_block)
  501. builder.assign(non_ext_dict, builder.call_c(dict_new_op, [], cdef.line), cdef.line)
  502. builder.goto(exit_block)
  503. builder.activate_block(exit_block)
  504. return non_ext_dict
  505. def add_non_ext_class_attr_ann(
  506. builder: IRBuilder,
  507. non_ext: NonExtClassInfo,
  508. lvalue: NameExpr,
  509. stmt: AssignmentStmt,
  510. get_type_info: Callable[[AssignmentStmt], TypeInfo | None] | None = None,
  511. ) -> None:
  512. """Add a class attribute to __annotations__ of a non-extension class."""
  513. # FIXME: try to better preserve the special forms and type parameters of generics.
  514. typ: Value | None = None
  515. if get_type_info is not None:
  516. type_info = get_type_info(stmt)
  517. if type_info:
  518. typ = load_type(builder, type_info, stmt.line)
  519. if typ is None:
  520. # FIXME: if get_type_info is not provided, don't fall back to stmt.type?
  521. ann_type = get_proper_type(stmt.type)
  522. if (
  523. isinstance(stmt.unanalyzed_type, UnboundType)
  524. and stmt.unanalyzed_type.original_str_expr is not None
  525. ):
  526. # Annotation is a forward reference, so don't attempt to load the actual
  527. # type and load the string instead.
  528. #
  529. # TODO: is it possible to determine whether a non-string annotation is
  530. # actually a forward reference due to the __annotations__ future?
  531. typ = builder.load_str(stmt.unanalyzed_type.original_str_expr)
  532. elif isinstance(ann_type, Instance):
  533. typ = load_type(builder, ann_type.type, stmt.line)
  534. else:
  535. typ = builder.add(LoadAddress(type_object_op.type, type_object_op.src, stmt.line))
  536. key = builder.load_str(lvalue.name)
  537. builder.call_c(dict_set_item_op, [non_ext.anns, key, typ], stmt.line)
  538. def add_non_ext_class_attr(
  539. builder: IRBuilder,
  540. non_ext: NonExtClassInfo,
  541. lvalue: NameExpr,
  542. stmt: AssignmentStmt,
  543. cdef: ClassDef,
  544. attr_to_cache: list[tuple[Lvalue, RType]],
  545. ) -> None:
  546. """Add a class attribute to __dict__ of a non-extension class."""
  547. # Only add the attribute to the __dict__ if the assignment is of the form:
  548. # x: type = value (don't add attributes of the form 'x: type' to the __dict__).
  549. if not isinstance(stmt.rvalue, TempNode):
  550. rvalue = builder.accept(stmt.rvalue)
  551. builder.add_to_non_ext_dict(non_ext, lvalue.name, rvalue, stmt.line)
  552. # We cache enum attributes to speed up enum attribute lookup since they
  553. # are final.
  554. if (
  555. cdef.info.bases
  556. and cdef.info.bases[0].type.fullname == "enum.Enum"
  557. # Skip these since Enum will remove it
  558. and lvalue.name not in ENUM_REMOVED_PROPS
  559. ):
  560. # Enum values are always boxed, so use object_rprimitive.
  561. attr_to_cache.append((lvalue, object_rprimitive))
  562. def find_attr_initializers(
  563. builder: IRBuilder, cdef: ClassDef, skip: Callable[[str, AssignmentStmt], bool] | None = None
  564. ) -> tuple[set[str], list[AssignmentStmt]]:
  565. """Find initializers of attributes in a class body.
  566. If provided, the skip arg should be a callable which will return whether
  567. to skip generating a default for an attribute. It will be passed the name of
  568. the attribute and the corresponding AssignmentStmt.
  569. """
  570. cls = builder.mapper.type_to_ir[cdef.info]
  571. if cls.builtin_base:
  572. return set(), []
  573. attrs_with_defaults = set()
  574. # Pull out all assignments in classes in the mro so we can initialize them
  575. # TODO: Support nested statements
  576. default_assignments = []
  577. for info in reversed(cdef.info.mro):
  578. if info not in builder.mapper.type_to_ir:
  579. continue
  580. for stmt in info.defn.defs.body:
  581. if (
  582. isinstance(stmt, AssignmentStmt)
  583. and isinstance(stmt.lvalues[0], NameExpr)
  584. and not is_class_var(stmt.lvalues[0])
  585. and not isinstance(stmt.rvalue, TempNode)
  586. ):
  587. name = stmt.lvalues[0].name
  588. if name == "__slots__":
  589. continue
  590. if name == "__deletable__":
  591. check_deletable_declaration(builder, cls, stmt.line)
  592. continue
  593. if skip is not None and skip(name, stmt):
  594. continue
  595. attr_type = cls.attr_type(name)
  596. # If the attribute is initialized to None and type isn't optional,
  597. # doesn't initialize it to anything (special case for "# type:" comments).
  598. if isinstance(stmt.rvalue, RefExpr) and stmt.rvalue.fullname == "builtins.None":
  599. if (
  600. not is_optional_type(attr_type)
  601. and not is_object_rprimitive(attr_type)
  602. and not is_none_rprimitive(attr_type)
  603. ):
  604. continue
  605. attrs_with_defaults.add(name)
  606. default_assignments.append(stmt)
  607. return attrs_with_defaults, default_assignments
  608. def generate_attr_defaults_init(
  609. builder: IRBuilder, cdef: ClassDef, default_assignments: list[AssignmentStmt]
  610. ) -> None:
  611. """Generate an initialization method for default attr values (from class vars)."""
  612. if not default_assignments:
  613. return
  614. cls = builder.mapper.type_to_ir[cdef.info]
  615. if cls.builtin_base:
  616. return
  617. with builder.enter_method(cls, "__mypyc_defaults_setup", bool_rprimitive):
  618. self_var = builder.self()
  619. for stmt in default_assignments:
  620. lvalue = stmt.lvalues[0]
  621. assert isinstance(lvalue, NameExpr)
  622. if not stmt.is_final_def and not is_constant(stmt.rvalue):
  623. builder.warning("Unsupported default attribute value", stmt.rvalue.line)
  624. attr_type = cls.attr_type(lvalue.name)
  625. val = builder.coerce(builder.accept(stmt.rvalue), attr_type, stmt.line)
  626. init = SetAttr(self_var, lvalue.name, val, -1)
  627. init.mark_as_initializer()
  628. builder.add(init)
  629. builder.add(Return(builder.true()))
  630. def check_deletable_declaration(builder: IRBuilder, cl: ClassIR, line: int) -> None:
  631. for attr in cl.deletable:
  632. if attr not in cl.attributes:
  633. if not cl.has_attr(attr):
  634. builder.error(f'Attribute "{attr}" not defined', line)
  635. continue
  636. for base in cl.mro:
  637. if attr in base.property_types:
  638. builder.error(f'Cannot make property "{attr}" deletable', line)
  639. break
  640. else:
  641. _, base = cl.attr_details(attr)
  642. builder.error(
  643. ('Attribute "{}" not defined in "{}" ' + '(defined in "{}")').format(
  644. attr, cl.name, base.name
  645. ),
  646. line,
  647. )
  648. def create_ne_from_eq(builder: IRBuilder, cdef: ClassDef) -> None:
  649. """Create a "__ne__" method from a "__eq__" method (if only latter exists)."""
  650. cls = builder.mapper.type_to_ir[cdef.info]
  651. if cls.has_method("__eq__") and not cls.has_method("__ne__"):
  652. gen_glue_ne_method(builder, cls, cdef.line)
  653. def gen_glue_ne_method(builder: IRBuilder, cls: ClassIR, line: int) -> None:
  654. """Generate a "__ne__" method from a "__eq__" method."""
  655. with builder.enter_method(cls, "__ne__", object_rprimitive):
  656. rhs_arg = builder.add_argument("rhs", object_rprimitive)
  657. # If __eq__ returns NotImplemented, then __ne__ should also
  658. not_implemented_block, regular_block = BasicBlock(), BasicBlock()
  659. eqval = builder.add(MethodCall(builder.self(), "__eq__", [rhs_arg], line))
  660. not_implemented = builder.add(
  661. LoadAddress(not_implemented_op.type, not_implemented_op.src, line)
  662. )
  663. builder.add(
  664. Branch(
  665. builder.translate_is_op(eqval, not_implemented, "is", line),
  666. not_implemented_block,
  667. regular_block,
  668. Branch.BOOL,
  669. )
  670. )
  671. builder.activate_block(regular_block)
  672. retval = builder.coerce(builder.unary_op(eqval, "not", line), object_rprimitive, line)
  673. builder.add(Return(retval))
  674. builder.activate_block(not_implemented_block)
  675. builder.add(Return(not_implemented))
  676. def load_non_ext_class(
  677. builder: IRBuilder, ir: ClassIR, non_ext: NonExtClassInfo, line: int
  678. ) -> Value:
  679. cls_name = builder.load_str(ir.name)
  680. add_dunders_to_non_ext_dict(builder, non_ext, line)
  681. class_type_obj = builder.py_call(
  682. non_ext.metaclass, [cls_name, non_ext.bases, non_ext.dict], line
  683. )
  684. return class_type_obj
  685. def load_decorated_class(builder: IRBuilder, cdef: ClassDef, type_obj: Value) -> Value:
  686. """Apply class decorators to create a decorated (non-extension) class object.
  687. Given a decorated ClassDef and a register containing a
  688. non-extension representation of the ClassDef created via the type
  689. constructor, applies the corresponding decorator functions on that
  690. decorated ClassDef and returns a register containing the decorated
  691. ClassDef.
  692. """
  693. decorators = cdef.decorators
  694. dec_class = type_obj
  695. for d in reversed(decorators):
  696. decorator = d.accept(builder.visitor)
  697. assert isinstance(decorator, Value)
  698. dec_class = builder.py_call(decorator, [dec_class], dec_class.line)
  699. return dec_class
  700. def cache_class_attrs(
  701. builder: IRBuilder, attrs_to_cache: list[tuple[Lvalue, RType]], cdef: ClassDef
  702. ) -> None:
  703. """Add class attributes to be cached to the global cache."""
  704. typ = builder.load_native_type_object(cdef.info.fullname)
  705. for lval, rtype in attrs_to_cache:
  706. assert isinstance(lval, NameExpr)
  707. rval = builder.py_get_attr(typ, lval.name, cdef.line)
  708. builder.init_final_static(lval, rval, cdef.name, type_override=rtype)
  709. def create_mypyc_attrs_tuple(builder: IRBuilder, ir: ClassIR, line: int) -> Value:
  710. attrs = [name for ancestor in ir.mro for name in ancestor.attributes]
  711. if ir.inherits_python:
  712. attrs.append("__dict__")
  713. items = [builder.load_str(attr) for attr in attrs]
  714. return builder.new_tuple(items, line)
  715. def add_dunders_to_non_ext_dict(
  716. builder: IRBuilder, non_ext: NonExtClassInfo, line: int, add_annotations: bool = True
  717. ) -> None:
  718. if add_annotations:
  719. # Add __annotations__ to the class dict.
  720. builder.add_to_non_ext_dict(non_ext, "__annotations__", non_ext.anns, line)
  721. # We add a __doc__ attribute so if the non-extension class is decorated with the
  722. # dataclass decorator, dataclass will not try to look for __text_signature__.
  723. # https://github.com/python/cpython/blob/3.7/Lib/dataclasses.py#L957
  724. filler_doc_str = "mypyc filler docstring"
  725. builder.add_to_non_ext_dict(non_ext, "__doc__", builder.load_str(filler_doc_str), line)
  726. builder.add_to_non_ext_dict(non_ext, "__module__", builder.load_str(builder.module_name), line)