source: doc/proposals/tagged-struct.txt @ c9383ee

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since c9383ee was 88177cf, checked in by Andrew Beach <ajbeach@…>, 7 years ago

Refinement of the tagged-struct proposal.

  • Property mode set to 100644
File size: 4.9 KB
Line 
1Proposal to add simple inhieritance to the language.
2
3Tagged structures allow for dynamic casting between types in a hierarchy.
4Children (rather pointers to) can be up-cast to their parents, a safe
5conversion that may recive language level support or even be implicit.
6Parents can be down cast to their children, which might fail if the underlying
7object is not of the child type, or a child of that.
8
9This does not however cause dynamic look-up. During function calls the
10underlying type is ignored, and the pointer type is used to type match the
11function call.
12
13The name tagged structure comes from tagged union, which carries a value to
14say which of the possible values is currently stored in the union. The idea
15here is similar, however the possibilities are more open ended.
16
17Alternate names include virtual structure and abstract structure.
18
19
20Syntax:
21
22"struct" name [ "tagged" [ parent-name ] ] "{" fields "}"
23
24The keywords can change (although they currently reflect the concept name
25closely). More formally, in terms of grammar this adds:
26
27struct-or-union-specifier
28        ...
29        struct identifier tagged { struct-declaration-list }
30        struct identifier tagged parent-identifier { struct-declaration-list }
31
32"tagged" by itself create a tagged structure that is the root of a new tree.
33It has no parent tagged structure. If "tagged" is used with a parent than
34that is the parent of this node.
35
36Tagged structures have fields beyond the ones listed. Root tags have a type
37field added which give the type of the instance. Child tags prepend all of
38their parent's fields to their field list so they can be upcast.
39
40The type field may be public, if it is then it can be accessed through a
41simple field access "instance.type". The type field would then be able to be
42used to access the type object, which contains the information for the type.
43It may just be a pointer to the type object "*instance.type", although a
44lookup function could also be used.
45
46
47Usage:
48
49The central feature for tagged structs is a checked cast between pointer types
50to the structures. A cast is successful if the true type of the pointed object
51is of the type being cast to or any of its children, otherwise the cast
52returns null.
53
54The type field should also allow for equality comparison of types.
55
56Currently, with only these operations (and similar features) the type field
57could be hidden and the operations given through helper functions. However
58if the type object has more complex (or even open ended) information in it
59than providing direct access becomes very valuable.
60
61
62Implemenation:
63
64Adding to the field list would have to be handled during translation. The
65simple act of adding declarations should not be difficult, althought it might
66take a bit of work to find the parent's declarations.
67
68Type objects are also simple in to generate, they should just be global
69(program lifetime) structures. Getting there to be exactly one instance of
70each allows the pointer to the structure to be used as the type id, and that
71should be possible to do during linking.
72
73
74Traits:
75
76[is_]tagged[_struct](dtype T)
77True if the given T is a tagged struct of some kind. This promises that it has
78a type object, but nothing else.
79
80[is_]tagged_under(dtype parent, dtype child)
81True if child is a child type of parent. Requires that both are tagged structs
82and that child can upcast to parent.
83
84
85Functions:
86
87forall(dtype T | is_tagged(T), dtype U | is_tagged(U))
88T * dynamic_cast(U * value)
89The cast function, that safely converts the U* into a T*, returning null if
90the underlying object value points to is not a child type of T. A shorter name
91might be perfered. The runtime should be no more than linear with the depth
92of U in the inhiertance tree.
93
94bug#11 might require `bool dynamic_cast(T ** dst, U * src)` instead.
95
96
97Tagging Unions (Extention):
98
99Using this system as is does not really work if used on unions directly.
100No new options to the union can be added, as they must be able to upcast.
101Similarly, if options are removed, writing to an upcast union is invalid.
102To allow for growth each option would have to be a structure itself.
103
104Which brings us to "tagget struct union", ie. a union of tagged structures
105as opposed to tagging the union itself. This extention acts as a constraint.
106If unions are declared tagged instead of creating a new tagged type, all
107possible values of the union must be of that tagged type or a child type.
108
109
110Custom Type Objects (Extention):
111
112Some method to define type objects used within a tree of types. One option is
113to allow the tree's type object to be specified by the tree root. It would
114then have to be filled in for each type in the tree, including the root.
115
116The only required field is the parent field, a pointer to the type object's
117type. (This is also the only required field on the tagged structure itself.)
118
119A further extention could allow expanding type objects, so child types could
120append fields to their parent's feild list. They might need their own type
121objects at that point, or maybe static checks will be enough to see the
122minimum field list.
Note: See TracBrowser for help on using the repository browser.