source: doc/theses/jiada_liang_MMath/conclusion.tex @ dcfcf368

Last change on this file since dcfcf368 was aa14aafe, checked in by JiadaL <j82liang@…>, 5 weeks ago

Update thesis

  • Property mode set to 100644
File size: 8.0 KB
Line 
1\chapter{Conclusion}
2\label{c:conclusion}
3
4This 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.
5Within this goal, the new \CFA enumeration should align with the analogous enumeration features in other languages to match modern programming expectations.
6Hence, 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
8Strong type-checking of enumeration initialization and assignment provides additional safety, ensuring an enumeration only contains its enumerators.
9Overloading 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.
10Typed enumerations solve the data-harmonization problem, increasing safety through better software engineering.
11Moreover, 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.
12Generalization and reuse are supported by incorporating the new enumeration type using the \CFA trait system.
13Enumeration 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.
14With 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.
15Finally, 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.
16These tests ensure future \CFA work does not accidentally break the new enumeration system.
17
18In 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
23The following are ideas to improve and extend the work in this thesis.
24\begin{enumerate}
25\item
26There are still corner cases being found in the current \CFA enumeration implementation.
27Fixing some of these corner cases requires changes to the \CFA resolver or extensions to \CFA. %, like compile-time constant-expression evaluation.
28When these changes are made, it should be straightforward to update the \CFA enumeration implementation to work with them.
29\item
30Currently, some aspects of the enumeration trait system require explicitly including the file @enum.hfa@, which can lead to problems.
31It should be possible to have this file included implicitly by updating the \CFA prelude.
32\item
33There are multiple \CFA features being developed in parallel with enumerations.
34Two closely related features are iterator and namespace.
35Enumerations may have to be modified to dovetail with these features.
36For example, enumerating with range loops does not align with the current iterator design, so some changes will be necessary.
37\item
38C 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).
39Given the existence of this form, it is conceivable to extend it with types other than @int@.
40\begin{cfa}
41enum { Size = 20u, PI = 3.14159L, Jack = L"John" };
42\end{cfa}
43which matches with @const@ aliasing in other programming languages.
44Here, the type of the enumerator is the type of the initialization constant, \eg @typeof( 20u )@ for @Size@ implies @unsigned int@.
45Auto-initialization is restricted to the case where all constants are @int@, matching with C.
46As seen in \VRef{s:EnumeratorTyping}, this feature is just a shorthand for multiple typed-enumeration declarations.
47\begin{cfa}
48enum( unsigned int ) { Size = 20u };
49enum( long double ) { PI = 3.14159L };
50enum( wchar_t * ) { Jack = L"John" };
51\end{cfa}
52\item
53Currently, enumeration scoping is all or nothing. In some cases, it might be useful to
54increase the scoping granularity to individual enumerators.
55\begin{cfa}
56enum E1 { @!@A, @^@B, C };
57enum E2 @!@ { @!@A, @^@B, C };
58\end{cfa}
59Here, @'!'@ means the enumerator is scoped, and @'^'@ means the enumerator is unscoped.
60For @E1@, @A@ is scoped; @B@ and @C@ are unscoped.
61For @E2@, @A@, and @C@ are scoped; @B@ is unscoped.
62Finding a use case is important to justify this extension.
63\item
64An extension mentioned in \VRef{s:Ada} is using @typedef@ to create an enumerator alias.
65\begin{cfa}
66enum(int) RGB { @Red@, @Green@, Blue };
67enum(int) Traffic_Light { @Red@, Yellow, @Green@ };
68typedef RGB.Red OtherRed; // alias
69\end{cfa}
70\item
71Label arrays are auxiliary data structures that are always generated for \CFA enumeration, which is a considerable program overhead.
72It is helpful to provide a new syntax or annotation for a \CFA enumeration definition that tells the compiler the labels will not be used
73throughout the execution. Therefore, \CFA optimizes the label array away. The @label()@ function can still be used on an enumeration constant,
74and the function called is reduced to a @char *@ constant expression that holds the name of the enumerator. But if @label()@ is called on
75a variable with an enumerated type, it returns an empty string since the label information is lost for the runtime.
76\item
77\CFA enumeration has its limitation in separate compilation. Consider the followings example:
78\begin{cfa}
79// libvacodec.h: v1
80enum(int) C_Codec ! {
81    FIRST_VIDEO = 0,
82    VP8 = 2,
83    VP9 = 4,
84    LAST_VIDEO = 8,
85
86    FIRST_AUDIO = 3,
87    VORBIS = 9,
88    OPUS = 27,
89    LAST_AUDIO = 81
90};
91
92enum(int) CFA_Codec ! {
93    FIRST_VIDEO = 0,
94    VP8 = 2,
95    VP9 = 4,
96    LAST_VIDEO = 8,
97
98    FIRST_AUDIO = 3,
99    VORBIS = 9,
100    OPUS = 27,
101    LAST_AUDIO = 81
102};
103
104// main.c
105C_Codec c_code = C_Codec.VORBIS;
106CFA_Codec cfa_code = CFA_Codec.VORBIS;
107\end{cfa}
108The memory location of @c_code@ stores a value 9, the integral value of VORBIS.
109The memory of @cfa_code@ stores 5, which is the position of @CFA_Codec.VORBIS@ and can be mapped to value 9.
110
111If to insert a new enumerator in both @C_Codec@ and @CFA_Codec@
112before the relative position of VORBIS, without a re-compilation of @main.c@, variable @cfa_code@ would be mapped
113to an unintented value.
114\begin{cfa}
115// libvacodec.h: v2
116enum(int) C_Codec ! {
117    FIRST_VIDEO = 0,
118    VP8 = 2,
119    VP9 = 4,
120    @AV1@,
121    LAST_VIDEO = 8,
122
123    FIRST_AUDIO = 3,
124    VORBIS = 9,
125    OPUS = 27,
126    LAST_AUDIO = 81
127};
128
129enum(int) CFA_Codec ! {
130    FIRST_VIDEO = 0,
131    VP8 = 2,
132    VP9 = 4,
133    @AV1@,
134    LAST_VIDEO = 8,
135
136    FIRST_AUDIO = 3,
137    VORBIS = 9,
138    OPUS = 27,
139    LAST_AUDIO = 81
140};
141
142\end{cfa}
143While @c_code@ still has the correct value of 9, @cfa_code@ (5) is now mapped to @First_AUDIO@. In other words, it is
144necessary to to recompile @main.c@ when to update @libvacodec.h@.
145
146To avoid a recompilation, enumeration positions can be represented as @extern const@. For example, \CFA-cc can generate the following code for @CFA_Codec@:
147\begin{cfa}
148// CFA-cc transpile: libvacodec.h
149const int FIRST_VIDEO = 0;
150const int VP8 = 1;
151const int VP9 = 2;
152const int LAST_VIDEO = 3;
153const int FIRST_AUDIO = 4;
154const int VORBIS = 5;
155const int OPUS = 6;
156const int LAST_AUDIO = 7;
157
158// CFA-cc transpile: main.c
159extern const int FIRST_VIDEO;
160extern const int VP8;
161extern const int VP9;
162extern const int LAST_VIDEO;
163extern const int FIRST_AUDIO;
164extern const int VORBIS;
165extern const int OPUS;
166extern const int LAST_AUDIO;
167
168int cfa_code = VORBIS;
169\end{cfa}
170If later a position of an enumerator is changed, the generated expression @int cfa_code = VORBIS;@ will still have the correct
171value, because it is linked to a external constant @VORBIS@ from @libvacodec.h@. This implementation requires extra memory to store position, but
172solve the separate compilation problem.
173
174\end{enumerate}
Note: See TracBrowser for help on using the repository browser.