classdef.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. """Transform class definitions from the mypy AST form to IR."""
  2. from __future__ import annotations
  3. from abc import abstractmethod
  4. from typing import Callable
  5. from typing_extensions import 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. else:
  435. # In Python 3.9 TypedDict is not a real type.
  436. name = "_TypedDict"
  437. base = builder.get_module_attr(module, name, cdef.line)
  438. elif is_named_tuple and cls.fullname == "builtins.tuple":
  439. if builder.options.capi_version < (3, 9):
  440. name = "NamedTuple"
  441. else:
  442. # This was changed in Python 3.9.
  443. name = "_NamedTuple"
  444. base = builder.get_module_attr("typing", name, cdef.line)
  445. else:
  446. cls_module = cls.fullname.rsplit(".", 1)[0]
  447. if cls_module == builder.current_module:
  448. base = builder.load_global_str(cls.name, cdef.line)
  449. else:
  450. base = builder.load_module_attr_by_fullname(cls.fullname, cdef.line)
  451. bases.append(base)
  452. if cls.fullname in MAGIC_TYPED_DICT_CLASSES:
  453. # The remaining base classes are synthesized by mypy and should be ignored.
  454. break
  455. return builder.new_tuple(bases, cdef.line)
  456. def find_non_ext_metaclass(builder: IRBuilder, cdef: ClassDef, bases: Value) -> Value:
  457. """Find the metaclass of a class from its defs and bases."""
  458. if cdef.metaclass:
  459. declared_metaclass = builder.accept(cdef.metaclass)
  460. else:
  461. if cdef.info.typeddict_type is not None and builder.options.capi_version >= (3, 9):
  462. # In Python 3.9, the metaclass for class-based TypedDict is typing._TypedDictMeta.
  463. # We can't easily calculate it generically, so special case it.
  464. return builder.get_module_attr("typing", "_TypedDictMeta", cdef.line)
  465. elif cdef.info.is_named_tuple and builder.options.capi_version >= (3, 9):
  466. # In Python 3.9, the metaclass for class-based NamedTuple is typing.NamedTupleMeta.
  467. # We can't easily calculate it generically, so special case it.
  468. return builder.get_module_attr("typing", "NamedTupleMeta", cdef.line)
  469. declared_metaclass = builder.add(
  470. LoadAddress(type_object_op.type, type_object_op.src, cdef.line)
  471. )
  472. return builder.call_c(py_calc_meta_op, [declared_metaclass, bases], cdef.line)
  473. def setup_non_ext_dict(
  474. builder: IRBuilder, cdef: ClassDef, metaclass: Value, bases: Value
  475. ) -> Value:
  476. """Initialize the class dictionary for a non-extension class.
  477. This class dictionary is passed to the metaclass constructor.
  478. """
  479. # Check if the metaclass defines a __prepare__ method, and if so, call it.
  480. has_prepare = builder.call_c(
  481. py_hasattr_op, [metaclass, builder.load_str("__prepare__")], cdef.line
  482. )
  483. non_ext_dict = Register(dict_rprimitive)
  484. true_block, false_block, exit_block = BasicBlock(), BasicBlock(), BasicBlock()
  485. builder.add_bool_branch(has_prepare, true_block, false_block)
  486. builder.activate_block(true_block)
  487. cls_name = builder.load_str(cdef.name)
  488. prepare_meth = builder.py_get_attr(metaclass, "__prepare__", cdef.line)
  489. prepare_dict = builder.py_call(prepare_meth, [cls_name, bases], cdef.line)
  490. builder.assign(non_ext_dict, prepare_dict, cdef.line)
  491. builder.goto(exit_block)
  492. builder.activate_block(false_block)
  493. builder.assign(non_ext_dict, builder.call_c(dict_new_op, [], cdef.line), cdef.line)
  494. builder.goto(exit_block)
  495. builder.activate_block(exit_block)
  496. return non_ext_dict
  497. def add_non_ext_class_attr_ann(
  498. builder: IRBuilder,
  499. non_ext: NonExtClassInfo,
  500. lvalue: NameExpr,
  501. stmt: AssignmentStmt,
  502. get_type_info: Callable[[AssignmentStmt], TypeInfo | None] | None = None,
  503. ) -> None:
  504. """Add a class attribute to __annotations__ of a non-extension class."""
  505. # FIXME: try to better preserve the special forms and type parameters of generics.
  506. typ: Value | None = None
  507. if get_type_info is not None:
  508. type_info = get_type_info(stmt)
  509. if type_info:
  510. typ = load_type(builder, type_info, stmt.line)
  511. if typ is None:
  512. # FIXME: if get_type_info is not provided, don't fall back to stmt.type?
  513. ann_type = get_proper_type(stmt.type)
  514. if (
  515. isinstance(stmt.unanalyzed_type, UnboundType)
  516. and stmt.unanalyzed_type.original_str_expr is not None
  517. ):
  518. # Annotation is a forward reference, so don't attempt to load the actual
  519. # type and load the string instead.
  520. #
  521. # TODO: is it possible to determine whether a non-string annotation is
  522. # actually a forward reference due to the __annotations__ future?
  523. typ = builder.load_str(stmt.unanalyzed_type.original_str_expr)
  524. elif isinstance(ann_type, Instance):
  525. typ = load_type(builder, ann_type.type, stmt.line)
  526. else:
  527. typ = builder.add(LoadAddress(type_object_op.type, type_object_op.src, stmt.line))
  528. key = builder.load_str(lvalue.name)
  529. builder.call_c(dict_set_item_op, [non_ext.anns, key, typ], stmt.line)
  530. def add_non_ext_class_attr(
  531. builder: IRBuilder,
  532. non_ext: NonExtClassInfo,
  533. lvalue: NameExpr,
  534. stmt: AssignmentStmt,
  535. cdef: ClassDef,
  536. attr_to_cache: list[tuple[Lvalue, RType]],
  537. ) -> None:
  538. """Add a class attribute to __dict__ of a non-extension class."""
  539. # Only add the attribute to the __dict__ if the assignment is of the form:
  540. # x: type = value (don't add attributes of the form 'x: type' to the __dict__).
  541. if not isinstance(stmt.rvalue, TempNode):
  542. rvalue = builder.accept(stmt.rvalue)
  543. builder.add_to_non_ext_dict(non_ext, lvalue.name, rvalue, stmt.line)
  544. # We cache enum attributes to speed up enum attribute lookup since they
  545. # are final.
  546. if (
  547. cdef.info.bases
  548. and cdef.info.bases[0].type.fullname == "enum.Enum"
  549. # Skip these since Enum will remove it
  550. and lvalue.name not in ENUM_REMOVED_PROPS
  551. ):
  552. # Enum values are always boxed, so use object_rprimitive.
  553. attr_to_cache.append((lvalue, object_rprimitive))
  554. def find_attr_initializers(
  555. builder: IRBuilder, cdef: ClassDef, skip: Callable[[str, AssignmentStmt], bool] | None = None
  556. ) -> tuple[set[str], list[AssignmentStmt]]:
  557. """Find initializers of attributes in a class body.
  558. If provided, the skip arg should be a callable which will return whether
  559. to skip generating a default for an attribute. It will be passed the name of
  560. the attribute and the corresponding AssignmentStmt.
  561. """
  562. cls = builder.mapper.type_to_ir[cdef.info]
  563. if cls.builtin_base:
  564. return set(), []
  565. attrs_with_defaults = set()
  566. # Pull out all assignments in classes in the mro so we can initialize them
  567. # TODO: Support nested statements
  568. default_assignments = []
  569. for info in reversed(cdef.info.mro):
  570. if info not in builder.mapper.type_to_ir:
  571. continue
  572. for stmt in info.defn.defs.body:
  573. if (
  574. isinstance(stmt, AssignmentStmt)
  575. and isinstance(stmt.lvalues[0], NameExpr)
  576. and not is_class_var(stmt.lvalues[0])
  577. and not isinstance(stmt.rvalue, TempNode)
  578. ):
  579. name = stmt.lvalues[0].name
  580. if name == "__slots__":
  581. continue
  582. if name == "__deletable__":
  583. check_deletable_declaration(builder, cls, stmt.line)
  584. continue
  585. if skip is not None and skip(name, stmt):
  586. continue
  587. attr_type = cls.attr_type(name)
  588. # If the attribute is initialized to None and type isn't optional,
  589. # doesn't initialize it to anything (special case for "# type:" comments).
  590. if isinstance(stmt.rvalue, RefExpr) and stmt.rvalue.fullname == "builtins.None":
  591. if (
  592. not is_optional_type(attr_type)
  593. and not is_object_rprimitive(attr_type)
  594. and not is_none_rprimitive(attr_type)
  595. ):
  596. continue
  597. attrs_with_defaults.add(name)
  598. default_assignments.append(stmt)
  599. return attrs_with_defaults, default_assignments
  600. def generate_attr_defaults_init(
  601. builder: IRBuilder, cdef: ClassDef, default_assignments: list[AssignmentStmt]
  602. ) -> None:
  603. """Generate an initialization method for default attr values (from class vars)."""
  604. if not default_assignments:
  605. return
  606. cls = builder.mapper.type_to_ir[cdef.info]
  607. if cls.builtin_base:
  608. return
  609. with builder.enter_method(cls, "__mypyc_defaults_setup", bool_rprimitive):
  610. self_var = builder.self()
  611. for stmt in default_assignments:
  612. lvalue = stmt.lvalues[0]
  613. assert isinstance(lvalue, NameExpr)
  614. if not stmt.is_final_def and not is_constant(stmt.rvalue):
  615. builder.warning("Unsupported default attribute value", stmt.rvalue.line)
  616. attr_type = cls.attr_type(lvalue.name)
  617. val = builder.coerce(builder.accept(stmt.rvalue), attr_type, stmt.line)
  618. init = SetAttr(self_var, lvalue.name, val, -1)
  619. init.mark_as_initializer()
  620. builder.add(init)
  621. builder.add(Return(builder.true()))
  622. def check_deletable_declaration(builder: IRBuilder, cl: ClassIR, line: int) -> None:
  623. for attr in cl.deletable:
  624. if attr not in cl.attributes:
  625. if not cl.has_attr(attr):
  626. builder.error(f'Attribute "{attr}" not defined', line)
  627. continue
  628. for base in cl.mro:
  629. if attr in base.property_types:
  630. builder.error(f'Cannot make property "{attr}" deletable', line)
  631. break
  632. else:
  633. _, base = cl.attr_details(attr)
  634. builder.error(
  635. ('Attribute "{}" not defined in "{}" ' + '(defined in "{}")').format(
  636. attr, cl.name, base.name
  637. ),
  638. line,
  639. )
  640. def create_ne_from_eq(builder: IRBuilder, cdef: ClassDef) -> None:
  641. """Create a "__ne__" method from a "__eq__" method (if only latter exists)."""
  642. cls = builder.mapper.type_to_ir[cdef.info]
  643. if cls.has_method("__eq__") and not cls.has_method("__ne__"):
  644. gen_glue_ne_method(builder, cls, cdef.line)
  645. def gen_glue_ne_method(builder: IRBuilder, cls: ClassIR, line: int) -> None:
  646. """Generate a "__ne__" method from a "__eq__" method."""
  647. with builder.enter_method(cls, "__ne__", object_rprimitive):
  648. rhs_arg = builder.add_argument("rhs", object_rprimitive)
  649. # If __eq__ returns NotImplemented, then __ne__ should also
  650. not_implemented_block, regular_block = BasicBlock(), BasicBlock()
  651. eqval = builder.add(MethodCall(builder.self(), "__eq__", [rhs_arg], line))
  652. not_implemented = builder.add(
  653. LoadAddress(not_implemented_op.type, not_implemented_op.src, line)
  654. )
  655. builder.add(
  656. Branch(
  657. builder.translate_is_op(eqval, not_implemented, "is", line),
  658. not_implemented_block,
  659. regular_block,
  660. Branch.BOOL,
  661. )
  662. )
  663. builder.activate_block(regular_block)
  664. retval = builder.coerce(builder.unary_op(eqval, "not", line), object_rprimitive, line)
  665. builder.add(Return(retval))
  666. builder.activate_block(not_implemented_block)
  667. builder.add(Return(not_implemented))
  668. def load_non_ext_class(
  669. builder: IRBuilder, ir: ClassIR, non_ext: NonExtClassInfo, line: int
  670. ) -> Value:
  671. cls_name = builder.load_str(ir.name)
  672. add_dunders_to_non_ext_dict(builder, non_ext, line)
  673. class_type_obj = builder.py_call(
  674. non_ext.metaclass, [cls_name, non_ext.bases, non_ext.dict], line
  675. )
  676. return class_type_obj
  677. def load_decorated_class(builder: IRBuilder, cdef: ClassDef, type_obj: Value) -> Value:
  678. """Apply class decorators to create a decorated (non-extension) class object.
  679. Given a decorated ClassDef and a register containing a
  680. non-extension representation of the ClassDef created via the type
  681. constructor, applies the corresponding decorator functions on that
  682. decorated ClassDef and returns a register containing the decorated
  683. ClassDef.
  684. """
  685. decorators = cdef.decorators
  686. dec_class = type_obj
  687. for d in reversed(decorators):
  688. decorator = d.accept(builder.visitor)
  689. assert isinstance(decorator, Value)
  690. dec_class = builder.py_call(decorator, [dec_class], dec_class.line)
  691. return dec_class
  692. def cache_class_attrs(
  693. builder: IRBuilder, attrs_to_cache: list[tuple[Lvalue, RType]], cdef: ClassDef
  694. ) -> None:
  695. """Add class attributes to be cached to the global cache."""
  696. typ = builder.load_native_type_object(cdef.info.fullname)
  697. for lval, rtype in attrs_to_cache:
  698. assert isinstance(lval, NameExpr)
  699. rval = builder.py_get_attr(typ, lval.name, cdef.line)
  700. builder.init_final_static(lval, rval, cdef.name, type_override=rtype)
  701. def create_mypyc_attrs_tuple(builder: IRBuilder, ir: ClassIR, line: int) -> Value:
  702. attrs = [name for ancestor in ir.mro for name in ancestor.attributes]
  703. if ir.inherits_python:
  704. attrs.append("__dict__")
  705. items = [builder.load_str(attr) for attr in attrs]
  706. return builder.new_tuple(items, line)
  707. def add_dunders_to_non_ext_dict(
  708. builder: IRBuilder, non_ext: NonExtClassInfo, line: int, add_annotations: bool = True
  709. ) -> None:
  710. if add_annotations:
  711. # Add __annotations__ to the class dict.
  712. builder.add_to_non_ext_dict(non_ext, "__annotations__", non_ext.anns, line)
  713. # We add a __doc__ attribute so if the non-extension class is decorated with the
  714. # dataclass decorator, dataclass will not try to look for __text_signature__.
  715. # https://github.com/python/cpython/blob/3.7/Lib/dataclasses.py#L957
  716. filler_doc_str = "mypyc filler docstring"
  717. builder.add_to_non_ext_dict(non_ext, "__doc__", builder.load_str(filler_doc_str), line)
  718. builder.add_to_non_ext_dict(non_ext, "__module__", builder.load_str(builder.module_name), line)