- Timestamp:
- Jun 23, 2017, 3:26:01 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 376ccadc, 4c03e63
- Parents:
- 653f2c7
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/proposals/tagged-struct.txt
r653f2c7 r88177cf 14 14 say which of the possible values is currently stored in the union. The idea 15 15 here is similar, however the possibilities are more open ended. 16 17 Alternate names include virtual structure and abstract structure. 16 18 17 19 … … 36 38 their parent's fields to their field list so they can be upcast. 37 39 40 The type field may be public, if it is then it can be accessed through a 41 simple field access "instance.type". The type field would then be able to be 42 used to access the type object, which contains the information for the type. 43 It may just be a pointer to the type object "*instance.type", although a 44 lookup function could also be used. 45 46 47 Usage: 48 49 The central feature for tagged structs is a checked cast between pointer types 50 to the structures. A cast is successful if the true type of the pointed object 51 is of the type being cast to or any of its children, otherwise the cast 52 returns null. 53 54 The type field should also allow for equality comparison of types. 55 56 Currently, with only these operations (and similar features) the type field 57 could be hidden and the operations given through helper functions. However 58 if the type object has more complex (or even open ended) information in it 59 than providing direct access becomes very valuable. 60 38 61 39 62 Implemenation: 40 63 41 Adding to the field list is a simple matter, should be doable during 42 translation. The type field is just a pointer to a type object. With proper 43 linking we can create a single unique instance of the type object for each 44 declared tagged struct. The instance's address is used as an id for the type. 45 It also holds data about the type, such as its parent's id/a pointer to the 46 parent type object. 64 Adding to the field list would have to be handled during translation. The 65 simple act of adding declarations should not be difficult, althought it might 66 take a bit of work to find the parent's declarations. 47 67 48 The type field could be hidden (as best as C can hide it) or it could be 49 visible to the user with easy access to allow the user to examine the type 50 object directly. 51 52 Direct access is more useful if the data on the type-objects can change, other 53 wise the build in function could handle all cases. Perhaps each root object 54 can specify a type object to use or the type objects are themselves tagged, 55 although there may not be a base case with the latter. 56 57 In the simplest case the type object is a pointer to the parent type object. 58 Additional data could be added, such as a name, or a function pointer to the 59 destructor. 68 Type objects are also simple in to generate, they should just be global 69 (program lifetime) structures. Getting there to be exactly one instance of 70 each allows the pointer to the structure to be used as the type id, and that 71 should be possible to do during linking. 60 72 61 73 … … 94 106 If unions are declared tagged instead of creating a new tagged type, all 95 107 possible values of the union must be of that tagged type or a child type. 108 109 110 Custom Type Objects (Extention): 111 112 Some method to define type objects used within a tree of types. One option is 113 to allow the tree's type object to be specified by the tree root. It would 114 then have to be filled in for each type in the tree, including the root. 115 116 The only required field is the parent field, a pointer to the type object's 117 type. (This is also the only required field on the tagged structure itself.) 118 119 A further extention could allow expanding type objects, so child types could 120 append fields to their parent's feild list. They might need their own type 121 objects at that point, or maybe static checks will be enough to see the 122 minimum field list.
Note: See TracChangeset
for help on using the changeset viewer.