[mypyc] Fix attribute offsets when only a subclass defines __call__ (#22483)

The object struct of a native class that has a `__call__` method has a
`vectorcall` field (used through `tp_vectorcall_offset`). It was placed
right after `vtable`, before the attributes. A class inherits `__call__`
from its bases, so if only a subclass defines it, the subclass struct
has the extra field while the base class struct doesn't. All attributes
inherited from the base class were then at different offsets in the
subclass, and code compiled for the base class (methods, attribute
access through the base type) read and wrote the wrong fields. This is a
segfault or memory corruption:

```python
class A:
    def __init__(self, x: str) -> None:
        self.x = x

    def get_x(self) -> str:
        return self.x

class B(A):
    def __call__(self) -> str:
        return self.x

B("b")()  # Segfault
```

Generated struct of `B`:

```c
typedef struct {
    PyObject_HEAD
    CPyVTableItem *vtable;
    vectorcallfunc vectorcall;  // not in the struct of A
    PyObject *_x;               // at a different offset than in A
} B;
```

`A.__init__` (compiled for the struct of `A`) stores `x` in the
`vectorcall` field of the `B` instance, so calling it jumps to the
address of a `str` object. With more attributes, base class methods and
subclass code also see each other's attributes in the wrong fields.

Now the `vectorcall` field goes after the attributes, so the attributes
of a base class are at the same offsets in all subclasses. The field is
only accessed through the struct of the class itself (in its setup
function, which isn't shared with native subclasses) and through
`tp_vectorcall_offset`, which is set for each class, so it's fine for
its offset to differ between a class and its subclasses. This also works
with separate compilation, since each module computes the layout from
the class IR in the same way.

Classes where the base class also defines `__call__` were not affected,
and neither were classes without attributes.

Tests: a run test with `__call__` defined only in a subclass, accessing
inherited attributes through base class methods, directly and through
`Any`; `__call__` overridden in a sub-subclass; an intermediate class
with attributes; a base class with weak reference support; and
interpreted subclasses (one inheriting `__call__`, one defining it). A
multi-module test has the base class in another module (another group in
separate compilation mode). The new run test fails on master.
diff --git a/mypyc/codegen/emitclass.py b/mypyc/codegen/emitclass.py
index 30bd660..2e00900 100644
--- a/mypyc/codegen/emitclass.py
+++ b/mypyc/codegen/emitclass.py
@@ -587,8 +587,6 @@
         # This must come before any field whose presence depends on the class, since
         # tp_weaklistoffset has to agree between a base class and its subclasses.
         lines.append("PyObject *weakreflist;")
-    if cl.has_method("__call__"):
-        lines.append("vectorcallfunc vectorcall;")
     # The flag affects attribute offsets, so concrete bases must agree on its presence.
     assert all(
         base.has_running_flag == cl.has_running_flag for base in cl.base_mro
@@ -617,6 +615,14 @@
                     if isinstance(rtype, RTuple):
                         emitter.declare_tuple_struct(rtype)
 
+    if cl.has_method("__call__"):
+        # This goes after the attributes, since a subclass may define __call__ when a
+        # base class doesn't, and the attributes of the base class must be at the same
+        # offsets in the subclass. The field is only accessed through the struct of the
+        # class or through tp_vectorcall_offset, so its offset can differ between
+        # classes.
+        lines.append("vectorcallfunc vectorcall;")
+
     lines.append(f"}} {cl.struct_name(emitter.names)};")
     lines.append("")
     emitter.context.declarations[cl.struct_name(emitter.names)] = HeaderDeclaration(
diff --git a/mypyc/test-data/run-classes.test b/mypyc/test-data/run-classes.test
index 1304837..17428ab 100644
--- a/mypyc/test-data/run-classes.test
+++ b/mypyc/test-data/run-classes.test
@@ -4659,6 +4659,139 @@
 def test_inherited() -> None:
     assert Derived()() == 1
 
+[case testClassWith__call__InSubclassOnly]
+import gc
+import weakref
+from typing import Any
+from mypy_extensions import i64, mypyc_attr
+
+class Base:
+    def __init__(self, x: str, n: i64) -> None:
+        self.x = x
+        self.n = n
+        self.b = True
+
+    def get_x(self) -> str:
+        return self.x
+
+    def get_n(self) -> i64:
+        return self.n
+
+    def set_x(self, x: str) -> None:
+        self.x = x
+
+class CallSub(Base):
+    def __init__(self, x: str, n: i64) -> None:
+        super().__init__(x, n)
+        self.y = "y"
+
+    def __call__(self, a: int) -> str:
+        return f"{self.x}:{self.n}:{self.y}:{a}"
+
+class CallSubSub(CallSub):
+    def __init__(self, x: str, n: i64) -> None:
+        super().__init__(x, n)
+        self.z = "z"
+
+    def __call__(self, a: int) -> str:
+        return f"{self.x}:{self.y}:{self.z}:{a}"
+
+class Mid(Base):
+    def __init__(self, x: str, n: i64) -> None:
+        super().__init__(x, n)
+        self.m = "m"
+
+class CallSubOfMid(Mid):
+    def __call__(self) -> str:
+        return self.x + self.m
+
+@mypyc_attr(weakrefs=True)
+class WeakBase:
+    def __init__(self, x: str) -> None:
+        self.x = x
+
+    def get_x(self) -> str:
+        return self.x
+
+class WeakCallSub(WeakBase):
+    def __init__(self, x: str) -> None:
+        super().__init__(x)
+        self.y = "y"
+
+    def __call__(self) -> str:
+        return self.x + self.y
+
+@mypyc_attr(allow_interpreted_subclasses=True)
+class OpenBase:
+    def __init__(self, x: str) -> None:
+        self.x = x
+
+    def get_x(self) -> str:
+        return self.x
+
+@mypyc_attr(allow_interpreted_subclasses=True)
+class OpenCallSub(OpenBase):
+    def __init__(self, x: str) -> None:
+        super().__init__(x)
+        self.y = "y"
+
+    def __call__(self) -> str:
+        return self.x + self.y
+
+def get_x(b: Base) -> str:
+    return b.x
+
+def test_subclass_defines_call() -> None:
+    c = CallSub("x", 5)
+    assert c.get_x() == "x"
+    assert c.get_n() == 5
+    assert get_x(c) == "x"
+    assert c.x == "x" and c.n == 5 and c.b and c.y == "y"
+    c.set_x("xx")
+    assert c.x == "xx"
+    assert c(1) == "xx:5:y:1"
+    o: Any = c
+    assert o(2) == "xx:5:y:2"
+    assert o.get_x() == "xx"
+
+def test_call_overridden_in_subclass() -> None:
+    c = CallSubSub("x", -3)
+    assert c.get_x() == "x" and c.get_n() == -3 and get_x(c) == "x"
+    assert c.y == "y" and c.z == "z"
+    assert c(1) == "x:y:z:1"
+    s: CallSub = c
+    assert s(2) == "x:y:z:2"
+    o: Any = c
+    assert o(3) == "x:y:z:3"
+
+def test_call_in_indirect_subclass() -> None:
+    c = CallSubOfMid("x", 7)
+    assert c.get_x() == "x" and c.get_n() == 7 and get_x(c) == "x"
+    assert c.m == "m"
+    assert c() == "xm"
+
+def test_call_with_weakrefs() -> None:
+    c = WeakCallSub("x")
+    assert c.get_x() == "x" and c.y == "y"
+    assert c() == "xy"
+    r = weakref.ref(c)
+    assert r() is c
+    del c
+    gc.collect()
+    assert r() is None
+
+def test_interpreted_subclass() -> None:
+    type2: Any = type
+    Sub = type2("Sub", (OpenCallSub,), {})
+    c = Sub("x")
+    c.z = "z"
+    assert c.get_x() == "x" and c.y == "y" and c.z == "z"
+    assert c() == "xy"
+    CallSub2 = type2("CallSub2", (OpenBase,), {"__call__": lambda self: self.get_x() + "!"})
+    d = CallSub2("x")
+    assert d.get_x() == "x"
+    assert d() == "x!"
+
 [case testCallTypeObjectOfNativeClass]
 from abc import ABC, abstractmethod
 from typing import Any, overload
diff --git a/mypyc/test-data/run-multimodule.test b/mypyc/test-data/run-multimodule.test
index 3c8b4ce..5dc8151 100644
--- a/mypyc/test-data/run-multimodule.test
+++ b/mypyc/test-data/run-multimodule.test
@@ -617,6 +617,37 @@
         super().__init__(["-x"], "x")
         self.x = 1
 
+[case testMultiModuleCallInSubclassOnly]
+# The base class is in another group in separate compilation mode, and only the
+# subclass defines __call__.
+from other_call_base import CallBase, call_base_x
+
+class CallSub(CallBase):
+    def __init__(self, x: str) -> None:
+        super().__init__(x)
+        self.y = "y"
+
+    def __call__(self, a: int) -> str:
+        return self.x + self.y + str(a)
+
+def test_call_in_subclass() -> None:
+    c = CallSub("x")
+    assert c.get_x() == "x"
+    assert call_base_x(c) == "x"
+    assert c.x == "x" and c.y == "y"
+    assert c(1) == "xy1"
+
+[file other_call_base.py]
+class CallBase:
+    def __init__(self, x: str) -> None:
+        self.x = x
+
+    def get_x(self) -> str:
+        return self.x
+
+def call_base_x(b: CallBase) -> str:
+    return b.x
+
 [case testNonNativeImportInPackageFile]
 # The import is really non-native only in separate compilation mode where __init__.py and
 # other_cache.py are in different libraries and the import uses the standard Python procedure.