MCPcopy Index your code
hub / github.com/python/mypy / generate_object_struct

Function generate_object_struct

mypyc/codegen/emitclass.py:437–468  ·  view source on GitHub ↗
(cl: ClassIR, emitter: Emitter)

Source from the content-addressed store, hash-verified

435
436
437def generate_object_struct(cl: ClassIR, emitter: Emitter) -> None:
438 seen_attrs: set[str] = set()
439 lines: list[str] = []
440 lines += ["typedef struct {", "PyObject_HEAD", "CPyVTableItem *vtable;"]
441 if cl.has_method("__call__"):
442 lines.append("vectorcallfunc vectorcall;")
443 bitmap_attrs = []
444 for base in reversed(cl.base_mro):
445 if not base.is_trait:
446 if base.bitmap_attrs:
447 # Do we need another attribute bitmap field?
448 if emitter.bitmap_field(len(base.bitmap_attrs) - 1) not in bitmap_attrs:
449 for i in range(0, len(base.bitmap_attrs), BITMAP_BITS):
450 attr = emitter.bitmap_field(i)
451 if attr not in bitmap_attrs:
452 lines.append(f"{BITMAP_TYPE} {attr};")
453 bitmap_attrs.append(attr)
454 for attr, rtype in base.attributes.items():
455 # Generated class may redefine certain attributes with different
456 # types in subclasses (this would be unsafe for user-defined classes).
457 if attr not in seen_attrs:
458 lines.append(f"{emitter.ctype_spaced(rtype)}{emitter.attr(attr)};")
459 seen_attrs.add(attr)
460
461 if isinstance(rtype, RTuple):
462 emitter.declare_tuple_struct(rtype)
463
464 lines.append(f"}} {cl.struct_name(emitter.names)};")
465 lines.append("")
466 emitter.context.declarations[cl.struct_name(emitter.names)] = HeaderDeclaration(
467 lines, is_type=True
468 )
469
470
471def generate_vtables(

Callers 1

generate_class_type_declFunction · 0.85

Calls 15

HeaderDeclarationClass · 0.90
setClass · 0.85
reversedFunction · 0.85
lenFunction · 0.85
rangeClass · 0.85
isinstanceFunction · 0.85
has_methodMethod · 0.80
appendMethod · 0.80
bitmap_fieldMethod · 0.80
ctype_spacedMethod · 0.80
attrMethod · 0.80
declare_tuple_structMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…