1 | Proposal For Use of Virtual Tables
|
---|
2 | ==================================
|
---|
3 |
|
---|
4 | This is an adaptation of the earlier virtual proposal, updating it with new
|
---|
5 | ideas, reframing it and laying out more design decisions.
|
---|
6 |
|
---|
7 | The basic concept of a virtual table (vtable) is the same here as in most
|
---|
8 | other languages. They will mostly contain function pointers although they
|
---|
9 | should be able to store anything that goes into a trait.
|
---|
10 |
|
---|
11 | This should replace the virtual proposal, although all not all features have
|
---|
12 | been converted to the new design.
|
---|
13 |
|
---|
14 | Trait Instances
|
---|
15 | ---------------
|
---|
16 |
|
---|
17 | Currently traits are completely abstract. Data types might implement a trait
|
---|
18 | but traits are not themselves data types. This will change that and allow
|
---|
19 | instances of traits to be created from instances of data types that implement
|
---|
20 | the trait.
|
---|
21 |
|
---|
22 | trait combiner(otype T) {
|
---|
23 | void combine(T&, int);
|
---|
24 | };
|
---|
25 |
|
---|
26 | struct summation {
|
---|
27 | int sum;
|
---|
28 | };
|
---|
29 |
|
---|
30 | void ?{}( struct summation & this ) {
|
---|
31 | this.sum = 0;
|
---|
32 | }
|
---|
33 |
|
---|
34 | void combine( struct summation & this, int num ) {
|
---|
35 | this.sum = this.sum + num;
|
---|
36 | }
|
---|
37 |
|
---|
38 | trait combiner obj = struct summation{};
|
---|
39 | combine(obj, 5);
|
---|
40 |
|
---|
41 | Internally a trait object is a pair of pointers. One to an underlying object
|
---|
42 | and the other to the vtable. All calls on an trait are implemented by looking
|
---|
43 | up the matching function pointer and passing the underlying object and the
|
---|
44 | remaining arguments to it.
|
---|
45 |
|
---|
46 | Trait objects can be copied and moved by copying and moving the pointers.
|
---|
47 | They should also be able to own or borrow the underlying object.
|
---|
48 |
|
---|
49 | Hierarchy
|
---|
50 | ---------
|
---|
51 |
|
---|
52 | Virtual tables by them selves are not quite enough to implement the planned
|
---|
53 | hierarchy system. An addition of type ids, implemented as pointers which
|
---|
54 | point to your parent's type id, is required to actually create the shape of
|
---|
55 | the hierarchy. However vtables would allow behaviour to be carried with the
|
---|
56 | tree.
|
---|
57 |
|
---|
58 | The hierachy would be a tree of types, of traits and structs. Currently we do
|
---|
59 | not support structural extension, so traits form the internal nodes and
|
---|
60 | structures the leaf nodes.
|
---|
61 |
|
---|
62 | The syntax is undecided but it will include a clause like `virtual (PARENT)`
|
---|
63 | on trait and struct definitions. It marks out all types in a hierarchy.
|
---|
64 | PARENT may be omitted, if it is this type is the root of a hierachy. Otherwise
|
---|
65 | it is the name of the type that is this type's parent in the hierarchy.
|
---|
66 |
|
---|
67 | Traits define a trait instance type that implements all assertions in this
|
---|
68 | trait and its parents up until the root of the hierarchy. Each trait then
|
---|
69 | defines a vtable type. Structures will also have a vtable type but it should
|
---|
70 | be the same as their parent's.
|
---|
71 |
|
---|
72 | Trait objects within the tree can be statically cast to a parent type. Casts
|
---|
73 | from a parent type to a child type are conditional, they check to make sure
|
---|
74 | the underlying instance is an instance of the child type, or an instance of
|
---|
75 | one of its children. The type then is recoverable at runtime.
|
---|
76 |
|
---|
77 | As with regular trait objects, calling a function on a trait object will cause
|
---|
78 | a lookup on the the virtual table. The casting rules make sure anything that
|
---|
79 | can be cast to a trait type will have all the function implementations for
|
---|
80 | that trait.
|
---|
81 |
|
---|
82 | Converting from a concrete type (structures at the edge of the hierchy) to
|
---|
83 | an abstract type works the same as with normal trait objects, the underlying
|
---|
84 | object is packaged with a virtal table pointer. Converting back to an abstract
|
---|
85 | type requires confirming the underlying type matches, but then simply extracts
|
---|
86 | the pointer to it.
|
---|
87 |
|
---|
88 | ### Inline vtables
|
---|
89 | Since the structures here are usually made to be turned into trait objects
|
---|
90 | it might be worth it to have fields on them to store the virtual table
|
---|
91 | pointer. This would have to be declared on the trait as an assertion, but if
|
---|
92 | it is the trait object could be a single pointer.
|
---|
93 |
|
---|
94 | It is trival to do if the field with the virtual table pointer is fixed.
|
---|
95 | Otherwise some trickery with pointing to the field and storing the offset in
|
---|
96 | the virtual table to recover the main object would have to be used.
|
---|
97 |
|
---|
98 | ### Virtual Tables as Types
|
---|
99 | Here we consider encoding plus the implementation of functions on it. Which
|
---|
100 | is to say in the type hierarchy structures aren't concrete types anymore,
|
---|
101 | instead they are parent types to vtables, which combine the encoding and
|
---|
102 | implementation.
|
---|
103 |
|
---|
104 | Resolution Scope
|
---|
105 | ----------------
|
---|
106 |
|
---|
107 | What is the scope of a resolution? When are the functions in a vtable decided
|
---|
108 | and how broadly is this applied?
|
---|
109 |
|
---|
110 | ### Type Level:
|
---|
111 | Each structure has a single resolution for all of the functions in the
|
---|
112 | virtual trait. This is how many languages that implement this or similar
|
---|
113 | features do it.
|
---|
114 |
|
---|
115 | The main thing CFA would need to do it this way is some single point where
|
---|
116 | the type declaration, including the functions that satify the trait, are
|
---|
117 | all defined. Currently there are many points where this can happen, not all
|
---|
118 | of them will have the same definitions and no way to select one over the
|
---|
119 | other.
|
---|
120 |
|
---|
121 | Some syntax would have to be added. All resolutions can be found at compile
|
---|
122 | time and a single vtable created for each type at compilation time.
|
---|
123 |
|
---|
124 | ### Explicate Resolution Points:
|
---|
125 | Slightly looser than the above, there are explicate points where the vtables
|
---|
126 | are resolved, but there is no limit on the number of resolution points that
|
---|
127 | might be provided. Each time a object is bound to a trait, one of the
|
---|
128 | resolutions is selected. This might be the most flexable option.
|
---|
129 |
|
---|
130 | An syntax would have to be provided as above. There may also be the option
|
---|
131 | to name resolution points so that you can choose between them. This also
|
---|
132 | could come with the ablity to forward declare them.
|
---|
133 |
|
---|
134 | Especially if they are not named, these resolution points should be able to
|
---|
135 | appear in functions, where the scoping rules can be used to select one.
|
---|
136 | However this also means that stack-allocated functions can end up in the
|
---|
137 | vtable.
|
---|
138 |
|
---|
139 | ### Site Based Resolution:
|
---|
140 | Every place in code where the binding of a vtable to an object occurs has
|
---|
141 | its own resolution. Syntax-wise this is the simplest as it should be able
|
---|
142 | to use just the existing declarations and the conversion to trait object.
|
---|
143 | It also is very close to the current polymorphic reolution rules.
|
---|
144 |
|
---|
145 | This works as the explicate resolution points except the resolution points
|
---|
146 | are implicate and their would be no selection of which resolution to use. The
|
---|
147 | closest (current) resolution is always selected.
|
---|
148 |
|
---|
149 | This could easily lead to an explosition of vtables as it has the most fine
|
---|
150 | grained resolution the number of bindings in a single scope (that produces
|
---|
151 | the same binding) could be quite high. Merging identical vtables might help
|
---|
152 | reduce that.
|
---|
153 |
|
---|
154 | Vtable Lifetime Issues
|
---|
155 | ----------------------
|
---|
156 |
|
---|
157 | Vtables interact badly with the thunk issue. Conceptually vtables are static
|
---|
158 | like type/function data they carry, as those decitions are made by the
|
---|
159 | resolver at compile time.
|
---|
160 |
|
---|
161 | Stack allocated functions interact badly with this because they are not
|
---|
162 | static. There are serveral ways to try to resolve this, however without a
|
---|
163 | general solution most can only buy time.
|
---|
164 |
|
---|
165 | Filling in some fields of a static vtable could cause issues on a recursive
|
---|
166 | call. And then we are still limited by the lifetime of the stack functions, as
|
---|
167 | the vtable with stale pointers is still a problem.
|
---|
168 |
|
---|
169 | Dynamically allocated vtables introduces memory management overhead and
|
---|
170 | requires some way to differentate between dynamic and statically allocated
|
---|
171 | tables. The stale function pointer problem continues unless those becomes
|
---|
172 | dynamically allocated as well which gives us the same costs again.
|
---|
173 |
|
---|
174 | Stack allocating the vtable seems like the best issue. The vtable's lifetime
|
---|
175 | is now the limiting factor but it should be effectively the same as the
|
---|
176 | shortest lifetime of a function assigned to it. However this still limits the
|
---|
177 | lifetime "implicately" and returns to the original problem with thunks.
|
---|