VIEW description


Motivation

We need a type-safe casting mechanism to avoid copying when communicating across untyped channels.

Implementation

VIEW ( expr, type )

VIEW can be used to impose a "view" of a region of memory as a different type. This view is type-safe -- it will not cause any unchecked runtime errors. VIEW only incurs runtime checks when the VIEW occurs.

The first argument, expr, must be a designator; that is, it must describe a region of memory. The result of the VIEW is a designator, and is writable if expr is writable. Following are the restrictions on the use of VIEW in more detail.

We make use the following definitions:

A VIEW would be legal whenever the type of expr --- call it T is representation-equivalent to type. Our implementation is simpler than this, and allows a VIEW under one of the following conditions:
  1. The type of expr is representation-equivalent to type. The following VIEW is legal:
    	VAR x: RECORD a, b: INTEGER := 3; END;
    	BEGIN
    	  VIEW (x, RECORD a: INTEGER; END).a := 255;
    	END;
    
    Our check for representation-equivalence is not much weaker than type equality. That is, A is representation-equivalent to B if A is equal to B, with the following weakening:

    As a result, our implementation does not support the following VIEW:

    	VAR x: RECORD a, b: INTEGER; END;
    	BEGIN
              (* this is not supported by our implementation *)
    	  VIEW (x, RECORD a: INTEGER; b: RECORD a: INTEGER; END; END).a := 255;
    	END;
    

  2. Representation-equivalence can be determined due to representation-completeness.

Examples

These examples are not ones from within SPIN, because there are too many types involved.
TYPE T = RECORD
	   a: INTEGER;
	   b: INTEGER;
           c: INTEGER;
         END;


PROCEDURE p(x: ARRAY [0..10] OF INTEGER; y: ARRAY OF INTEGER;
            z: ARRAY [0..100] OF [0..255]) =
  BEGIN
    WITH t = VIEW (x, T) DO
      (* legal, no runtime check because sizes are known statically,
         and alignments match *)
    END
    WITH t = VIEW (y, T) DO
      (* legal, a runtime size check on the open array.  alignments
         match *)
    END
    WITH t = VIEW (z, T) DO
      (* legal, a runtime alignment check, because z is only
         aligned on an 8-bit boundary *)
    END
  END p;


Other SPIN Modula-3 changes

October 7, 1996

whsieh@cs.washington.edu