Go to the first, previous, next, last section, table of contents.


5.4.5 Union Declarations

The sorts for union types are defined in the same way as for class and struct types. That is, if the user explicitly uses a trait that defines a sort with the same name as the union (and gives a definition of the contained_objects trait function that takes that sort), then Larch/C++ does not automatically construct a trait for the union type's abstract values. However, as with struct and class types, the user may choose to provide such a trait. See section 11.11 Union Types for details on the automatically-constructed traits for unions.

With the following type definition

union U {int i_var; char *char_p;};

consider the following table.

Declaration      Name   Its Sort (when used as a global variable)
-------------    ----   -----------------------------------------
U x;             x      Obj[U]

The sort of x, when used as a global variable, is Obj[U], which means that x is a variable that holds a U value. The sort of x' is U, which is a (LSL) union of two locations that share storage. Assuming the automatically-constructed trait for this type (see section 11.11 Union Types), the tag i_var can be used to refer to an object of sort Obj[int], and the tag char_p denotes an object of sort Obj[Ptr[Obj[char]]]. That is, the term x'.char_p has sort Obj[Ptr[Obj[char]]] and x'.i_var has sort Obj[int].

When a union variable, such as x above, is declared Larch/C++ implicitly uses an appropriate instantiation of the trait MutableObj. In this example, the included trait is: MutableObj(U) (see section 2.8.1 Formal Model of Objects).

See section 11.11 Union Types for details on how the tags of a union are used to refer to the different parts of the union value in the automatically constructed trait for a union.


Go to the first, previous, next, last section, table of contents.