IBM
Contents Index Previous Next



Integration with C Data Types


The package ctypes presented below contains a number of types and generators that is intended to directly support C data types in SDL. The package ctypes can also be used in OS integrations and with Cmicro. However, usage of C pointers (generator Ref) might cause problems, due to potential memory leaks and potential memory access protection between OS tasks.

The file ctypes.sdl is a SDL/PR version of this package suitable to use in an include statement in an SDL/PR system, while ctypes.sun is a SDL/GR version of the package.

In an SDL/GR system it is only necessary to insert a use clause, i.e.

use ctypes;

at a proper place. The Organizer will then by itself include the ctypes package, for example when the system is to be analyzed. To use the ctypes package in an SDL/PR system the following structure should be used.

/*#include `ctypes.sdl'*/

use ctypes;
system example;
  ...
endsystem;

The ctypes package consists of the following newtypes, syntypes, and generators:

SDL C
syntype ShortInt

short int, short

syntype LongInt

long int, long

syntype UnsignedShortInt

unsigned short int, 
unsigned short

syntype UnsignedInt

unsigned int, unsigned

syntype UnsignedLongInt

unsigned long int, 
unsigned long

syntype Float

float

newtype Charstar

char *

newtype Voidstar

void *

newtype Voidstarstar

void **

generator Carray

array type

generator Ref

pointer type

All the newtypes and syntypes introduce type names for "standard types" in C.

Some of the types and generators are briefly described below.

Charstar

In Charstar there is a literal and some operators included:

LITERALS
  Null;
OPERATORS
  cstar2cstring : Charstar   -> Charstring;
  cstring2cstar : Charstring -> Charstar;
  cstar2vstar   : Charstar   -> Voidstar;
  vstar2cstar   : Voidstar   -> Charstar;
  cstar2vstarstar : Charstar -> Voidstarstar;

Note that the operators cstar2cstring and cstring2cstar are not available when using Cmicro.

The operators are all conversion routines to convert a value from one type to another. Note that Charstar and Charstring are not the same types even if they both corresponds to char * in C. Note also that freeing allocated memory for Charstar is the responsibility of the user, as there is not enough information to handle this automatically (as for Charstring). For more information about how to free memory, see the Ref generator below.

Voidstarstar

The Voidstarstar type has all the properties of the Ref generator (see below). This means that *>, &, +, and - can be used and that the following literal and operators are defined:

LITERALS
  Null,
  Alloc;
OPERATORS
  vstarstar2vstar : Voidstarstar -> Voidstar;
  vstar2vstarstar : Voidstar -> Voidstarstar;

Carray

The generator Carray has the following parameters:

GENERATOR Carray (CONSTANT Length, TYPE Itemsort)

where Length is an integer giving the number of elements of the array (index from 0 to Length-1), and Itemsort gives the type of each element in the array. A Carray in SDL is translated to an array in C. Indexing a Carray variable in SDL follows the same rules as for ordinary SDL Arrays.

Ref

The generator Ref has the following definition:

GENERATOR Ref (TYPE Itemsort)
  LITERALS
    Null,
    Alloc;
  OPERATORS
    "*>"      : Ref, Itemsort -> Ref;
    "*>"      : Ref -> Itemsort;
    "&"       : Itemsort -> Ref;
    make!     : Itemsort -> Ref;
    free      : in/out Ref;
    "+"       : Ref, Integer -> Ref;
    "-"       : Ref, Integer -> Ref;
    Ref2VStar : Ref -> Voidstar;
    VStar2Ref : Voidstar -> Ref;
    Ref2VStarStar : Ref /*#REF*/ -> Voidstarstar;
  DEFAULT Null;
ENDGENERATOR Ref;

procedure Free; fpar p Voidstarstar;
external;

Instantiating the Ref generator creates a pointer type on the type given as generator parameter. The literals and operators have the following behavior:

The SDL Analyzer can allow implicit type conversion of pointer data types created by the Ref generator; see Implicit Type Conversions.


http://www.ibm.com/rational
Contents Index Previous Next