Previous Up Next

Chapter 10  Ptypedefs

Ptypedefs allow additional constraints to be added to an existing type.

10.1  Syntax

typedef_predicates ::= identifier identifier => { predicate }
typedef_ty ::= Ptypedef p_ty identifier [p_formals] [: typedef_predicates] ;

We explain the meaning of this syntax in the remainder of this chapter. All non-terminals not defined in this grammar fragment were defined previously, as follows. Predicates (predicate) are defined in Section 3.3, Pads types (p_ty) and parameter lists (p_formals) in Section 3.6.

10.1.1  Examples

Ptypedef predicates (typedef_predicates) are written over a variable bound to the in-memory representation of the base type of the Ptypedef. For example, the declaration

Ptypedef Puint32 bid_t : bid_t x => {x > 100};

defines a new type bid_t to be a Puint32 with the additional constraint that any legal bid must be greater than 100. We read this constraint “If x is a bid_t, then x must be greater than 100.” In the body of the constraint, the variable x has type bid_t, the in-memory represention type of the Ptypedef.

Like all other Pads types, Ptypedefs can be parameterized. The declaration

Ptypedef Pa_uint64_FW(:len:)
   pn_t(:Puint8 len, Puint64 hi:): pn_t x => {x <= hi};

introduces a new type pn_t. The base type for this declaration is a 64-bit unsigned integer, represented in the source as a sequence of len ASCII digits. The declaration adds the constraint that any pn_t must have a value less than hi. In general, the constraint can be any integer-valued expression.

10.2  Generated library

We will use the bid_t example to illustrate the data structures and functions generated for Ptypedefs.

10.2.1  In-memory representation

The in-memory representation of a Ptypedef is the same as the representation of the underlying base type:

typedef Puint32 bid_t;

10.2.2  Mask

The mask for a Ptypedef is a C struct containing a pair of masks. The base field stores a mask of the base mask type. This mask specifies how to handle the underlying type. The user mask allows the user to toggle behavior at the level of the Ptypedef as a whole. For example, during parsing, the base mask lets the user control constraint checking of the base type, while the user mask lets the user control checking the constraint associated with the Ptypedef itself.

typedef struct bid_t_m_s bid_t_m;
struct bid_t_m_s {
  Pbase_m base;         
/* Base mask */
  Pbase_m user;         
/* Typedef mask */
};

10.2.3  Parse descriptor

The parse descriptor for a Ptypedef is the same as the parse descriptor for the underlying base type.

typedef Pbase_pd bid_t_pd;

10.2.4  Operations

The operations generated by the Pads compiler for a Ptypedef are those described in Chapter 3. The operations appear in Figure 10.1


Perror_t bid_t_init (P_t *pads,bid_t *rep);

Perror_t bid_t_pd_init (P_t *pads,bid_t_pd *pd);

Perror_t bid_t_cleanup (P_t *pads,bid_t *rep);

Perror_t bid_t_pd_cleanup (P_t *pads,bid_t_pd *pd);

Perror_t bid_t_copy (P_t *pads,bid_t *rep_dst,bid_t *rep_src);

Perror_t bid_t_pd_copy (P_t *pads,bid_t_pd *pd_dst,bid_t_pd *pd_src);

void bid_t_m_init (P_t *pads,bid_t_m *mask,Pbase_m baseMask);

Perror_t bid_t_read (P_t *pads,bid_t_m *m,bid_t_pd *pd,bid_t *rep);

int bid_t_verify (bid_t *rep);

int bid_t_genPD (P_t *pads, bid_t *rep, bid_t_pd *pd);
Figure 10.1: Prototypes of operations generated for the Ptypedef bit_t.

Read function

The error codes for Ptypedefs are:

CodeMeaning
P_NO_ERRIndicates no error occurred
P_TYPEDEF_CONSTRAINT_ERRIndicates that the typedef constraint failed.

Accumulator functions

Accumulator functions for Ptypedefs are described in Chapter 16.

Histogram functions

Histogram functions for Ptypedefs are described in Chapter 17.

Clustering functions

Clustering functions for Ptypedefs are described in Chapter 18.


Previous Up Next