IBM
Contents Index Previous Next



Special Translation Rules for C Compilers


CPP2SDL by default assumes that its input is C++ code, and that the target compiler is a C++ compiler. In order to also support C code and C target compilers, the translation rules have to be slightly modified. CPP2SDL does this if the -c option is set. The tool then executes in "C mode".

A general difference is how the external specifier for all SDL declarations will be generated. Normally this specifier is followed by the string `C++' to tell the Code Generator that the declaration was translated from a C++ declaration. In C mode the string will instead be `C'.

Example 140 : Different external specifier in C mode

C++:

const int a;

SDL:

SYNONYM a int = EXTERNAL 'C';

In C, a struct or a union is not a scope unit, which means that declarations inside a struct or a union should be treated as ordinary declarations. This means that the #REFNAME directive that normally is used for specifying the qualified name of for example a nested struct declaration, will not be printed in C mode.

Instead, a #REFNAME directive will be inserted after the name of newtypes that represent tagged types (i.e. structs, unions and enums). The reason is that C, contrary to C++, does not allow such types to be referenced only with the name of the tag. Another place where the "full" type name is required in C mode is in the C style cast that is generated by means of a #REFNAME directive for IntToEnum operators (see Enumerated Types).

Example 141 : Differences in translation of structs, unions and enums

C++:

struct S {
int a;
struct SS {
int b;
};
};
typedef enum E {e1, e2, e3} Etype;

SDL:

NEWTYPE ptr_SS Ref( SS);
ENDNEWTYPE ptr_SS;EXTERNAL 'C';
NEWTYPE SS /*#REFNAME 'struct SS'*/
STRUCT
b int;
ENDNEWTYPE SS;EXTERNAL 'C';
NEWTYPE ptr_S Ref( S);
ENDNEWTYPE ptr_S;EXTERNAL 'C';
NEWTYPE S /*#REFNAME 'struct S'*/
STRUCT
a int;
ENDNEWTYPE S;EXTERNAL 'C';
NEWTYPE E /*#REFNAME 'enum E'*/
LITERALS e1, e2, e3;
OPERATORS
IntToEnum /*#REFNAME '(enum E)'*/ : int -> E;
EnumToInt : E -> int; /*#OP(PY)*/
ORDERING;
ENDNEWTYPE E;EXTERNAL 'C';
SYNTYPE Etype = E
ENDSYNTYPE Etype;EXTERNAL 'C';

Finally, note that memory allocation is done differently in C and C++. This is reflected in SDL by using a different definition of the Ref generator when CPP2SDL executes in C mode, where for example the new, delete, new_array, and delete_array operators are not present. See Dynamic Memory Management and SDL Library for Fundamental C/C++ Types for more information.


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