TIP 642: Let TK_OPTION_BOOL/TK_OPTION_STRING_TABLE handle (C99) bool/enum.

Bounty program for improvements to Tcl and certain Tcl packages.
Author:         Jan Nijtmans <[email protected]>
State:          Final
Type:           Project
Vote:           Done
Created:        05-Oct-2022
Tcl-Version:    8.7
Tk-Branch:      tip-642
Vote-Summary:  Accepted 3/0/0
Votes-For:     JN, KBK SL
Votes-Against: none
Votes-Present: none


This TIP proposes new flags for Tk's configuration options TK_OPTION_BOOL/TK_OPTION_STRING_TABLE.

The Tk_OptionSpec.internalOffset field describes the offset of the variable being handled in the record. This variable has to be of type int. This TIP expands this, such that the variable pointed to by this internalOffset also can be of another type, as long as sizeof(var) is 1, 2 or 4. For example (C99) bool (useful for TK_OPTION_BOOLEAN) and enum (useful for TK_OPTION_STRING_TABLE) can now be used, even when sizeof(bool) resp. sizeof(enum) != sizeof(int).


Two new macro's are defined:

    #define TK_OPTION_VAR(type)		...
    #define TK_OPTION_ENUM_VAR		...

Those macro's can be used in the Tk_OptionSpec.flags field, indicating the size of the variable internalOffset points to. It can be combined with any other flag, using the | operator.

For example:

This will result in the value 64 if sizeof(bool) == 1, or 0 if sizeof(bool) == sizeof(int).


This will result in the value 64 if sizeof(enum) == 1, 128 if sizeof(enum) == 2, or 0 if sizeof(enum) == sizeof(int).

Those 2 flag values (64 and 128) are used in the option handling, indicating that the variable pointed to by internalOffset is not an int, but a smaller type. Those flag values don't conflict with any other flag value which can be used here.

The TK_OPTION_ENUM_VAR macro is already implemented, in tkInt.h, as part of bugfix eedd795d98: It turned out that in various places, enum variables were already used in Tk, which is not portable. This TIP moves TK_OPTION_ENUM_VAR to the public header-file tk.h, so it can officially be used in extensions as well.

Gcc has a compilation option -fshort-enums, which makes sizeof(enum) == sizeof(short). Using this option, and without bug-fix eedd795d98, the problem described in the bug-fix ticket can be demonstrated. Some (mostly embedded) targets use -fshort-enums as default.


This is 100% upwards compatible.

Reference Implementation

Under development on the tip-642 branch.


This document has been placed in the public domain.