Next: Pragma Restriction_Warnings, Previous: Pragma Psect_Object, Up: Implementation Defined Pragmas
pragma Pure_Function ([Entity =>] function_LOCAL_NAME);
This pragma appears in the same declarative part as a function
declaration (or a set of function declarations if more than one
overloaded declaration exists, in which case the pragma applies
to all entities). It specifies that the function Entity
is
to be considered pure for the purposes of code generation. This means
that the compiler can assume that there are no side effects, and
in particular that two calls with identical arguments produce the
same result. It also means that the function can be used in an
address clause.
Note that, quite deliberately, there are no static checks to try
to ensure that this promise is met, so Pure_Function
can be used
with functions that are conceptually pure, even if they do modify
global variables. For example, a square root function that is
instrumented to count the number of times it is called is still
conceptually pure, and can still be optimized, even though it
modifies a global variable (the count). Memo functions are another
example (where a table of previous calls is kept and consulted to
avoid re-computation).
Note: Most functions in a Pure
package are automatically pure, and
there is no need to use pragma Pure_Function
for such functions. One
exception is any function that has at least one formal of type
System.Address
or a type derived from it. Such functions are not
considered pure by default, since the compiler assumes that the
Address
parameter may be functioning as a pointer and that the
referenced data may change even if the address value does not.
Similarly, imported functions are not considered to be pure by default,
since there is no way of checking that they are in fact pure. The use
of pragma Pure_Function
for such a function will override these default
assumption, and cause the compiler to treat a designated subprogram as pure
in these cases.
Note: If pragma Pure_Function
is applied to a renamed function, it
applies to the underlying renamed function. This can be used to
disambiguate cases of overloading where some but not all functions
in a set of overloaded functions are to be designated as pure.
If pragma Pure_Function
is applied to a library level function, the
function is also considered pure from an optimization point of view, but the
unit is not a Pure unit in the categorization sense. So for example, a function
thus marked is free to with
non-pure units.