In Ada, you declare a new enumeration type by providing a list of literals:
type Position is ( Left, Middle, Right );
Ada also lets you use character literals in an enumeration:
type Calc_Op is ( '*', '/', '+', '-' );
type DNACode is ( 'A', 'C', 'G', 'T', Unknown );
Once you have an enumeration type, you can use these literals just as you would other literals:
op: Calc_Op := '*'; ... case op is when '*' => ...
In Ada, each literal is technically the name of a function taking no arguments and returning the corresponding value. However, these functions are not generated in the object code unless they are actually needed. When such a function is needed, the compiler generates additional object code that does not correspond directly to the source code. For example, the
"return" of the generated function does not correspond to a
"return" statement in the source text. At DO-178B and DO-178C level A, it is important to analyse cases where the compiler introduces additional object code in this way (DO-178B 184.108.40.206(b)). Here are two straightforward cases that will cause this to happen.
The most direct way this happens is when you create a function that renames a literal:
function make_times return Calc_Op renames '*';
This causes the GNAT expander (we use gnatmake options
-gnatDGL for this) to emit this definition:
function lee__make_times return lee__calc_op is begin return '*'; end lee__make_times;
If the renamed function is not called, later optimisation stages remove it. Otherwise, you get object code as follows (on a standard host platform):
00401510 <_lee__make_times.2282>: 401510: 55 push %ebp 401511: 89 e5 mov %esp,%ebp 401513: 83 ec 18 sub $0x18,%esp 401516: 89 4d f4 mov %ecx,-0xc(%ebp) 401519: b8 00 00 00 00 mov $0x0,%eax 40151e: c9 leave 40151f: c3 ret
Note here that the internal representation of
'*' is not its ASCII code; it's allocated as per any other enumeration, starting from 0. Ada lets you take an access to such a function, but does not allow an access directly to the corresponding literal.
If a parameter to a generic is a function of no arguments returning an enumeration value, a literal may be supplied directly without any need for renaming. In this example, the user can instantiate an evaluate function based on a
generic with function get_op return Calc_Op; function evaluate( L, R: Integer ) return Integer; function evaluate( L, R: Integer ) return Integer is begin case get_op is when '*' => return L * R; when '/' => return L / R; when '+' => return L + R; when '-' => return L - R; end case; end evaluate; function multiply is new evaluate( '*' );
In the expanded code, among the other package creation and expansion, GNAT provides a function for
'*' . In the start of the new instance of evaluate, it creates a call to
function lee__multiply (l : integer; r : integer) return integer is begin R31b : constant lee__calc_op := lee__multiplyGP644__get_op; ...
and then later on, it creates a body for
function lee__multiplyGP644__get_op return lee__calc_op is begin return '*'; end lee__multiplyGP644__get_op;
This makes some kind of sense: from the point of view of the code within the generic, whatever is supplied for
get_op must behave exactly like a function, including taking an access to that function. The same thing happens with default values. Here is the same code with a default for
generic with function get_op return Calc_Op is '+'; function evaluate( L, R: Integer ) return Integer;
Instantiating this without providing anything for
get_op leads to a new function in the expander:
function multiply2 is new evaluate; function lee__multiply2GP1096__get_op return lee__calc_op is begin return '+'; end lee__multiply2GP1096__get_op;
Function coverage and literals
If using this feature of the Ada language, we recommend analysing the generated object code on your platform to investigate how these implicit functions are generated. The intermediate view from the expander is helpful in explaining the intent of the additional object code. DO-178B and DO-178C structural coverage is assessed at the source level. You need to decide whether you want to treat functions that rename literals - directly or via generic formal functions - as source-level functions for which coverage should be demonstrated, or as compiler-added object code for which additional review is needed. Or, as with a lot of convenient features, impose a coding standard that forbids the use of these features in your critical code.