1 Data Structures
2 ===============
3
4 The __class__ Attribute
5 -----------------------
6
7 All objects support the __class__ attribute:
8
9 Class: refers to the type class (type.__class__ also refers to the type class)
10 Function: refers to the function class
11 Instance: refers to the class instantiated to make the object
12
13 Invocation
14 ----------
15
16 The following actions need to be supported:
17
18 Class: create instance, call __init__ with instance, return object
19 Function: call function body, return result
20 Instance: call __call__ method, return result
21
22 Structure Layout
23 ----------------
24
25 The __new__ reference would lead to code consisting of the following
26 instructions:
27
28 create instance for C
29 call C.__init__(instance, ...)
30 return instance
31
32 If C has a __call__ attribute, the invocation "slot" of C instances would
33 refer to the same thing as C.__call__. This "slot" has to be prepared when
34 creating instances, either by modifying the sequence of instructions used in,
35 amongst other places, the instantiator function, or by generating a template
36 instance whose details are copied when new instances are created.
37
38 For functions, the same general layout applies:
39
40 Function f:
41
42 0 1 2 3 4 5 6
43 code for attrcode code code class attribute ...
44 function for reference #args, function (default)
45 function defaults reference reference
46 reference
47
48 Here, the code reference would lead to code for the function. Note that the
49 function locals are completely distinct from this structure and are not
50 comparable to attributes. Instead, attributes are reserved for default
51 parameter values.
52
53 For modules, there is no meaningful invocation reference:
54
55 Module m:
56
57 0 1 2 3 4 5 6
58 code for m attrcode (unused) (unused) module type attribute ...
59 for m reference (global)
60 reference
61
62 Both classes and modules have code in their definitions, but this would be
63 generated in order and not referenced externally.
64
65 Invocation Operation
66 --------------------
67
68 Consequently, regardless of the object an invocation is always done as
69 follows:
70
71 get invocation reference (at object+1)
72 jump to reference
73
74 Additional preparation is necessary before the above code: positional
75 arguments must be saved to the parameter stack, and keyword arguments must be
76 resolved and saved to the appropriate position in the parameter stack.
77
78 Attribute Operations
79 --------------------
80
81 Attribute access needs to go through the attribute lookup table. Some
82 optimisations are possible and are described in the appropriate section.
83
84 One important aspect of attribute access is the appropriate setting of the
85 context in the acquired attribute value. From the table describing the
86 acquisition of values, it is clear that the principal exception is that where
87 a class-originating attribute is accessed on an instance. Consequently, the
88 following algorithm could be employed once an attribute has been located:
89
90 1. If the attribute's context is a special value, indicating that it should
91 be replaced upon instance access, then proceed to the next step;
92 otherwise, acquire both the context and the object as they are.
93
94 2. If the accessor is an instance, use that as the value's context, acquiring
95 only the object from the attribute.
96
97 Where accesses can be determined ahead of time (as discussed in the
98 optimisations section), the above algorithm may not necessarily be employed in
99 the generated code for some accesses.
100
101 Instance/Class Compatibility
102 ----------------------------
103
104 Although it would be possible to have a data structure mapping classes to
105 compatible classes, which in the case of context (or self argument)
106 suitability in invocations would involve a mapping from a class to itself plus
107 its descendants, the need to retain the key to such a data structure for each
108 class might introduce a noticeable overhead. Such a structure would
109 effectively be a matrix with each dimension indexed using the same sequence of
110 codes for each of the classes in a program.
111
112 The current solution is to insert descendants as special attributes into the
113 object/attribute lookup table. This requires an extra key to be retained,
114 since each class must provide its own attribute code such that upon an
115 instance/class compatibility test, the code may be obtained and used in the
116 object table.