17.2 xlate D Operator

The xlate D operator is used to perform a translation from an input expression to one of the defined translation output structures. The xlate operator is used in an expression of the following form:

xlate <output-type> ( input-expression )

For example, to invoke the hypothetical translator for FILE structs that are defined previously and access the file_fd member, you would write the expression as follows:

xlate <struct file_info *>(f)->file_fd;

where f is a D variable of type FILE *. The xlate expression itself is assigned the type that is defined by the output-type. When a translator is defined, it can be used to translate input expressions to either the translator output struct type or to a pointer to that struct.

If you translate an input expression to a struct, you can either dereference a particular member of the output immediately by using the “.” operator, or you can assign the entire translated struct to another D variable to make a copy of the values of all the members. If you dereference a single member, the D compiler only generates code that corresponds to the expression for that member. You may not apply the & operator to a translated struct to obtain its address, as the data object itself does not exist until it is copied or one of its members is referenced.

If you translate an input expression to a pointer to a struct, you can either dereference a particular member of the output immediately by using the -> operator, or you can dereference the pointer by using the unary * operator. In the latter case, the result behaves as though you translated the expression to a struct. If you dereference a single member, the D compiler only generates code corresponding to the expression for that member. You may not assign a translated pointer to another D variable, as the data object does not exist until it is copied or one of its members is referenced, and therefore cannot be addressed.

A translator declaration may omit expressions for one or more members of the output type. If an xlate expression is used to access a member for which no translation expression is defined, the D compiler produces an appropriate error message and aborts the program compilation. If the entire output type is copied by means of a structure assignment, any members for which no translation expressions are defined are filled with zeroes.

To find a matching translator for an xlate operation, the D compiler examines the set of available translators in the following order:

  • The compiler checks for a translation from the exact input expression type to the exact output type.

  • The compiler resolves the input and output types by following any typedef aliases to the underlying type names, and then checks for a translation from the resolved input type to the resolved output type.

  • The compiler checks for a translation from a compatible input type to the resolved output type. The compiler uses the same rules as those used for determining compatibility of function call arguments with function prototypes in order to determine if an input expression type is compatible with a translator's input type.

If no matching translator can be found according to these rules, the D compiler produces an appropriate error message and the program compilation fails.