Skip to content

Commit f10467d

Browse files
committed
VulkanHeaders generator: use sized enums
1 parent a80ab6d commit f10467d

File tree

1 file changed

+8
-112
lines changed

1 file changed

+8
-112
lines changed

libs/VulkanHeaders/generator.py

Lines changed: 8 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -623,117 +623,14 @@ def buildEnumCDecl(self, expand, groupinfo, groupName):
623623
self.logMsg('error', 'Invalid value for bitwidth attribute (', groupElem.get('bitwidth'), ') for ', groupName, ' - must be an integer value\n')
624624
exit(1)
625625

626-
usebitmask = False
627-
usedefine = False
628-
629-
# Bitmask flags can be generated as either "static const uint{32,64}_t" values,
630-
# or as 32-bit C enums. 64-bit types must use uint64_t values.
631-
if groupElem.get('type') == 'bitmask':
632-
if bitwidth > 32 or self.misracppstyle():
633-
usebitmask = True
634-
if self.misracstyle():
635-
usedefine = True
636-
637-
if usedefine or usebitmask:
638-
# Validate the bitwidth and generate values appropriately
639-
if bitwidth > 64:
640-
self.logMsg('error', 'Invalid value for bitwidth attribute (', groupElem.get('bitwidth'), ') for bitmask type ', groupName, ' - must be less than or equal to 64\n')
641-
exit(1)
642-
else:
643-
return self.buildEnumCDecl_BitmaskOrDefine(groupinfo, groupName, bitwidth, usedefine)
644-
else:
645-
# Validate the bitwidth and generate values appropriately
646-
if bitwidth > 32:
647-
self.logMsg('error', 'Invalid value for bitwidth attribute (', groupElem.get('bitwidth'), ') for enum type ', groupName, ' - must be less than or equal to 32\n')
648-
exit(1)
649-
else:
650-
return self.buildEnumCDecl_Enum(expand, groupinfo, groupName)
651-
652-
def buildEnumCDecl_BitmaskOrDefine(self, groupinfo, groupName, bitwidth, usedefine):
653-
"""Generate the C declaration for an "enum" that is actually a
654-
set of flag bits"""
655-
groupElem = groupinfo.elem
656-
flagTypeName = groupElem.get('name')
657-
658-
# Prefix
659-
body = f"// Flag bits for {flagTypeName}\n"
660-
661-
if bitwidth == 64:
662-
body += f"typedef VkFlags64 {flagTypeName};\n";
626+
# Validate the bitwidth and generate values appropriately
627+
if bitwidth > 64:
628+
self.logMsg('error', 'Invalid value for bitwidth attribute (', groupElem.get('bitwidth'), ') for enum type ', groupName, ' - must be less than or equal to 64\n')
629+
exit(1)
663630
else:
664-
body += f"typedef VkFlags {flagTypeName};\n";
665-
666-
# Maximum allowable value for a flag (unsigned 64-bit integer)
667-
maxValidValue = 2**(64) - 1
668-
minValidValue = 0
669-
670-
# Get a list of nested 'enum' tags.
671-
enums = groupElem.findall('enum')
672-
673-
# Check for and report duplicates, and return a list with them
674-
# removed.
675-
enums = self.checkDuplicateEnums(enums)
676-
677-
# Accumulate non-numeric enumerant values separately and append
678-
# them following the numeric values, to allow for aliases.
679-
# NOTE: this does not do a topological sort yet, so aliases of
680-
# aliases can still get in the wrong order.
681-
aliasText = ''
682-
683-
# Loop over the nested 'enum' tags.
684-
for elem in enums:
685-
# Convert the value to an integer and use that to track min/max.
686-
# Values of form -(number) are accepted but nothing more complex.
687-
# Should catch exceptions here for more complex constructs. Not yet.
688-
(numVal, strVal) = self.enumToValue(elem, True, bitwidth, True)
689-
name = elem.get('name')
690-
691-
# Range check for the enum value
692-
if numVal is not None and (numVal > maxValidValue or numVal < minValidValue):
693-
self.logMsg('error', 'Allowable range for flag types in C is [', minValidValue, ',', maxValidValue, '], but', name, 'flag has a value outside of this (', strVal, ')\n')
694-
exit(1)
695-
696-
decl = self.genRequirements(name, mustBeFound = False)
697-
698-
if self.isEnumRequired(elem):
699-
protect = elem.get('protect')
700-
if protect is not None:
701-
body += f'#ifdef {protect}\n'
702-
703-
body += self.deprecationComment(elem, indent = 0)
704-
705-
if usedefine:
706-
decl += f"#define {name} {strVal}\n"
707-
elif self.misracppstyle():
708-
decl += f"static constexpr {flagTypeName} {name} {{{strVal}}};\n"
709-
else:
710-
# Some C compilers only allow initializing a 'static const' variable with a literal value.
711-
# So initializing an alias from another 'static const' value would fail to compile.
712-
# Work around this by chasing the aliases to get the actual value.
713-
while numVal is None:
714-
alias = self.registry.tree.find(f"enums/enum[@name='{strVal}']")
715-
if alias is not None:
716-
(numVal, strVal) = self.enumToValue(alias, True, bitwidth, True)
717-
else:
718-
self.logMsg('error', f'No such alias {strVal} for enum {name}')
719-
decl += f"static const {flagTypeName} {name} = {strVal};\n"
720-
721-
if numVal is not None:
722-
body += decl
723-
else:
724-
aliasText += decl
725-
726-
if protect is not None:
727-
body += '#endif\n'
728-
729-
# Now append the non-numeric enumerant values
730-
body += aliasText
731-
732-
# Postfix
733-
734-
return ("bitmask", body)
631+
return self.buildEnumCDecl_Enum(expand, groupinfo, groupName, bitwidth)
735632

736-
def buildEnumCDecl_Enum(self, expand, groupinfo, groupName):
633+
def buildEnumCDecl_Enum(self, expand, groupinfo, groupName, bitwidth):
737634
"""Generate the C declaration for an enumerated type"""
738635
groupElem = groupinfo.elem
739636

@@ -749,13 +646,12 @@ def buildEnumCDecl_Enum(self, expand, groupinfo, groupName):
749646
expandPrefix = expandName.rsplit(expandSuffix, 1)[0]
750647

751648
# Prefix
752-
body = ["typedef enum %s {" % groupName]
649+
body = ["typedef enum %s : %s {" % ( groupName, "uint32_t" if bitwidth == 32 else "uint64_t" )]
753650

754651
# @@ Should use the type="bitmask" attribute instead
755652
isEnum = ('FLAG_BITS' not in expandPrefix)
756653

757-
# Allowable range for a C enum - which is that of a signed 32-bit integer
758-
maxValidValue = 2**(32 - 1) - 1
654+
maxValidValue = 2**(bitwidth - 1) - 1
759655
minValidValue = (maxValidValue * -1) - 1
760656

761657
# Get a list of nested 'enum' tags.

0 commit comments

Comments
 (0)