Previous Section Next Section

4.2 Extending Structs

Struct extensions add struct members to a previously defined struct. These extensions can be specified in the same file or a different file. Moreover, it is possible to apply many extensions to a struct. Each extension adds a layer of struct members to the original struct definition. The syntax definition for extending a struct is as follows:

extend [struct-subtype] base-struct-type {
            [struct-member][;struct-member] ...};

An extension to a struct can have all components that are contained in a struct definition. The components that can be defined in an extension of the struct are explained in further detail in Table 4-2 below.

Table 4-2. Components of a Struct Extension

base-struct-type

Indicates the base struct type to extend.

member; …

Denotes the contents of the struct. Any legal struct member is allowed in a struct extension.

struct-subtype

Adds struct members to the specified subtype of the base struct type only. The added struct members are known only in that subtype, not in other subtypes. This will be discussed later.

Example 4-2 shows the extension of a struct definition in the same file. Note that sys is a predefined struct in e. Therefore, sys can only be extended. All user-defined structs must be instantiated below sys.

Example 4-2 Extension of a Struct Definition in the Same File
File name: packet.e
-------------------
<'
type packet_kind: [ATM, ETH]; // Enumerated type

//Definition packet struct
struct packet {
    len: int; //Field of struct
    keep len < 256; //Constraint on struct
    kind: packet_kind; //Field of struct
    calc_par() is { //Method (procedure) in a struct
    --
     }; //end of method definition
}; //end of packet struct

<'
//Extension in the same file
//Extend the sys struct and instantiate a list
//of packets in sys. Initially, sys is empty.
//Extensions add struct members and instantiations
//to sys
extend sys {
    packets: list of packet; 
    keep packets.size() == 10;
    run() is also {
        print packets;
    };
};

//Extension of packet definition in the
//same file
extend packet {
    addr : byte; //Add 8-bit address field to definition
    keep len < 128; //Extend original packet definition
};

'>

A struct can also be extended in a separate file as shown in Example 4-3. The original struct definition must be loaded before the extension file is loaded.

Example 4-3 Extension of a Struct Definition in a Different File
File name: packet_extension.e
-----------------------------
<'
//Import the original packet definition
import packet.e;

//Extension of packet struct in a separate file
extend packet {
    keep kind == ATM; //Add constraint to packet definition
                      //to keep all packets of type ATM
    keep len == 64;   //Add constraint to packet definition
                      //to keep length exactly 64.
};

'>

The advantage of using the extend statement is that the original definition of the struct does not need to be modified or edited. This makes it very easy to quickly create a basic verification environment and then enhance it incrementally. Figure 4-1 shows a representation of how an extend statement adds code incrementally to the definition of the packet struct. Specman Elite gathers the original struct definition and all extensions to the definition to create a description of each struct.

Figure 4-1. Extending the Packet Struct

graphics/04fig01.gif

The following sections discuss the fields of a struct definition in greater detail.

Previous Section Next Section