Next Previous Up Top Contents Index

7.10 The C structure

7.10.2 The C.Type structure

This structure provides support for representing C type information in a manipulable form as ML data.

enum_value

Type abbreviation

Specification:

type enum_value

Description:

This type is used to model enumerated values. There are conversion functions to and from integers. Equivalent to string.
tag

Type abbreviation

Specification:

type tag

Description:

This is used to provide convenient 'type' tags. Equivalent to string.
pointer_kind

Datatype

Specification:

datatype pointer_kind = LOCAL_PTR | RELATIVE_PTR | REMOTE_PTR

Description:

As described in earlier sections, machine pointers need to be treated with special care. To provide this care, the idea of a 'pointer kind' is introduced. This provides qualification of pointer values and determines how they can be used. A pointer kind is one of the following:
LOCAL_PTR

Machine address pointing within the associated store.

REMOTE_PTR

Machine address pointing within user-accessible memory.

RELATIVE_PTR

Index value accessing location within associated store.

c_type

Datatype

Specification:

datatype c_type =
    VOID_TYPE | CHAR_TYPE | ...

Description:

The ML type c_type provides a representation of C type information accessible as an ML value. These values are used to provide information on how to interpret the value parts of c_object objects used to represent foreign data.
VOID_TYPE

This represents the C type void. Its size is zero bytes.

CHAR_TYPE

This represents the C type char. Its size is 1 byte. It may be associated with either signed or unsigned chars by the particular C compiler used.

UNSIGNED_CHAR_TYPE

This represents the C type unsigned char.
SIGNED_CHAR_TYPE

This represents the C type signed char.
SHORT_TYPE

This represents the C type short int.

INT_TYPE

This represents the C type int.

LONG_TYPE

This represents the C type long int.

UNSIGNED_SHORT_TYPE

This represents the C type unsigned short int.
UNSIGNED_INT_TYPE

This represents the C type unsigned int.
UNSIGNED_LONG_TYPE

This represents the C type unsigned long int.
FLOAT_TYPE

This represents the C type float.

DOUBLE_TYPE

This represents the C type double.

LONG_DOUBLE_TYPE

This represents the C type long double.
STRING_TYPE of { length : int }

This represents C string type char* where each string has an explicit amount of storage allocated for it. This length should include room for the null byte sentinel.
TYPENAME of { name : name, defn : c_type, size : int }

This represents the use of a named type (that is, a name created with typedef) within a C type.
POINTER_TYPE of { ctype : c_type, mode : pointer_kind ref }

This represents ANSI C's idea of typed pointers. The additional pointer mode information concerns how ML encodes and treats this pointer information. In particular, 'relative' pointers are simply small indices which make indirection within a store workspace more direct and efficient. The representation also caters for 'remote' pointers which can refer to arbitrary places in memory, and 'local' pointers are remote pointers which are known to refer to places in the associated store workspace.
STRUCT_TYPE of { tag : tag option, fields : c_field list, size : int }

This represents the structured record type in C. Such types may be tagged or untagged.
UNION_TYPE of { tag : tag option, variants : c_variant list ref, size : int, current : c_variant }

This represents the union type in C. Such types may be tagged or untagged.
ARRAY_TYPE of { length : int, ctype : c_type }

This represents the array type in C.
ENUM_TYPE of { tag : tag option, elems : enum_value list, card : int }

This represents (simple) enumerated types as provided by C. Such types may be tagged or untagged.
c_variant

Datatype

Specification:

datatype c_variant = VARIANT of { name : name, ctype : c_type, size : int }

Description:

Used to encode members of C unions. Note that each variant object contains its size.
c_field

Datatype

Specification:

datatype c_field = FIELD of { name : name, ctype : c_type, size : int, padding : int, offset : int }

Description:

This is used to encode field components of C structs. In addition to its size, this representation also has the offset for the field from the start of the record (useful for indexing) and also takes account of any 'padding' required within each field. This padding depends upon the particular compiler used to compile foreign code.
sizeOf

Function

Signature:

val sizeOf : c_type -> int

Description:

This function computes the size of a c_type object and fills in any size attributes that have not already been computed. Clearly, this requires named types to have had declarations filled in -- with failure if they are not.
equalType

Function

Signature:

val equalType : c_type * c_type -> bool

Description:

Because c_type values can contain size attribute (which may be set to NONE), this function is used to make equality comparisons between two c_types which disregard the attribute components they may possess.

Some convenience functions for building compound c_type objects:

structType

Function

Signature:

val structType : string * (string * c_type) list -> c_type

Description:

Builds C struct type representations. Note that these C type objects are tagged.
unionType

Function

Signature:

val unionType : string * (string * c_type) list -> c_type

Description:

Builds C union type representations. Note that these C type objects are tagged.
ptrType

Function

Signature:

val ptrType : c_type -> c_type

Description:

Builds C pointer type representations.
typeName

Function

Signature:

val typeName : string -> c_type

Description:

Builds C named type objects.
enumType

Function

Signature:

val enumType : string * string list -> c_type

Description:

Builds C enumerated type representations


MLWorks Reference Manual version 2.0 - 29 Jul 1998

Next Previous Up Top Contents Index

Generated with Harlequin WebMaker