From 11121e142a6dfa7038bf8d75068e4f6a81d2713c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20S=C3=BC=C3=9Fenbach?= Date: Mon, 15 Apr 2024 10:18:02 +0200 Subject: [PATCH] Simplified constant alias handling (#1845) --- VulkanHppGenerator.cpp | 44 +++++++++++++++++++----------------------- VulkanHppGenerator.hpp | 8 ++++---- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/VulkanHppGenerator.cpp b/VulkanHppGenerator.cpp index 1c77d41..fb2f774 100644 --- a/VulkanHppGenerator.cpp +++ b/VulkanHppGenerator.cpp @@ -4897,15 +4897,8 @@ std::string VulkanHppGenerator::generateConstexprDefines() const { if ( !listedConstants.contains( constant ) ) { - auto constIt = m_constants.find( constant ); - if ( constIt == m_constants.end() ) - { - auto aliasIt = m_constantAliases.find( constant ); - assert( aliasIt != m_constantAliases.end() ); - constIt = m_constants.find( aliasIt->second.name ); - assert( constIt != m_constants.end() ); - } - std::string tag = findTag( constant ); + auto constIt = findByNameOrAlias( m_constants, constant ); + std::string tag = findTag( constant ); constants += replaceWithMap( constexprValueTemplate, { { "type", constIt->second.type }, { "constName", stripPrefix( toCamelCase( stripPostfix( constant, tag ) ), "Vk" ) + tag }, @@ -5126,14 +5119,7 @@ std::string VulkanHppGenerator::generateConstexprUsings() const { if ( !listedConstants.contains( constant ) ) { - auto constIt = m_constants.find( constant ); - if ( constIt == m_constants.end() ) - { - auto aliasIt = m_constantAliases.find( constant ); - assert( aliasIt != m_constantAliases.end() ); - constIt = m_constants.find( aliasIt->second.name ); - assert( constIt != m_constants.end() ); - } + assert( findByNameOrAlias( m_constants, constant ) != m_constants.end() ); std::string tag = findTag( constant ); constants += replaceWithMap( constexprUsingTemplate, { { "constName", stripPrefix( toCamelCase( stripPostfix( constant, tag ) ), "Vk" ) + tag } } ); listedConstants.insert( constant ); @@ -13024,10 +13010,10 @@ void VulkanHppGenerator::readEnumsConstants( tinyxml2::XMLElement const * elemen std::string alias = aliasIt->second; std::string name = attributes.find( "name" )->second; - checkForError( m_constants.contains( alias ), line, "enum <" + name + "> is an alias of an unknown enum <" + alias + ">." ); + auto constIt = m_constants.find( alias ); + checkForError( constIt != m_constants.end(), line, "constant alias <" + name + "> is an alias of an unknown constant <" + alias + ">." ); + checkForError( constIt->second.aliases.insert( { name, line } ).second, line, "constant alias <" + name + "> already listed for constant <" + alias + ">" ); checkForError( m_types.insert( { name, TypeData{ TypeCategory::Constant, {}, line } } ).second, line, "enum <" + name + "> already specified" ); - assert( !m_constantAliases.contains( name ) ); - m_constantAliases[name] = { alias, line }; } else { @@ -13054,7 +13040,7 @@ void VulkanHppGenerator::readEnumsConstants( tinyxml2::XMLElement const * elemen checkForError( m_types.insert( { name, TypeData{ TypeCategory::Constant, {}, line } } ).second, line, "enum <" + name + "> already specified" ); assert( !m_constants.contains( name ) ); - m_constants[name] = { type, value, line }; + m_constants[name] = { {}, type, value, line }; } } @@ -13882,10 +13868,20 @@ void VulkanHppGenerator::readRequireEnum( if ( extends.empty() ) { + // enum aliases that don't extend something are listed as constants + auto typeIt = m_types.find( alias ); + checkForError( typeIt != m_types.end(), line, "enum alias <" + name + "> is an alias of an unknown enum <" + alias + ">" ); + checkForError( + typeIt->second.category == TypeCategory::Constant, line, "enum alias <" + name + "> is an alias of a non-constant type <" + alias + ">" ); checkForError( m_types.insert( { name, TypeData{ TypeCategory::Constant, { requiredBy }, line } } ).second, line, "required enum <" + name + "> already specified" ); - assert( !m_constantAliases.contains( name ) ); - m_constantAliases[name] = { alias, line }; + + // if that constant is a uint32_t, it's stored in m_constants (I think, this doesn't happen at all!!) + auto constIt = m_constants.find( alias ); + if ( constIt != m_constants.end() ) + { + checkForError( constIt->second.aliases.insert( { name, line } ).second, line, "enum alias <" + name + "> already listed for enum <" + alias + ">" ); + } } else { @@ -13971,7 +13967,7 @@ void VulkanHppGenerator::readRequireEnum( if ( type == "uint32_t" ) { assert( !m_constants.contains( name ) ); - m_constants[name] = { type, value, line }; + m_constants[name] = { {}, type, value, line }; } } diff --git a/VulkanHppGenerator.hpp b/VulkanHppGenerator.hpp index 480d2c1..0d45ccc 100644 --- a/VulkanHppGenerator.hpp +++ b/VulkanHppGenerator.hpp @@ -199,9 +199,10 @@ private: struct ConstantData { - std::string type = {}; - std::string value = {}; - int xmlLine = {}; + std::map aliases = {}; + std::string type = {}; + std::string value = {}; + int xmlLine = {}; }; struct DefineData @@ -1033,7 +1034,6 @@ private: std::map m_baseTypes; std::map m_bitmasks; std::map m_commands; - std::map m_constantAliases; std::map m_constants; std::map m_defines; DefinesPartition m_definesPartition; // partition defined macros into mutually-exclusive sets of callees, callers, and values