1 | \chapter{Conclusion} |
---|
2 | \label{c:conclusion} |
---|
3 | |
---|
4 | This work aims to extend the simple and unsafe enumeration type in the C programming language into a complex and safe enumeration type in the \CFA programming language while maintaining backward compatibility with C. |
---|
5 | Within this goal, the new \CFA enumeration should align with the analogous enumeration features in other languages to match modern programming expectations. |
---|
6 | Hence, the \CFA enumeration features are borrowed from a number of programming languages, but engineered to work and play with \CFA's type system and feature set. |
---|
7 | |
---|
8 | Strong type-checking of enumeration initialization and assignment provides additional safety, ensuring an enumeration only contains its enumerators. |
---|
9 | Overloading and scoping of enumerators significantly reduces the naming problem, providing a better software-engineering environment, with fewer name clashes and the ability to disambiguate those that cannot be implicitly resolved. |
---|
10 | Typed enumerations solve the data-harmonization problem, increasing safety through better software engineering. |
---|
11 | Moreover, integrating enumerations with existing control structures provides a consistent upgrade for programmers and a succinct and secure mechanism to enumerate with the new loop-range feature. |
---|
12 | Generalization and reuse are supported by incorporating the new enumeration type using the \CFA trait system. |
---|
13 | Enumeration traits define the meaning of an enumeration, allowing functions to be written that work on any enumeration, such as the reading and printing of an enumeration. |
---|
14 | With advanced structural typing, C enumerations can be extended so they work with all of the enumeration features, providing for legacy C code to be moved forward into the modern \CFA programming domain. |
---|
15 | Finally, the \CFA project's test suite has been expanded with multiple enumeration features tests with respect to implicit conversions, control structures, inheritance, interaction with the polymorphic types, and the features built on top of enumeration traits. |
---|
16 | These tests ensure future \CFA work does not accidentally break the new enumeration system. |
---|
17 | |
---|
18 | In summary, the new \CFA enumeration mechanisms achieve the initial goals, providing C programmers with an intuitive enumeration mechanism for handling modern programming requirements. |
---|
19 | |
---|
20 | |
---|
21 | \section{Future Work} |
---|
22 | |
---|
23 | The following are ideas to improve and extend the work in this thesis. |
---|
24 | \begin{enumerate} |
---|
25 | \item |
---|
26 | There are still corner cases being found in the current \CFA enumeration implementation. |
---|
27 | Fixing some of these corner cases requires changes to the \CFA resolver or extensions to \CFA. %, like compile-time constant-expression evaluation. |
---|
28 | When these changes are made, it should be straightforward to update the \CFA enumeration implementation to work with them. |
---|
29 | |
---|
30 | \item |
---|
31 | Currently, some aspects of the enumeration trait system require explicitly including the file \lstinline[deletekeywords={enum}]{enum.hfa}, which can lead to problems. |
---|
32 | It should be possible to have this file included implicitly by updating the \CFA prelude. |
---|
33 | \item |
---|
34 | There are multiple \CFA features being developed in parallel with enumerations. |
---|
35 | Two closely related features are iterator and namespace. |
---|
36 | Enumerations may have to be modified to dovetail with these features. |
---|
37 | For example, enumerating with range loops does not align with the current iterator design, so some changes will be necessary. |
---|
38 | |
---|
39 | \item |
---|
40 | C already provides @const@-style aliasing using the \emph{unnamed} enumerator \see{\VRef{s:TypeName}}, even if the name @enum@ is misleading (@const@ would be better). |
---|
41 | Given the existence of this form, it is conceivable to extend it with types other than @int@. |
---|
42 | \begin{cfa} |
---|
43 | enum { Size = 20u, PI = 3.14159L, Jack = L"John" }; |
---|
44 | \end{cfa} |
---|
45 | which matches with @const@ aliasing in other programming languages. |
---|
46 | Here, the type of the enumerator is the type of the initialization constant, \eg @typeof( 20u )@ for @Size@ implies @unsigned int@. |
---|
47 | Auto-initialization is restricted to the case where all constants are @int@, matching with C. |
---|
48 | As seen in \VRef{s:EnumeratorTyping}, this feature is just a shorthand for multiple typed-enumeration declarations. |
---|
49 | \begin{cfa} |
---|
50 | enum( unsigned int ) { Size = 20u }; |
---|
51 | enum( long double ) { PI = 3.14159L }; |
---|
52 | enum( wchar_t * ) { Jack = L"John" }; |
---|
53 | \end{cfa} |
---|
54 | |
---|
55 | \item |
---|
56 | Currently, enumeration scoping is all or nothing. In some cases, it might be useful to |
---|
57 | increase the scoping granularity to individual enumerators. |
---|
58 | \begin{cfa} |
---|
59 | enum E1 { @!@A, @^@B, C }; |
---|
60 | enum E2 @!@ { @!@A, @^@B, C }; |
---|
61 | \end{cfa} |
---|
62 | Here, @'!'@ means the enumerator is scoped, and @'^'@ means the enumerator is unscoped. |
---|
63 | For @E1@, @A@ is scoped; @B@ and @C@ are unscoped. |
---|
64 | For @E2@, @A@, and @C@ are scoped; @B@ is unscoped. |
---|
65 | Finding a use case is important to justify this extension. |
---|
66 | |
---|
67 | \item |
---|
68 | An extension mentioned in \VRef{s:Ada} is using @typedef@ to create an enumerator alias. |
---|
69 | \begin{cfa} |
---|
70 | enum( int ) RGB { @Red@, @Green@, Blue }; |
---|
71 | enum( int ) Traffic_Light { @Red@, Yellow, @Green@ }; |
---|
72 | typedef RGB.Red OtherRed; // alias |
---|
73 | \end{cfa} |
---|
74 | |
---|
75 | \item |
---|
76 | Label and values arrays are auxiliary data structures that are always generated for \CFA enumeration, which is a program overhead when unused. |
---|
77 | It might be possible to provide a new syntax or annotation for a \CFA enumeration definition indicating these arrays are not used. |
---|
78 | Therefore, \CFA does not generated them. |
---|
79 | @label@ can still be used on an enumeration constant, as the call reduces to a @char *@ constant expression that holds the name of the enumerator. |
---|
80 | But calls on an enumeration variable, generate a compile-time error. |
---|
81 | The best alternative is for the linker to discard these arrays if unused. |
---|
82 | |
---|
83 | \item |
---|
84 | The \CFA enumeration has limitations with separate compilation. |
---|
85 | Consider the following: |
---|
86 | \begin{cquote} |
---|
87 | \setlength{\tabcolsep}{20pt} |
---|
88 | \begin{tabular}{@{}ll@{}} |
---|
89 | \begin{cfa} |
---|
90 | enum C_Codec { |
---|
91 | FIRST_VIDEO = 0, |
---|
92 | VP8 = 0, |
---|
93 | VP9, |
---|
94 | LAST_VIDEO, |
---|
95 | |
---|
96 | FIRST_AUDIO = 64, |
---|
97 | VORBIS = 64, |
---|
98 | OPUS, |
---|
99 | LAST_AUDIO |
---|
100 | }; |
---|
101 | \end{cfa} |
---|
102 | & |
---|
103 | \begin{cfa} |
---|
104 | enum( int ) CFA_Codec { |
---|
105 | FIRST_VIDEO = 0, |
---|
106 | VP8 = 0, |
---|
107 | VP9, |
---|
108 | LAST_VIDEO, |
---|
109 | |
---|
110 | FIRST_AUDIO = 64, |
---|
111 | VORBIS = 64, |
---|
112 | OPUS, |
---|
113 | LAST_AUDIO |
---|
114 | }; |
---|
115 | \end{cfa} |
---|
116 | \\ |
---|
117 | \begin{cfa} |
---|
118 | C_Codec c_code = OPUS; |
---|
119 | CFA_Codec cfa_code = OPUS; |
---|
120 | \end{cfa} |
---|
121 | \end{tabular} |
---|
122 | \end{cquote} |
---|
123 | @c_code@ has value 65, the integral value of @c_code.OPUS@, while @cfa_code@ has value 6, the position of @CFA_Codec.OPUS@. |
---|
124 | |
---|
125 | If the enumerator @AV1@ is inserted in @C_Codec@ and @CFA_Codec@, |
---|
126 | \begin{cquote} |
---|
127 | \setlength{\tabcolsep}{20pt} |
---|
128 | \begin{tabular}{@{}ll@{}} |
---|
129 | \begin{cfa} |
---|
130 | enum C_Codec { |
---|
131 | FIRST_VIDEO = 0, |
---|
132 | VP8 = 0, |
---|
133 | VP9, |
---|
134 | @AV1@, |
---|
135 | LAST_VIDEO, |
---|
136 | |
---|
137 | FIRST_AUDIO = 64, |
---|
138 | VORBIS = 64, |
---|
139 | OPUS, |
---|
140 | LAST_AUDIO |
---|
141 | }; |
---|
142 | \end{cfa} |
---|
143 | & |
---|
144 | \begin{cfa} |
---|
145 | enum( int ) CFA_Codec { |
---|
146 | FIRST_VIDEO = 0, |
---|
147 | VP8 = 0, |
---|
148 | VP9, |
---|
149 | @AV1@, |
---|
150 | LAST_VIDEO, |
---|
151 | |
---|
152 | FIRST_AUDIO = 64, |
---|
153 | VORBIS = 64, |
---|
154 | OPUS, |
---|
155 | LAST_AUDIO |
---|
156 | }; |
---|
157 | \end{cfa} |
---|
158 | \end{tabular} |
---|
159 | \end{cquote} |
---|
160 | the assignments still result in @c_code@ with value 65, but @cfa_code@ is now 7. |
---|
161 | For \CFA, if all translation units including @CFA_Codec@ are not recompiled, some could assign the old 6 and some the new 7, while partially compiled C translation units all continue to assign 65. |
---|
162 | |
---|
163 | For \CFA to achieve the same behaviour for positions as C does with value for partial recompilation, enumeration positions could be represented as @const@ declarations with corresponding @extern@ declarations in the include file. |
---|
164 | \begin{cquote} |
---|
165 | \setlength{\tabcolsep}{20pt} |
---|
166 | \begin{tabular}{@{}ll@{}} |
---|
167 | \begin{cfa} |
---|
168 | const int FIRST_VIDEO_posn = 0; |
---|
169 | const int VP8_posn = 1; |
---|
170 | const int VP9_posn = 2; |
---|
171 | |
---|
172 | const int LAST_VIDEO_posn = 3; |
---|
173 | const int FIRST_AUDIO_posn = 4; |
---|
174 | const int VORBIS_posn = 5; |
---|
175 | const int OPUS_posn = 6; |
---|
176 | const int LAST_AUDIO_posn = 7; |
---|
177 | \end{cfa} |
---|
178 | &\begin{cfa} |
---|
179 | const int FIRST_VIDEO_posn = 0; |
---|
180 | const int VP8_posn = 1; |
---|
181 | const int VP9_posn = 2; |
---|
182 | const int AV1_posn = 3; |
---|
183 | const int LAST_VIDEO_posn = 4; |
---|
184 | const int FIRST_AUDIO_posn = 5; |
---|
185 | const int VORBIS_posn = 6; |
---|
186 | const int OPUS_posn = 7; |
---|
187 | const int LAST_AUDIO_posn = 8; |
---|
188 | \end{cfa} |
---|
189 | \end{tabular} |
---|
190 | \end{cquote} |
---|
191 | Then the linker always uses the most recent object file with the up-to-date positions. |
---|
192 | However, this implementation means the equivalent of a position array is generated using more storage. |
---|
193 | \end{enumerate} |
---|