8. Intrinsic Subprograms

GNAT allows a user application program to write the declaration:

pragma Import (Intrinsic, name);

providing that the name corresponds to one of the implemented intrinsic subprograms in GNAT, and that the parameter profile of the referenced subprogram meets the requirements. This chapter describes the set of implemented intrinsic subprograms, and the requirements on parameter profiles. Note that no body is supplied; as with other uses of pragma Import, the body is supplied elsewhere (in this case by the compiler itself). Note that any use of this feature is potentially non-portable, since the Ada standard does not require Ada compilers to implement this feature.

8.1. Intrinsic Operators

All the predefined numeric operators in package Standard in pragma Import (Intrinsic,..) declarations. In the binary operator case, the operands must have the same size. The operand or operands must also be appropriate for the operator. For example, for addition, the operands must both be floating-point or both be fixed-point, and the right operand for "**" must have a root type of Standard.Integer'Base. You can use an intrinsic operator declaration as in the following example:

type Int1 is new Integer;
type Int2 is new Integer;

function "+" (X1 : Int1; X2 : Int2) return Int1;
function "+" (X1 : Int1; X2 : Int2) return Int2;
pragma Import (Intrinsic, "+");

This declaration would permit ‘mixed mode’ arithmetic on items of the differing types Int1 and Int2. It is also possible to specify such operators for private types, if the full views are appropriate arithmetic types.

8.2. Compilation_ISO_Date

This intrinsic subprogram is used in the implementation of the library package GNAT.Source_Info. The only useful use of the intrinsic import in this case is the one in this unit, so an application program should simply call the function GNAT.Source_Info.Compilation_ISO_Date to obtain the date of the current compilation (in local time format YYYY-MM-DD).

8.3. Compilation_Date

Same as Compilation_ISO_Date, except the string is in the form MMM DD YYYY.

8.4. Compilation_Time

This intrinsic subprogram is used in the implementation of the library package GNAT.Source_Info. The only useful use of the intrinsic import in this case is the one in this unit, so an application program should simply call the function GNAT.Source_Info.Compilation_Time to obtain the time of the current compilation (in local time format HH:MM:SS).

8.5. Enclosing_Entity

This intrinsic subprogram is used in the implementation of the library package GNAT.Source_Info. The only useful use of the intrinsic import in this case is the one in this unit, so an application program should simply call the function GNAT.Source_Info.Enclosing_Entity to obtain the name of the current subprogram, package, task, entry, or protected subprogram.

8.6. Exception_Information

This intrinsic subprogram is used in the implementation of the library package GNAT.Current_Exception. The only useful use of the intrinsic import in this case is the one in this unit, so an application program should simply call the function GNAT.Current_Exception.Exception_Information to obtain the exception information associated with the current exception.

8.7. Exception_Message

This intrinsic subprogram is used in the implementation of the library package GNAT.Current_Exception. The only useful use of the intrinsic import in this case is the one in this unit, so an application program should simply call the function GNAT.Current_Exception.Exception_Message to obtain the message associated with the current exception.

8.8. Exception_Name

This intrinsic subprogram is used in the implementation of the library package GNAT.Current_Exception. The only useful use of the intrinsic import in this case is the one in this unit, so an application program should simply call the function GNAT.Current_Exception.Exception_Name to obtain the name of the current exception.

8.9. File

This intrinsic subprogram is used in the implementation of the library package GNAT.Source_Info. The only useful use of the intrinsic import in this case is the one in this unit, so an application program should simply call the function GNAT.Source_Info.File to obtain the name of the current file.

8.10. Line

This intrinsic subprogram is used in the implementation of the library package GNAT.Source_Info. The only useful use of the intrinsic import in this case is the one in this unit, so an application program should simply call the function GNAT.Source_Info.Line to obtain the number of the current source line.

8.11. Shifts and Rotates

In standard Ada, the shift and rotate functions are available only for the predefined modular types in package Interfaces. However, in GNAT it is possible to define these functions for any integer type (signed or modular), as in this example:

function Shift_Left
  (Value  : T;
   Amount : Natural) return T
with Import, Convention => Intrinsic;

The function name must be one of Shift_Left, Shift_Right, Shift_Right_Arithmetic, Rotate_Left, or Rotate_Right. T must be an integer type. T’Size must be 8, 16, 32 or 64 bits; if T is modular, the modulus must be 2**8, 2**16, 2**32 or 2**64. The result type must be the same as the type of Value. The shift amount must be Natural. The formal parameter names can be anything.

A more convenient way of providing these shift operators is to use the Provide_Shift_Operators pragma, which provides the function declarations and corresponding pragma Import’s for all five shift functions. For signed types the semantics of these operators is to interpret the bitwise result of the corresponding operator for modular type. In particular, shifting a negative number may change its sign bit to positive.

8.12. Source_Location

This intrinsic subprogram is used in the implementation of the library routine GNAT.Source_Info. The only useful use of the intrinsic import in this case is the one in this unit, so an application program should simply call the function GNAT.Source_Info.Source_Location to obtain the current source file location.