soumitrachatterjee / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. Note: the repository does not accept github pull requests at this moment. Please submit your patches at http://reviews.llvm.org.
http://llvm.org
Other
0 stars 7 forks source link

Mechanism to obtain symbolic name of enum value #27

Open soumitrachatterjee opened 1 year ago

soumitrachatterjee commented 1 year ago

Modify Clang to accept the new operator __nameof to enable users to obtain the symbolic name of any enumerator value. The above operator should be converted internally by Clang to an indexing operation into the table of symbolic names generated in #26.

For example, given the following enumerator definition:

enum class Day {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
};

The user should be able to use the new operator __nameof to extract the automatically generated symbolic name of the enum values.

For example, with the above enumerator definition, the user should be able to write code as:

printf("Symbolic name of '%d' is '%s'", Day::Tuesday, __nameof(Day::Tuesday));

Since the table is an array of const char* [], the type of __nameof would be const char*. As a result, __nameof should be usable in any operation that can be done on a const char*. For example, const char* name = __nameof(Day::Tuesday);.