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