source: doc/proposals/autogen.md @ 9476549

Last change on this file since 9476549 was 2c8946b, checked in by Andrew Beach <ajbeach@…>, 6 months ago

Various whitespace and intentation updates.

  • Property mode set to 100644
File size: 9.3 KB
Line 
1# Review of Autogeneration
2There have been known issues with auto-generated routines for a long time. Although no one has time to leap onto the problem right now, we figure people should start thinking about that. And the first part of that is to get all the grievances with the current system.
3
4# Core Features
5What are the core features of autogeneration, or that autogeneration allows?
6
7## C Compatibility
8Old C code should continue to work without any (or mimimal). Furthermore, C-style code should usually work when mixed with CFA features. This includes behaviour not implemented as operators in CFA (such as field access and designators) as well as those that do.
9
10Note, that some CFA feature can disable C Compatibility, for instance visibility modifiers on fields might disable by-field initialization. However, orthogonal features, such as polymorphism, should not.
11
12## Life-Time Functions
13We want to get the life-time functions (destructor, copy assignment and copy construction) without having to write them when they are obvious.
14
15This actually has a lot of overlap with C Compatibility, in that these are also things you can do with them in C. So these functions should act like the primitive operations in C in those cases.
16
17## Custom Implementations
18We should be able to write custom implementations of the operators. These can be used to replace one of the generated functions. It also or to add a new operator for the type.
19
20## Purposeful Missing Functions
21For the C-Compatibility's functions and life-time functions, sometimes we do not need, and in fact do not want, some of those functions. These should be possible to remove and any attempt to use them should be rejected on compilation.
22
23# Problems
24Those are the principles we have, but here are particular issues.
25(Thanks to Mike for producing a lot of the examples.)
26
27## Problems With Generated Functions
28
29### Value Call Semantics
30This is actually more general issue than autogenerated functions, but the the copy constructor and copy assignment operators still take their source argument by value. They have to be copied in C-style to implement the copy operator. When it is fixed, then autogeneration will have to be updated as well.
31
32Current Forms:
33        void ?{}(char &, char);
34        char ?=?(char &, char);
35
36New Forms:
37        void ?{}(char &, char const &);
38        char & ?=?(char &, char const &);
39
40### Unused Assertions Still Added to the Assertion List
41All assertions on the type decklaration are used in all autogenerated functions even if they are never used. For example:
42
43The declaration of:
44        forall(T)
45        struct Cell { T x; }
46
47Results in the following autogenerated expands to:
48        forall(T* | { T ?=?(T&, T); void ?{}(T&); void ?{}(T&, T); void ^?{}(T&); })
49        void ?{}(Cell(T)&);
50        forall(T* | { T ?=?(T&, T); void ?{}(T&); void ?{}(T&, T); void ^?{}(T&); })
51        void ?{}(Cell(T)&, Cell(T));
52        forall(T* | { T ?=?(T&, T); void ?{}(T&); void ?{}(T&, T); void ^?{}(T&); })
53        void ^?{}(Cell(T)&);
54        forall(T* | { T ?=?(T&, T); void ?{}(T&); void ?{}(T&, T); void ^?{}(T&); })
55        void ?=?(Cell(T)&, Cell(T));
56        forall(T* | { T ?=?(T&, T); void ?{}(T&); void ?{}(T&, T); void ^?{}(T&); })
57        void ?{}(Cell(T)&, T);
58
59If these assertions were reduced to the minimial required assertions the result would instead look something like the:
60        forall(T* | { void ?{}(T&); })
61        void ?{}(Cell(T)&);
62        forall(T* | { void ?{}(T&, T); })
63        void ?{}(Cell(T)&, Cell(T));
64        forall(T* | { void ^?{}(T&); })
65        void ^?{}(Cell(T)&);
66        forall(T* | { T ?=?(T&, T); })
67        void ?=?(Cell(T)&, Cell(T));
68        forall(T* | { void ?{}(T&, T); })
69        void ?{}(Cell(T)&, T);
70
71This leads to exponential thunk generation for `Cell(Cell(int))` (or a matrix represented `vector(vector(vector(int)))`).
72
73### Autogened Functions cannot use Avaible Functions
74If you supply an implementation for one of the autogenerated functions, it will not be used while generating other functions.
75
76Consider a case with a custom copy constructor but don't define an assignment operator. The current (problematic) behaviour reimplements the assignment operator member-wise. The ideal solution would be to create a new implementation of the operator that applies the approprate destructor, then the custom copy constructor. Although this implementation may be slower, it will have correct behaviour if the other operators are implemented properly.
77
78An alternate behaviour would simply to remove the assignment operator entirely unless the users explicity provides one. This is more similar to C++'s "The Rule of Three" (or "The Rule of Five" with move operations), where all three of the lifetime functions must be redefined if any of them are. The advantage of the new assignment operator (mentioned in the "ideal solution") is that it avoids a similar rule of three, needing only destruction and copy construction for proper lifetime behaivour.
79
80## Problems With Removed Functions
81
82### Failed Autogeneration Leaves Behind Declaration
83All autogenerated functions are a checked by attempting to resolve it. If there is an error than the autogenerated function is removed. But that only removes the definition, so it can still be considered as a candidate for resolution. The following code will compile but fail during linking.
84
85        forall(T *) struct Cell { T x; };
86        Cell(char) s;
87
88This should be an error at resolution time, reporting that no such constructor is defined, instead of making it all the way to the linker.
89
90### Overriding a Function can Lead to Problems
91Implementing your oven version of a function should always override the autogenerated function. This does not happen, especially if the declared function does not use the exact same assertions as the autogenerated function (provided via the type declaration itself).
92
93(This issue is filled as Trac Ticket 186.)
94
95### Cannot Manually Remove Functions
96You cannot request that a function not be generated. The above cases could be worked around if you could. In addition, there are cases where an autogenerated routine could be created, but you do not want that operator to be be callable at all.
97
98You can delete (using `= void`) functions to mask the autogeneration functions. However the autogenerated functions still exist and you have to get the signatures exactly the same, so this is not considered practical.
99
100The main reason to manually remove functions is to enforce a behaviour based interface for a type, as opposed to a data based one. To enforce that new interface, this would have to interact with visibility.
101
102## Other Problems & Requested Features
103
104### Designators
105Designators (named parameters or keyword arguments) are nice features and being able to use them with constructors/initializers are really nice. This could be either a general solution for keyword arguments or something special for initializers.
106
107        vector v = {capacity: 128};
108
109The designator syntax (including in the example) being different from C is also a problem for compatability, but does not change their use in pure Cforall.
110
111### Non-Intuitive Reference Initializer
112Initializing reference type struct members can easily catch up beginners. The following piece of code compiles without error or warning, but will lead to a segmentation fault.
113
114        struct S { int & x; };
115        void ?{}( S & this, int & x ) {
116                (this.x){ x };
117                // The correct way to implement this operation.
118        //      (&this.x){ &x };
119        }
120
121### No Const Field Initialization
122One cannot initialize a constant field without using a cast, which makes writing constructors for types with such field more difficult. The following example does not compile.
123
124        struct Const { const int x; };
125        void ?{}( Const & this, int x ) {
126                (this.x){ x };
127                // A correct way to implement this operation.
128        //      ?{}(*(int*)&this.x, x);
129        }
130
131(The `(*(int*)&this.x){ x };` form appears not to work for unrelated reasons.)
132
133### New Type Parameter Shorthand
134A request to include another parameter shorthand for a group of assertions between sized and object-type. Notably, often we don't need to create fresh instance, we just want to manipulate existing instances and destroy them when we are done.
135
136Mike reports that for `forall(T * | [life-time-assertions])` cases he sees an approximate breakdown of:
137
13820%:  dtor
13940%:  dtor + copy ctor
14010%:  dtor + copy ctor + no-arg ctor
14120%:  dtor + copy ctor + custom ctor  (no need for a no-arg ctor)
14210%:  anything else
143
144(This was not counting copy assignment, although it could be considered an optimization of destory and then copy (re)construct.)
145
146### Incorrect Field Detection
147When you do write your own constructor (or destructor) any fields you do not construct (or destruct) particular fields they are automatically constructed (or destructed). But the detection is inaccurate.
148
149Exact issues are not known. But at the very least the rules are not clearly documented because no one seems to know what they are.
150
151### No-op Constructor
152This may be solved, in some cases, but there is not a clear interface for not running constructors. It would be nice to, like in C, to leave stack allocated variable uninitialized, this is mostly a preformance issue but can allow you do declare a variable before the information to construct it is read.
153
154However, if a constructor is run, then all of its components should be initialized by default.
155
156### Earlier Inline of Autogenerated Function
157A warning that comes up around autogenerated functions mentions static function called from inline functions. Although, this may not lead to problems, it does highlight some issues with the C initializer to Cforall constructor conversion.
Note: See TracBrowser for help on using the repository browser.