[Ada Information Clearinghouse]
Ada '83 Rationale, Sec 15.8: Unchecked Conversions

"Rationale for the Design of the
Ada® Programming Language"

[Ada '83 Rationale, HTML Version]

Copyright ©1986 owned by the United States Government. All rights reserved.
Direct inquiries to the Ada Information Clearinghouse at adainfo@sw-eng.falls-church.va.us.

CHAPTER 15: Representation Clauses and Machine Dependences

15.8 Unchecked Conversions

The conversions allowed between numeric types and between types that are derived from each other are safe conversions that do not violate the rules of type checking.

Unchecked type conversions can be achieved in any language that permits code insertions or address clauses. Such conversions may, for example, be needed if a user wants to define his own allocation strategy for access types. In this case, conversions from integer to access values are necessary to define an ALLOCATE procedure and a converse FREE procedure.

From the point of view of programming management (and also of maintainability) it is desirable to provide a standard way to achieve such unchecked conversions. In this way the parts of a program that use such dangerous features are made easier to identify. The following generic library function is predefined to that effect.

generic
  type SOURCE    is limited private;
  type TARGET    is limited private;
function UNCHECKED_CONVERSION(S  :   SOURCE) return  TARGET;

A program unit that uses unchecked type conversions must mention this generic function in its with clauses. A possible scenario is indicated with the package LIST_HANDLING given below:

package LIST_HANDLING is
  type PLACE;
  type LIST is access PLACE;

  type PLACE is
    record
      SUCC, PRED :  LIST;
      VALUE      :  INTEGER;
    end record;
  ...
  procedure ALLOCATE  (X :  out LIST);
  procedure FREE   (X :  in out LIST);
  ...
  pragma CONTROLLED(LIST);        -- no garbage collection
  for LIST'STORAGE_SIZE use 0;    -- new will not be usable
 end;

with UNCHECKED_CONVERSION;
package body LIST_HANDLING is
  function INT_TO_LIST is
    new UNCHECKED_CONVERSION(SOURCE =>  INTEGER,
                         TARGET =>  LIST);
  ...
  procedure ALLOCATE(X :  out LIST) is
    ADDRESS :  INTEGER;
  begin
    ...
    -- Compute address, Then:
    X :=  INT_TO_LIST(ADDRESS);
  end;
  ...
end LIST_HANDLING;

The function INT_TO_LIST is obtained within the body of LIST_HANDLING by instantiation of UNCHECKED_CONVERSION, and is used to convert an integer address into a LIST.

The programming environment may be able to control and restrict the programs that are allowed to get access to the function UNCHECKED_CONVERSION.


NEXTPREVIOUSUPTOCINDEX
Address any questions or comments to adainfo@sw-eng.falls-church.va.us.