Skip to content

Commit 1e74722

Browse files
MarkY-LunarGjzulauf-lunarg
authored andcommitted
codegen: Move enum->string body code to Khronos
1 parent a8acb05 commit 1e74722

File tree

2 files changed

+96
-67
lines changed

2 files changed

+96
-67
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/python3 -i
2+
#
3+
# Copyright (c) 2021-2024 LunarG, Inc.
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to
7+
# deal in the Software without restriction, including without limitation the
8+
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9+
# sell copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21+
# IN THE SOFTWARE.
22+
23+
import sys
24+
from khronos_base_generator import write
25+
26+
27+
# KhronosEnumToStringBodyGenerator
28+
# Generates C++ functions for stringifying Khronos API enums.
29+
class KhronosEnumToStringBodyGenerator():
30+
"""Generate C++ functions for Khronos ToString() functions"""
31+
32+
def write_enum_to_string_body(self):
33+
for enum in sorted(self.enum_names):
34+
if not enum in self.enumAliases:
35+
if self.is_flags_enum_64bit(enum):
36+
# Since every caller needs to know exactly what it is calling, we may as well
37+
# dispense with the parameters that are always ignored:
38+
body = 'std::string {0}ToString(const {0} value)\n'
39+
else:
40+
body = 'template <> std::string ToString<{0}>(const {0}& value, ToStringFlags, uint32_t, uint32_t)\n'
41+
body += '{{\n'
42+
enumerants = self.enumEnumerants[enum]
43+
if len(enumerants):
44+
body += ' switch (value) {{\n'
45+
for enumerant in enumerants:
46+
body += ' case {0}: return "{0}";\n'.format(
47+
enumerant
48+
)
49+
body += ' default: break;\n'
50+
body += ' }}\n'
51+
body += ' return "Unhandled {0}";\n'
52+
body += '}}\n'
53+
if 'Bits' in enum:
54+
if self.is_flags_enum_64bit(enum):
55+
body += '\nstd::string {1}ToString(VkFlags64 vkFlags)\n'
56+
body += '{{\n'
57+
body += ' std::string str;\n'
58+
body += ' VkFlags64 index = 0U;\n'
59+
body += ' while (vkFlags)\n'
60+
body += ' {{\n'
61+
body += ' if (vkFlags & 1U)\n'
62+
body += ' {{\n'
63+
body += ' if (!str.empty())\n'
64+
body += ' {{\n'
65+
body += ' str += \'|\';\n'
66+
body += ' }}\n'
67+
body += ' str.append({0}ToString(static_cast<{0}>(1U) << index));\n'
68+
body += ' }}\n'
69+
body += ' ++index;\n'
70+
body += ' vkFlags >>= 1U;\n'
71+
body += ' }}\n'
72+
body += ' if (str.empty())\n'
73+
body += ' {{\n'
74+
body += ' str.append({0}ToString(0U));\n'
75+
body += ' }}\n'
76+
body += ' return str;\n'
77+
body += '}}\n'
78+
else:
79+
# Original version(these are never actually being called which is part of issue #620):
80+
body += '\ntemplate <> std::string ToString<{0}>(VkFlags vkFlags, ToStringFlags, uint32_t, uint32_t)\n'
81+
# Simpler, non-template version that matches the 64 bit version above. Changing
82+
# to these signatures actually compiles fine, showing the originals were never
83+
# called anywhere. Leaving this commented-out but ready for the PR that fixes
84+
# issue #620 to use.
85+
# body += '\nstd::string {1}ToString(VkFlags vkFlags)\n'
86+
body += '{{\n'
87+
body += ' return BitmaskToString<{0}>(vkFlags);\n'
88+
body += '}}\n'
89+
write(
90+
body.format(enum, self.get_flags_type_from_enum(enum)),
91+
file=self.outFile
92+
)

framework/generated/khronos_generators/vulkan_generators/vulkan_enum_to_string_body_generator.py

+4-67
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2121
# IN THE SOFTWARE.
2222

23-
import os
24-
import re
2523
import sys
2624
import inspect
2725
from base_generator import *
26+
from khronos_enum_to_string_body_generator import KhronosEnumToStringBodyGenerator
2827

2928

3029
class VulkanEnumToStringBodyGeneratorOptions(BaseGeneratorOptions):
@@ -58,7 +57,7 @@ def __init__(
5857

5958
# VulkanEnumToStringBodyGenerator - subclass of BaseGenerator.
6059
# Generates C++ functions for stringifying Vulkan API enums.
61-
class VulkanEnumToStringBodyGenerator(BaseGenerator):
60+
class VulkanEnumToStringBodyGenerator(BaseGenerator, KhronosEnumToStringBodyGenerator):
6261
"""Generate C++ functions for Vulkan ToString() functions"""
6362

6463
def __init__(
@@ -87,7 +86,8 @@ def beginFile(self, genOpts):
8786
# Method override
8887
# yapf: disable
8988
def endFile(self):
90-
self.write_enum_to_string_body()
89+
KhronosEnumToStringBodyGenerator.write_enum_to_string_body(self)
90+
9191
body = inspect.cleandoc('''
9292
GFXRECON_END_NAMESPACE(util)
9393
GFXRECON_END_NAMESPACE(gfxrecon)
@@ -104,66 +104,3 @@ def need_feature_generation(self):
104104
if self.feature_struct_members:
105105
return True
106106
return False
107-
108-
# Performs C++ code generation for the enum to string body.
109-
# yapf: disable
110-
def write_enum_to_string_body(self):
111-
for enum in sorted(self.enum_names):
112-
if not enum in self.enumAliases:
113-
if self.is_flags_enum_64bit(enum):
114-
# Since every caller needs to know exactly what it is calling, we may as well
115-
# dispense with the parameters that are always ignored:
116-
body = 'std::string {0}ToString(const {0} value)\n'
117-
else:
118-
body = 'template <> std::string ToString<{0}>(const {0}& value, ToStringFlags, uint32_t, uint32_t)\n'
119-
body += '{{\n'
120-
enumerants = self.enumEnumerants[enum]
121-
if len(enumerants):
122-
body += ' switch (value) {{\n'
123-
for enumerant in enumerants:
124-
body += ' case {0}: return "{0}";\n'.format(
125-
enumerant)
126-
body += ' default: break;\n'
127-
body += ' }}\n'
128-
body += ' return "Unhandled {0}";\n'
129-
body += '}}\n'
130-
if 'Bits' in enum:
131-
if self.is_flags_enum_64bit(enum):
132-
body += '\nstd::string {1}ToString(VkFlags64 vkFlags)\n'
133-
body += '{{\n'
134-
body += ' std::string str;\n'
135-
body += ' VkFlags64 index = 0U;\n'
136-
body += ' while (vkFlags)\n'
137-
body += ' {{\n'
138-
body += ' if (vkFlags & 1U)\n'
139-
body += ' {{\n'
140-
body += ' if (!str.empty())\n'
141-
body += ' {{\n'
142-
body += ' str += \'|\';\n'
143-
body += ' }}\n'
144-
body += ' str.append({0}ToString(static_cast<{0}>(1U) << index));\n'
145-
body += ' }}\n'
146-
body += ' ++index;\n'
147-
body += ' vkFlags >>= 1U;\n'
148-
body += ' }}\n'
149-
body += ' if (str.empty())\n'
150-
body += ' {{\n'
151-
body += ' str.append({0}ToString(0U));\n'
152-
body += ' }}\n'
153-
body += ' return str;\n'
154-
body += '}}\n'
155-
else:
156-
# Original version(these are never actually being called which is part of issue #620):
157-
body += '\ntemplate <> std::string ToString<{0}>(VkFlags vkFlags, ToStringFlags, uint32_t, uint32_t)\n'
158-
# Simpler, non-template version that matches the 64 bit version above. Changing
159-
# to these signatures actually compiles fine, showing the originals were never
160-
# called anywhere. Leaving this commented-out but ready for the PR that fixes
161-
# issue #620 to use.
162-
# body += '\nstd::string {1}ToString(VkFlags vkFlags)\n'
163-
body += '{{\n'
164-
body += ' return BitmaskToString<{0}>(vkFlags);\n'
165-
body += '}}\n'
166-
write(body.format(enum, self.get_flags_type_from_enum(enum)),
167-
file=self.outFile)
168-
169-
# yapf: enable

0 commit comments

Comments
 (0)