|
| 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 | + ) |
0 commit comments