This is an archive of the discontinued LLVM Phabricator instance.

[ADT] Alternative way to declare enum type as bitmask
ClosedPublic

Authored by sepavloff on Feb 16 2023, 10:03 AM.

Details

Summary

If an enumeration represents set of bit flags, using the macro
LLVM_MARK_AS_BITMASK_ENUM can make operations with such enumeration more
convenient. It however brings problems if the enumeration is non-scoped.
As the macro adds an item LLVM_BITMASK_LARGEST_ENUMERATOR to the
enumeration type, only one such type may be declared as bitmask. This
problem could be solved by convertion of the enumeration to scoped, but
it requires static_casts in new places and the convenience can be
eliminated.

This change introduces a new macro LLVM_DECLARE_ENUM_AS_BITMASK, which
allows non-invasive convertion of an enumeration into bitmask. It
provides specialization to trait classes, which previously were built
based on presence of LLVM_BITMASK_LARGEST_ENUMERATOR in the enumeration.
The macro must be specified in global or llvm namespace because the
trait classes are declared in llvm namespace.

Diff Detail

Event Timeline

sepavloff created this revision.Feb 16 2023, 10:03 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 16 2023, 10:03 AM
sepavloff requested review of this revision.Feb 16 2023, 10:03 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 16 2023, 10:03 AM
arsenm accepted this revision.Feb 17 2023, 3:07 AM
arsenm added a subscriber: arsenm.

LGTM. I reproduced the same thing, except I was also trying to eliminate the existing LLVM_MARK_AS_BITMASK_ENUM method which was the tricky part

This revision is now accepted and ready to land.Feb 17 2023, 3:07 AM
This revision was landed with ongoing or failed builds.Feb 20 2023, 10:45 PM
This revision was automatically updated to reflect the committed changes.