source: doc/proposals/alt-enums.md @ 878b1385

Last change on this file since 878b1385 was 8c267cf, checked in by Andrew Beach <ajbeach@…>, 18 months ago

Added proposal for alternate enumerations that could replace some of the typed enumeration features.

  • Property mode set to 100644
File size: 6.2 KB
Line 
1Alternate Enumeration Types
2===========================
3A proposal to rework enumeration types.
4
5This is a new design for enumerations in Cforall.
6They are based around extending C enumerations focusing on the logical
7view of enumerations, keeping the underlying representation opaque.
8
9This does replace some of the existing features of Cforall; so implementing
10it will require redoing some work. However, while I do think the improvement
11will be worth it, I have included notes about how to regain some of the
12functionality/ease-of-use of typed enumerations. Because many of these uses
13were in the area of "enums as macros", some are not even enumeration features.
14
15Note that all the new syntax and names are only examples;
16in addition to any functionally changes that could happen.
17
18Sequential Enumerations
19----------------------
20The sequential enumeration is the most basic form of enumeration.
21
22```cfa
23enum Colour seq {
24  Red,
25  Purple,
26  Violet = Purple,
27  Blue,
28  Green,
29  Yellow,
30  Orange,
31};
32```
33
34Uses traditional enum declaration syntax, where each label is uninitialized
35or initialized to another label in the enumeration (no cycles, possibly
36limited to previous labels).
37
38Uninitialized labels introduce a new possible value into the enumeration.
39This value has no representation beyond the label(s) that represents it.
40Initialized labels are simple aliases to an existing value.
41
42A type created this way automatically supports: copy construction,
43copy assignment, destruction and equality, inequality and ordering with
44itself. Each value is equal only to itself, so labels are equal to themselves
45and other labels via aliasing. Values are ordered low to high by first to
46last appearance of the uninitialized label.
47They can also be used in a switch statement with the labels used in case
48labels.
49(Default construction could be added by picking one of the labels - say the
50first - to be the default.)
51
52Note: This does not add the enumeration labels to the enclosing scope. Use
53qualification to access it or write `inline enum Colour seq ...` to begin
54the enumeration.
55This is dependent on the module and namespacing system design.
56
57Enumerated Arrays
58-----------------
59Arrays that use an enumeration as their index. The entire enumeration type
60(instead of a subset of int) is used in the index operation.
61
62```cfa
63extern string colourNames[Colour];
64```
65
66This example is a forward declaration that declares the symbol but does not
67give the values or allocate any storage. This is used in header files.
68The type of colourNames would be a new type `string[Colour]`.
69
70In implementation tiles it is safe to give the array's values;
71whether it the array has been previously forward declared or not.
72```cfa
73string colourNames[Colour] = {
74  "red",
75  "violet",
76  "blue",
77  // Or without worrying about ordering:
78  [Green] = "green",
79  [Orange] = "orange",
80  [Yellow] = "yellow",
81};
82```
83
84The forward declaration and full definition variants allow the user to manage
85memory themselves, following the same rules as `extern` variables.
86The user can use `const` to fix the values in the array.
87
88Except for the index type (and that the size of the array is fixed per
89index type, as it always covers the whole enumeration) it should be the same
90as a traditional array.
91
92Or one of the new safer Cforall arrays, as the features could be combined.
93
94Combined Declaration
95--------------------
96People have requested, as a convenience feature, the ability to declare the
97enumeration and the array in one go. I say "People have requested" because
98I'm still working on the details, from storage to syntax, but a rough sketch
99of the feature would look like this:
100
101```cfa
102string colourNames[inline enum Colour] = {
103  Red = "red",
104  Purple = "violet",
105  Blue = "blue",
106  Green = "green",
107  Yellow = "yellow",
108  Orange = "orange",
109};
110```
111
112This should possibly be implemented as an extension.
113
114Enumeration Iteration
115---------------------
116Iterate through all values in a sequential enumeration.
117
118The design very dependent on the overall design of the for-each loop and
119iterators in general (when added to the language).
120However, the behaviour should just go over every value in the enumeration in
121order of the initial appearance in the declaration.
122
123Flag Set Enumerations
124---------------------
125Another common use of enumerations is as a named bitset.
126
127```cfa
128enum Directions flag {
129  Up,
130  Down,
131  Left,
132  Right,
133  Upwards = Up,
134  Vertical = Up | Down,
135};
136```
137
138Uses the existing enumeration syntax, except that all initializers must be
139bitwise expressions, using only the operators |, & and ~ and, as leaf values,
140other labels from the enumeration (no cycles) and 0.
141
142Each uninitialized label creates a new flag. Every instance of the
143enumeration will have each flag be set or unset. The labels act as instances
144of the enumeration with only that flag set.
145
146A type created this way automatically supports: default construction,
147from zero_t construction, copy construction, copy assignment, destruction,
148equality, inequality and bitwise and (&), or (|) and not (~).
149Default construction and from zero_t construction create an instance with no
150flags set. Two instances are the same if the same flags are set.
151Bitwise operations act on the individual flags in the set.
152
153In addition the type can be converted to a Boolean.
154An flag set is truthy if any flags are set and falsy if no flags are set.
155This is not a primitive operation, but comes from combining the zero_t
156constructor and inequality.
157
158Note: Scoping rules are also waiting on the namespacing and module system.
159
160Named Literal
161-------------
162(Replacing the "enum as macros" uses of typed enumerations.)
163
164A new storage class specifier called `literal`. It specifies that the value
165is not stored but it is inlined at each use. These can be used like a macro
166except they respect scoping rules and are resolved and evaluated once.
167
168A variable with literal storage allocates no memory and does not define
169any symbols for the linker. They must be initialized with a constant
170expression. All uses of a literal variable are rvalues (that is to say, they
171are treated as literals, you may not assign to them or get their address).
172
173```cfa
174literal int two = 2;
175```
176
177To allow this, there may have to be restrictions on the types that can be
178used with literal storage class. Some more implementation details will have
179to be known to decide what those are.
Note: See TracBrowser for help on using the repository browser.