Simplified generation of lists of arguments or initializers (#1821)

This commit is contained in:
Andreas Süßenbach 2024-03-05 08:16:22 +01:00 committed by GitHub
parent 6fb8def272
commit 5e7649dcb5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 104 additions and 168 deletions

View File

@ -27,15 +27,16 @@ namespace
{ {
std::vector<std::pair<std::string, size_t>> filterNumbers( std::vector<std::string> const & names ); std::vector<std::pair<std::string, size_t>> filterNumbers( std::vector<std::string> const & names );
std::string generateCArraySizes( std::vector<std::string> const & sizes ); std::string generateCArraySizes( std::vector<std::string> const & sizes );
std::string generateNamespacedType( std::string const & type ); std::string generateList( std::vector<std::string> const & elements, std::string const & prefix, std::string const & separator );
std::string generateNoDiscard( bool returnsSomething, bool multiSuccessCodes, bool multiErrorCodes ); std::string generateNamespacedType( std::string const & type );
std::string generateStandardArray( std::string const & type, std::vector<std::string> const & sizes ); std::string generateNoDiscard( bool returnsSomething, bool multiSuccessCodes, bool multiErrorCodes );
bool isAllUpper( std::string const & name ); std::string generateStandardArray( std::string const & type, std::vector<std::string> const & sizes );
VulkanHppGenerator::MacroData parseMacro( std::vector<std::string> const & completeMacro ); bool isAllUpper( std::string const & name );
std::string readSnippet( std::string const & snippetFile ); VulkanHppGenerator::MacroData parseMacro( std::vector<std::string> const & completeMacro );
std::string startLowerCase( std::string const & input ); std::string readSnippet( std::string const & snippetFile );
std::string startUpperCase( std::string const & input ); std::string startLowerCase( std::string const & input );
std::vector<std::string> tokenizeAny( std::string const & tokenString, std::string const & separators ); std::string startUpperCase( std::string const & input );
std::vector<std::string> tokenizeAny( std::string const & tokenString, std::string const & separators );
} // namespace } // namespace
void writeToFile( std::string const & str, std::string const & fileName ); void writeToFile( std::string const & str, std::string const & fileName );
@ -2288,16 +2289,11 @@ std::string VulkanHppGenerator::generateArgumentListEnhanced( std::vector<ParamD
const bool withAllocators = flavourFlags & CommandFlavourFlagBits::withAllocator; const bool withAllocators = flavourFlags & CommandFlavourFlagBits::withAllocator;
const size_t defaultStartIndex = withAllocators ? ~0 : determineDefaultStartIndex( params, skippedParams ); const size_t defaultStartIndex = withAllocators ? ~0 : determineDefaultStartIndex( params, skippedParams );
std::string argumentList; std::vector<std::string> arguments;
bool encounteredArgument = false;
for ( size_t i = 0; i < params.size(); ++i ) for ( size_t i = 0; i < params.size(); ++i )
{ {
if ( !skippedParams.contains( i ) ) if ( !skippedParams.contains( i ) )
{ {
if ( encounteredArgument )
{
argumentList += ", ";
}
bool hasDefaultAssignment = false; bool hasDefaultAssignment = false;
std::string composedType = params[i].type.compose( "VULKAN_HPP_NAMESPACE" ); std::string composedType = params[i].type.compose( "VULKAN_HPP_NAMESPACE" );
@ -2309,7 +2305,7 @@ std::string VulkanHppGenerator::generateArgumentListEnhanced( std::vector<ParamD
params[i].type.type.starts_with( "Vk" ) ); params[i].type.type.starts_with( "Vk" ) );
assert( !isHandleType( params[i].type.type ) ); assert( !isHandleType( params[i].type.type ) );
assert( composedType.ends_with( " *" ) ); assert( composedType.ends_with( " *" ) );
argumentList += stripPostfix( composedType, " *" ) + " & " + stripPluralS( startLowerCase( stripPrefix( params[i].name, "p" ) ) ); arguments.push_back( stripPostfix( composedType, " *" ) + " & " + stripPluralS( startLowerCase( stripPrefix( params[i].name, "p" ) ) ) );
} }
else if ( params[i].type.isConstPointer() ) else if ( params[i].type.isConstPointer() )
{ {
@ -2322,18 +2318,18 @@ std::string VulkanHppGenerator::generateArgumentListEnhanced( std::vector<ParamD
assert( params[i].arraySizes.empty() ); assert( params[i].arraySizes.empty() );
if ( params[i].type.type == "void" ) if ( params[i].type.type == "void" )
{ {
argumentList += arguments.push_back( templatedParams.contains( i ) ? ( stripPrefix( params[i].name, "p" ) + "Type const & " + name )
templatedParams.contains( i ) ? ( stripPrefix( params[i].name, "p" ) + "Type const & " + name ) : ( composedType + " " + params[i].name ); : ( composedType + " " + params[i].name ) );
} }
else if ( params[i].optional ) else if ( params[i].optional )
{ {
argumentList += "Optional<" + stripPostfix( composedType, " *" ) + "> " + name + arguments.push_back( "Optional<" + stripPostfix( composedType, " *" ) + "> " + name +
( ( definition || withAllocators ) ? "" : " VULKAN_HPP_DEFAULT_ARGUMENT_NULLPTR_ASSIGNMENT" ); ( ( definition || withAllocators ) ? "" : " VULKAN_HPP_DEFAULT_ARGUMENT_NULLPTR_ASSIGNMENT" ) );
hasDefaultAssignment = true; hasDefaultAssignment = true;
} }
else else
{ {
argumentList += stripPostfix( composedType, " *" ) + " & " + name; arguments.push_back( stripPostfix( composedType, " *" ) + " & " + name );
} }
} }
else else
@ -2346,13 +2342,13 @@ std::string VulkanHppGenerator::generateArgumentListEnhanced( std::vector<ParamD
assert( params[i].type.type == "char" ); assert( params[i].type.type == "char" );
if ( params[i].optional ) if ( params[i].optional )
{ {
argumentList += arguments.push_back( "Optional<const std::string> " + name +
"Optional<const std::string> " + name + ( ( definition || withAllocators ) ? "" : " VULKAN_HPP_DEFAULT_ARGUMENT_NULLPTR_ASSIGNMENT" ); ( ( definition || withAllocators ) ? "" : " VULKAN_HPP_DEFAULT_ARGUMENT_NULLPTR_ASSIGNMENT" ) );
hasDefaultAssignment = true; hasDefaultAssignment = true;
} }
else else
{ {
argumentList += "const std::string & " + name; arguments.push_back( "const std::string & " + name );
} }
} }
else else
@ -2364,12 +2360,12 @@ std::string VulkanHppGenerator::generateArgumentListEnhanced( std::vector<ParamD
{ {
type.replace( pos, 4, stripPrefix( params[i].name, "p" ) + "Type" ); type.replace( pos, 4, stripPrefix( params[i].name, "p" ) + "Type" );
} }
argumentList += arguments.push_back( std::string( "VULKAN_HPP_NAMESPACE::" ) + ( params[i].strideParam.first.empty() ? "" : "Strided" ) + "ArrayProxy<" + type +
std::string( "VULKAN_HPP_NAMESPACE::" ) + ( params[i].strideParam.first.empty() ? "" : "Strided" ) + "ArrayProxy<" + type + "> const & " + name; "> const & " + name );
if ( params[i].optional && !definition ) if ( params[i].optional && !definition )
{ {
assert( params[i].strideParam.first.empty() ); assert( params[i].strideParam.first.empty() );
argumentList += " VULKAN_HPP_DEFAULT_ARGUMENT_NULLPTR_ASSIGNMENT"; arguments.back() += " VULKAN_HPP_DEFAULT_ARGUMENT_NULLPTR_ASSIGNMENT";
hasDefaultAssignment = true; hasDefaultAssignment = true;
} }
} }
@ -2379,36 +2375,30 @@ std::string VulkanHppGenerator::generateArgumentListEnhanced( std::vector<ParamD
{ {
if ( ( params[i].type.type == "void" ) && ( flavourFlags & CommandFlavourFlagBits::keepVoidPtr ) ) if ( ( params[i].type.type == "void" ) && ( flavourFlags & CommandFlavourFlagBits::keepVoidPtr ) )
{ {
argumentList += composedType + " "; arguments.push_back( composedType + " " );
} }
else else
{ {
assert( withDispatcher || !isHandleType( params[i].type.type ) ); assert( withDispatcher || !isHandleType( params[i].type.type ) );
assert( params[i].lenExpression.empty() && !params[i].optional ); assert( params[i].lenExpression.empty() && !params[i].optional );
assert( composedType.ends_with( " *" ) ); assert( composedType.ends_with( " *" ) );
argumentList += stripPostfix( composedType, " *" ) + " & "; arguments.push_back( stripPostfix( composedType, " *" ) + " & " );
} }
argumentList += params[i].name; arguments.back() += params[i].name;
} }
else else
{ {
assert( params[i].type.isValue() ); assert( params[i].type.isValue() );
argumentList += composedType + " " + params[i].name + generateCArraySizes( params[i].arraySizes ); arguments.push_back( composedType + " " + params[i].name + generateCArraySizes( params[i].arraySizes ) );
} }
argumentList += std::string( !definition && ( defaultStartIndex <= i ) && !hasDefaultAssignment ? " VULKAN_HPP_DEFAULT_ARGUMENT_ASSIGNMENT" : "" ); arguments.back() += std::string( !definition && ( defaultStartIndex <= i ) && !hasDefaultAssignment ? " VULKAN_HPP_DEFAULT_ARGUMENT_ASSIGNMENT" : "" );
encounteredArgument = true;
} }
} }
if ( withAllocators ) if ( withAllocators )
{ {
if ( ( flavourFlags & CommandFlavourFlagBits::chained ) && needsStructureChainResize( vectorParams, chainedReturnParams ) ) if ( ( flavourFlags & CommandFlavourFlagBits::chained ) && needsStructureChainResize( vectorParams, chainedReturnParams ) )
{ {
if ( encounteredArgument ) arguments.push_back( "StructureChainAllocator & structureChainAllocator" );
{
argumentList += ", ";
}
argumentList += "StructureChainAllocator & structureChainAllocator";
encounteredArgument = true;
} }
else else
{ {
@ -2416,10 +2406,6 @@ std::string VulkanHppGenerator::generateArgumentListEnhanced( std::vector<ParamD
{ {
if ( !params[sp].lenExpression.empty() ) if ( !params[sp].lenExpression.empty() )
{ {
if ( encounteredArgument )
{
argumentList += ", ";
}
std::string type; std::string type;
if ( templatedParams.contains( sp ) ) if ( templatedParams.contains( sp ) )
{ {
@ -2440,21 +2426,16 @@ std::string VulkanHppGenerator::generateArgumentListEnhanced( std::vector<ParamD
{ {
type = ( params[sp].type.type == "void" ) ? "Uint8_t" : startUpperCase( stripPrefix( params[sp].type.type, "Vk" ) ); type = ( params[sp].type.type == "void" ) ? "Uint8_t" : startUpperCase( stripPrefix( params[sp].type.type, "Vk" ) );
} }
argumentList += type + "Allocator & " + startLowerCase( type ) + "Allocator"; arguments.push_back( type + "Allocator & " + startLowerCase( type ) + "Allocator" );
encounteredArgument = true;
} }
} }
} }
} }
if ( withDispatcher ) if ( withDispatcher )
{ {
if ( encounteredArgument ) arguments.push_back( std::string( "Dispatch const & d" ) + ( definition ? "" : " VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT" ) );
{
argumentList += ", ";
}
argumentList += std::string( "Dispatch const & d" ) + ( definition ? "" : " VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT" );
} }
return argumentList; return generateList( arguments, "", ", " );
} }
std::string VulkanHppGenerator::generateArgumentListStandard( std::vector<ParamData> const & params, std::set<size_t> const & skippedParams ) const std::string VulkanHppGenerator::generateArgumentListStandard( std::vector<ParamData> const & params, std::set<size_t> const & skippedParams ) const
@ -2712,8 +2693,7 @@ std::string VulkanHppGenerator::generateCallArgumentsEnhanced( CommandData const
CommandFlavourFlags flavourFlags ) const CommandFlavourFlags flavourFlags ) const
{ {
assert( initialSkipCount <= commandData.params.size() ); assert( initialSkipCount <= commandData.params.size() );
std::string arguments; std::vector<std::string> arguments;
bool encounteredArgument = false;
if ( raii ) if ( raii )
{ {
switch ( initialSkipCount ) switch ( initialSkipCount )
@ -2723,8 +2703,7 @@ std::string VulkanHppGenerator::generateCallArgumentsEnhanced( CommandData const
assert( isHandleType( commandData.params[0].type.type ) && commandData.params[0].type.isValue() ); assert( isHandleType( commandData.params[0].type.type ) && commandData.params[0].type.isValue() );
assert( commandData.params[0].arraySizes.empty() && commandData.params[0].lenExpression.empty() ); assert( commandData.params[0].arraySizes.empty() && commandData.params[0].lenExpression.empty() );
assert( commandData.params[0].type.type == commandData.handle ); assert( commandData.params[0].type.type == commandData.handle );
arguments = "static_cast<" + commandData.handle + ">( m_" + startLowerCase( stripPrefix( commandData.handle, "Vk" ) ) + " )"; arguments.push_back( "static_cast<" + commandData.handle + ">( m_" + startLowerCase( stripPrefix( commandData.handle, "Vk" ) ) + " )" );
encounteredArgument = true;
break; break;
case 2: case 2:
{ {
@ -2733,12 +2712,11 @@ std::string VulkanHppGenerator::generateCallArgumentsEnhanced( CommandData const
assert( commandData.params[0].type.type == commandData.handle ); assert( commandData.params[0].type.type == commandData.handle );
auto handleIt = m_handles.find( commandData.params[1].type.type ); auto handleIt = m_handles.find( commandData.params[1].type.type );
assert( handleIt != m_handles.end() ); assert( handleIt != m_handles.end() );
arguments = "static_cast<" + commandData.handle + ">( m_" + startLowerCase( stripPrefix( commandData.handle, "Vk" ) ) + " )"; arguments.push_back( "static_cast<" + commandData.handle + ">( m_" + startLowerCase( stripPrefix( commandData.handle, "Vk" ) ) + " )" );
assert( commandData.params[1].type.isValue() && commandData.params[1].arraySizes.empty() && commandData.params[1].lenExpression.empty() ); assert( commandData.params[1].type.isValue() && commandData.params[1].arraySizes.empty() && commandData.params[1].lenExpression.empty() );
arguments += ", static_cast<" + commandData.params[1].type.type + ">( m_" + arguments.push_back( "static_cast<" + commandData.params[1].type.type + ">( m_" +
generateRAIIHandleConstructorParamName( handleIt->first, handleIt->second.destructorIt ) + " )"; generateRAIIHandleConstructorParamName( handleIt->first, handleIt->second.destructorIt ) + " )" );
encounteredArgument = true;
} }
break; break;
default: assert( false ); break; default: assert( false ); break;
@ -2748,42 +2726,28 @@ std::string VulkanHppGenerator::generateCallArgumentsEnhanced( CommandData const
{ {
for ( size_t i = 0; i < initialSkipCount; ++i ) for ( size_t i = 0; i < initialSkipCount; ++i )
{ {
if ( encounteredArgument )
{
arguments += ", ";
}
assert( isHandleType( commandData.params[i].type.type ) && commandData.params[i].type.isValue() ); assert( isHandleType( commandData.params[i].type.type ) && commandData.params[i].type.isValue() );
assert( commandData.params[i].arraySizes.empty() && commandData.params[i].lenExpression.empty() ); assert( commandData.params[i].arraySizes.empty() && commandData.params[i].lenExpression.empty() );
arguments += "m_" + startLowerCase( stripPrefix( commandData.params[i].type.type, "Vk" ) ); arguments.push_back( "m_" + startLowerCase( stripPrefix( commandData.params[i].type.type, "Vk" ) ) );
encounteredArgument = true;
} }
} }
for ( size_t i = initialSkipCount; i < commandData.params.size(); ++i ) for ( size_t i = initialSkipCount; i < commandData.params.size(); ++i )
{ {
if ( encounteredArgument ) arguments.push_back(
{ generateCallArgumentEnhanced( commandData.params, i, nonConstPointerAsNullptr, singularParams, templatedParams, flavourFlags, raiiFactory ) );
arguments += ", ";
}
arguments += generateCallArgumentEnhanced( commandData.params, i, nonConstPointerAsNullptr, singularParams, templatedParams, flavourFlags, raiiFactory );
encounteredArgument = true;
} }
return arguments; return generateList( arguments, "", ", " );
} }
std::string VulkanHppGenerator::generateCallArgumentsStandard( std::string const & handle, std::vector<ParamData> const & params ) const std::string VulkanHppGenerator::generateCallArgumentsStandard( std::string const & handle, std::vector<ParamData> const & params ) const
{ {
std::string arguments; std::vector<std::string> arguments;
bool encounteredArgument = false;
for ( auto const & param : params ) for ( auto const & param : params )
{ {
if ( encounteredArgument )
{
arguments += ", ";
}
if ( ( param.type.type == handle ) && param.type.isValue() ) if ( ( param.type.type == handle ) && param.type.isValue() )
{ {
assert( param.arraySizes.empty() && param.lenExpression.empty() ); assert( param.arraySizes.empty() && param.lenExpression.empty() );
arguments += "m_" + startLowerCase( stripPrefix( param.type.type, "Vk" ) ); arguments.push_back( "m_" + startLowerCase( stripPrefix( param.type.type, "Vk" ) ) );
} }
else else
{ {
@ -2807,11 +2771,10 @@ std::string VulkanHppGenerator::generateCallArgumentsStandard( std::string const
argument = "reinterpret_cast<" + param.type.compose( "" ) + ">( " + argument + " )"; argument = "reinterpret_cast<" + param.type.compose( "" ) + ">( " + argument + " )";
} }
} }
arguments += argument; arguments.push_back( argument );
} }
encounteredArgument = true;
} }
return arguments; return generateList( arguments, "", ", " );
} }
std::string VulkanHppGenerator::generateCallArgumentEnhanced( std::vector<ParamData> const & params, std::string VulkanHppGenerator::generateCallArgumentEnhanced( std::vector<ParamData> const & params,
@ -8836,21 +8799,15 @@ std::string VulkanHppGenerator::generateRAIIHandleCommandFactoryArgumentList( st
bool definition, bool definition,
bool singular ) const bool singular ) const
{ {
std::string arguments; std::vector<std::string> arguments;
bool encounteredArgument = false;
for ( size_t i = 0; i < params.size(); ++i ) for ( size_t i = 0; i < params.size(); ++i )
{ {
if ( !skippedParams.contains( i ) ) if ( !skippedParams.contains( i ) )
{ {
if ( encounteredArgument ) arguments.push_back( generateRAIIHandleConstructorArgument( params[i], definition, singular, false ) );
{
arguments += ", ";
}
arguments += generateRAIIHandleConstructorArgument( params[i], definition, singular, false );
encounteredArgument = true;
} }
} }
return arguments; return generateList( arguments, "", ", " );
} }
std::pair<std::string, std::string> std::pair<std::string, std::string>
@ -9503,30 +9460,25 @@ ${memberFunctionDeclarations}
std::string VulkanHppGenerator::generateRAIIHandleDestructorCallArguments( std::string const & handleType, std::string VulkanHppGenerator::generateRAIIHandleDestructorCallArguments( std::string const & handleType,
std::map<std::string, CommandData>::const_iterator destructorIt ) const std::map<std::string, CommandData>::const_iterator destructorIt ) const
{ {
std::string arguments; std::vector<std::string> arguments;
bool encounteredArgument = false;
for ( auto param : destructorIt->second.params ) for ( auto param : destructorIt->second.params )
{ {
if ( encounteredArgument )
{
arguments += ", ";
}
if ( param.type.type == handleType ) if ( param.type.type == handleType )
{ {
std::string handleName = param.name; std::string handleName = param.name;
if ( param.type.isValue() ) if ( param.type.isValue() )
{ {
arguments += "static_cast<" + handleType + ">( m_" + handleName + " )"; arguments.push_back( "static_cast<" + handleType + ">( m_" + handleName + " )" );
} }
else else
{ {
arguments += "reinterpret_cast<" + handleType + " const *>( &m_" + stripPluralS( startLowerCase( stripPrefix( handleName, "p" ) ) ) + " )"; arguments.push_back( "reinterpret_cast<" + handleType + " const *>( &m_" + stripPluralS( startLowerCase( stripPrefix( handleName, "p" ) ) ) + " )" );
} }
} }
else if ( param.type.type == "VkAllocationCallbacks" ) else if ( param.type.type == "VkAllocationCallbacks" )
{ {
// vk::AllocationCallbacks is stored as a member of the handle class // vk::AllocationCallbacks is stored as a member of the handle class
arguments += "reinterpret_cast<const VkAllocationCallbacks *>( m_allocator )"; arguments.push_back( "reinterpret_cast<const VkAllocationCallbacks *>( m_allocator )" );
} }
else if ( isHandleType( param.type.type ) ) else if ( isHandleType( param.type.type ) )
{ {
@ -9534,7 +9486,7 @@ std::string VulkanHppGenerator::generateRAIIHandleDestructorCallArguments( std::
std::string argument = "m_" + param.name; std::string argument = "m_" + param.name;
if ( param.type.isValue() ) if ( param.type.isValue() )
{ {
arguments += "static_cast<" + param.type.type + ">( " + argument + " )"; arguments.push_back( "static_cast<" + param.type.type + ">( " + argument + " )" );
} }
else else
{ {
@ -9542,7 +9494,7 @@ std::string VulkanHppGenerator::generateRAIIHandleDestructorCallArguments( std::
assert( !param.lenExpression.empty() && std::any_of( destructorIt->second.params.begin(), assert( !param.lenExpression.empty() && std::any_of( destructorIt->second.params.begin(),
destructorIt->second.params.end(), destructorIt->second.params.end(),
[&param]( ParamData const & pd ) { return pd.name == param.lenExpression; } ) ); [&param]( ParamData const & pd ) { return pd.name == param.lenExpression; } ) );
arguments += "reinterpret_cast<" + param.type.type + " const *>( &" + argument + " )"; arguments.push_back( "reinterpret_cast<" + param.type.type + " const *>( &" + argument + " )" );
} }
} }
else else
@ -9550,11 +9502,10 @@ std::string VulkanHppGenerator::generateRAIIHandleDestructorCallArguments( std::
assert( ( param.type.type == "uint32_t" ) && param.type.isValue() && param.arraySizes.empty() && param.lenExpression.empty() && !param.optional ); assert( ( param.type.type == "uint32_t" ) && param.type.isValue() && param.arraySizes.empty() && param.lenExpression.empty() && !param.optional );
assert( std::any_of( assert( std::any_of(
destructorIt->second.params.begin(), destructorIt->second.params.end(), [&param]( ParamData const & pd ) { return pd.lenExpression == param.name; } ) ); destructorIt->second.params.begin(), destructorIt->second.params.end(), [&param]( ParamData const & pd ) { return pd.lenExpression == param.name; } ) );
arguments += "1"; arguments.push_back( "1" );
} }
encounteredArgument = true;
} }
return arguments; return generateList( arguments, "", ", " );
} }
std::tuple<std::string, std::string, std::string, std::string, std::string, std::string, std::string> std::tuple<std::string, std::string, std::string, std::string, std::string, std::string, std::string>
@ -10590,37 +10541,33 @@ std::string VulkanHppGenerator::generateStructConstructors( std::pair<std::strin
{} {}
)"; )";
std::string arguments, initializers; std::vector<std::string> arguments, initializers;
bool listedArgument = false;
bool firstArgument = true;
for ( auto const & member : structData.second.members ) for ( auto const & member : structData.second.members )
{ {
// gather the arguments // gather the arguments
std::string argument = generateStructConstructorArgument( listedArgument, member, true ); std::string argument = generateStructConstructorArgument( member, true );
if ( !argument.empty() ) if ( !argument.empty() )
{ {
listedArgument = true; arguments.push_back( argument );
arguments += argument;
} }
// gather the initializers; skip members with exactly one legal value // gather the initializers; skip members with exactly one legal value
if ( member.value.empty() ) if ( member.value.empty() )
{ {
initializers += std::string( firstArgument ? ": " : ", " ) + member.name + "( " + member.name + "_ )"; initializers.push_back( member.name + "( " + member.name + "_ )" );
firstArgument = false;
} }
} }
auto pNextIt = std::find_if( structData.second.members.begin(), structData.second.members.end(), []( MemberData const & md ) { return md.name == "pNext"; } ); auto pNextIt = std::find_if( structData.second.members.begin(), structData.second.members.end(), []( MemberData const & md ) { return md.name == "pNext"; } );
if ( pNextIt != structData.second.members.end() ) if ( pNextIt != structData.second.members.end() )
{ {
// add pNext as a last optional argument to the constructor // add pNext as a last optional argument to the constructor
arguments += std::string( listedArgument ? ", " : "" ) + pNextIt->type.compose( "VULKAN_HPP_NAMESPACE" ) + " pNext_ = nullptr"; arguments.push_back( pNextIt->type.compose( "VULKAN_HPP_NAMESPACE" ) + " pNext_ = nullptr" );
} }
std::string str = replaceWithMap( constructors, std::string str = replaceWithMap( constructors,
{ { "arguments", arguments }, { { "arguments", generateList( arguments, "", ", " ) },
{ "constexpr", generateConstexprString( structData.first ) }, { "constexpr", generateConstexprString( structData.first ) },
{ "initializers", initializers }, { "initializers", generateList( initializers, ": ", ", " ) },
{ "structName", stripPrefix( structData.first, "Vk" ) } } ); { "structName", stripPrefix( structData.first, "Vk" ) } } );
str += generateStructConstructorsEnhanced( structData ); str += generateStructConstructorsEnhanced( structData );
@ -10646,17 +10593,15 @@ std::string VulkanHppGenerator::generateStructConstructorsEnhanced( std::pair<st
} }
} }
std::string arguments; std::vector<std::string> arguments, initializers;
std::vector<std::string> initializersList; bool arrayListed = false;
bool listedArgument = false;
bool arrayListed = false;
std::string templateHeader, sizeChecks, copyOps; std::string templateHeader, sizeChecks, copyOps;
for ( auto mit = structData.second.members.begin(); mit != structData.second.members.end(); ++mit ) for ( auto mit = structData.second.members.begin(); mit != structData.second.members.end(); ++mit )
{ {
// gather the initializers // gather the initializers
if ( mit->name == "pNext" ) // for pNext, we just get the initializer... the argument is added at the end if ( mit->name == "pNext" ) // for pNext, we just get the initializer... the argument is added at the end
{ {
initializersList.push_back( "pNext( pNext_ )" ); initializers.push_back( "pNext( pNext_ )" );
} }
else if ( mit->value.empty() ) // skip constant members else if ( mit->value.empty() ) // skip constant members
{ {
@ -10664,7 +10609,7 @@ std::string VulkanHppGenerator::generateStructConstructorsEnhanced( std::pair<st
if ( litit != lenIts.end() ) if ( litit != lenIts.end() )
{ {
// len arguments just have an initalizer, from the array size // len arguments just have an initalizer, from the array size
initializersList.push_back( mit->name + "( " + generateLenInitializer( mit, litit, structData.second.mutualExclusiveLens ) + " )" ); initializers.push_back( mit->name + "( " + generateLenInitializer( mit, litit, structData.second.mutualExclusiveLens ) + " )" );
sizeChecks += generateSizeCheck( litit->second, stripPrefix( structData.first, "Vk" ), structData.second.mutualExclusiveLens ); sizeChecks += generateSizeCheck( litit->second, stripPrefix( structData.first, "Vk" ), structData.second.mutualExclusiveLens );
} }
else if ( hasLen( *mit, structData.second.members ) ) else if ( hasLen( *mit, structData.second.members ) )
@ -10688,31 +10633,29 @@ std::string VulkanHppGenerator::generateStructConstructorsEnhanced( std::pair<st
argumentType.replace( pos, strlen( "void" ), "T" ); argumentType.replace( pos, strlen( "void" ), "T" );
} }
arguments += listedArgument ? ", " : "";
if ( mit->lenExpressions[0] == "null-terminated" ) if ( mit->lenExpressions[0] == "null-terminated" )
{ {
assert( ( mit->type.type == "char" ) && ( mit->arraySizes.size() == 1 ) ); assert( ( mit->type.type == "char" ) && ( mit->arraySizes.size() == 1 ) );
arguments += "std::string const & " + argumentName; arguments.push_back( "std::string const & " + argumentName );
} }
else if ( mit->arraySizes.empty() ) else if ( mit->arraySizes.empty() )
{ {
arguments += "VULKAN_HPP_NAMESPACE::ArrayProxyNoTemporaries<" + argumentType + "> const & " + argumentName; arguments.push_back( "VULKAN_HPP_NAMESPACE::ArrayProxyNoTemporaries<" + argumentType + "> const & " + argumentName );
} }
else else
{ {
assert( mit->arraySizes.size() == 1 ); assert( mit->arraySizes.size() == 1 );
arguments += "VULKAN_HPP_NAMESPACE::ArrayProxy<" + argumentType + "> const & " + argumentName; arguments.push_back( "VULKAN_HPP_NAMESPACE::ArrayProxy<" + argumentType + "> const & " + argumentName );
} }
if ( arrayListed ) if ( arrayListed )
{ {
arguments += " = {}"; arguments.back() += " = {}";
} }
listedArgument = true; arrayListed = true;
arrayListed = true;
if ( mit->type.isPointer() ) if ( mit->type.isPointer() )
{ {
initializersList.push_back( mit->name + "( " + argumentName + ".data() )" ); initializers.push_back( mit->name + "( " + argumentName + ".data() )" );
} }
else else
{ {
@ -10750,13 +10693,12 @@ std::string VulkanHppGenerator::generateStructConstructorsEnhanced( std::pair<st
} }
else else
{ {
std::string argument = generateStructConstructorArgument( listedArgument, *mit, arrayListed ); std::string argument = generateStructConstructorArgument( *mit, arrayListed );
if ( !argument.empty() ) if ( !argument.empty() )
{ {
listedArgument = true; arguments.push_back( argument );
arguments += argument;
} }
initializersList.push_back( mit->name + "( " + mit->name + "_ )" ); initializers.push_back( mit->name + "( " + mit->name + "_ )" );
} }
} }
} }
@ -10766,17 +10708,7 @@ std::string VulkanHppGenerator::generateStructConstructorsEnhanced( std::pair<st
if ( pNextIt != structData.second.members.end() ) if ( pNextIt != structData.second.members.end() )
{ {
// add pNext as a last optional argument to the constructor // add pNext as a last optional argument to the constructor
arguments += std::string( listedArgument ? ", " : "" ) + pNextIt->type.compose( "VULKAN_HPP_NAMESPACE" ) + " pNext_ = nullptr"; arguments.push_back( pNextIt->type.compose( "VULKAN_HPP_NAMESPACE" ) + " pNext_ = nullptr" );
}
std::string initializers;
if ( !initializersList.empty() )
{
initializers = ": " + initializersList[0];
for ( size_t i = 1; i < initializersList.size(); ++i )
{
initializers += ", " + initializersList[i];
}
} }
static const std::string constructorTemplate = R"( static const std::string constructorTemplate = R"(
@ -10788,9 +10720,9 @@ ${templateHeader} ${structName}( ${arguments} )
)"; )";
return replaceWithMap( constructorTemplate, return replaceWithMap( constructorTemplate,
{ { "arguments", arguments }, { { "arguments", generateList( arguments, "", ", " ) },
{ "copyOps", copyOps }, { "copyOps", copyOps },
{ "initializers", initializers }, { "initializers", generateList( initializers, ": ", ", " ) },
{ "sizeChecks", sizeChecks }, { "sizeChecks", sizeChecks },
{ "structName", stripPrefix( structData.first, "Vk" ) }, { "structName", stripPrefix( structData.first, "Vk" ) },
{ "templateHeader", templateHeader } } ); { "templateHeader", templateHeader } } );
@ -10798,13 +10730,12 @@ ${templateHeader} ${structName}( ${arguments} )
return ""; return "";
} }
std::string VulkanHppGenerator::generateStructConstructorArgument( bool listedArgument, MemberData const & memberData, bool withDefault ) const std::string VulkanHppGenerator::generateStructConstructorArgument( MemberData const & memberData, bool withDefault ) const
{ {
// skip members 'pNext' and members with a specified value, as they are never explicitly set // skip members 'pNext' and members with a specified value, as they are never explicitly set
std::string str; std::string str;
if ( ( memberData.name != "pNext" ) && memberData.value.empty() ) if ( ( memberData.name != "pNext" ) && memberData.value.empty() )
{ {
str += ( listedArgument ? ( ", " ) : "" );
if ( memberData.arraySizes.empty() ) if ( memberData.arraySizes.empty() )
{ {
str += memberData.type.compose( "VULKAN_HPP_NAMESPACE" ) + " "; str += memberData.type.compose( "VULKAN_HPP_NAMESPACE" ) + " ";
@ -11607,15 +11538,13 @@ std::string VulkanHppGenerator::generateStructSubConstructor( std::pair<std::str
firstArgument = false; firstArgument = false;
} }
std::string subArguments; std::vector<std::string> subArguments;
bool listedArgument = true;
for ( size_t i = subStruct->second.members.size(); i < structData.second.members.size(); i++ ) for ( size_t i = subStruct->second.members.size(); i < structData.second.members.size(); i++ )
{ {
std::string argument = generateStructConstructorArgument( listedArgument, structData.second.members[i], true ); std::string argument = generateStructConstructorArgument( structData.second.members[i], true );
if ( !argument.empty() ) if ( !argument.empty() )
{ {
listedArgument = true; subArguments.push_back( argument );
subArguments += argument;
} }
assert( structData.second.members[i].arraySizes.empty() ); assert( structData.second.members[i].arraySizes.empty() );
@ -11630,7 +11559,7 @@ ${subCopies} {}
)"; )";
return replaceWithMap( subStructConstructorTemplate, return replaceWithMap( subStructConstructorTemplate,
{ { "structName", stripPrefix( structData.first, "Vk" ) }, { { "structName", stripPrefix( structData.first, "Vk" ) },
{ "subArguments", subArguments }, { "subArguments", generateList( subArguments, ", ", ", " ) },
{ "subCopies", subCopies }, { "subCopies", subCopies },
{ "subStructArgumentName", subStructArgumentName }, { "subStructArgumentName", subStructArgumentName },
{ "subStructName", stripPrefix( subStruct->first, "Vk" ) } } ); { "subStructName", stripPrefix( subStruct->first, "Vk" ) } } );
@ -11797,19 +11726,12 @@ std::string VulkanHppGenerator::generateUnion( std::pair<std::string, StructureD
assert( memberIt->arraySizes.size() == 1 ); assert( memberIt->arraySizes.size() == 1 );
const int size = std::stoi( memberIt->arraySizes[0] ); const int size = std::stoi( memberIt->arraySizes[0] );
assert( std::to_string( size ) == memberIt->arraySizes[0] ); assert( std::to_string( size ) == memberIt->arraySizes[0] );
std::string arguments, callArguments; std::vector<std::string> arguments, callArguments;
bool firstArgument = true;
for ( int i = 0; i < size; i++ ) for ( int i = 0; i < size; i++ )
{ {
if ( !firstArgument )
{
arguments += ", ";
callArguments += ", ";
}
std::string argumentIndex = std::to_string( i ); std::string argumentIndex = std::to_string( i );
arguments += memberIt->type.type + " " + memberIt->name + "_" + argumentIndex; arguments.push_back( memberIt->type.type + " " + memberIt->name + "_" + argumentIndex );
callArguments += memberIt->name + "_" + argumentIndex; callArguments.push_back( memberIt->name + "_" + argumentIndex );
firstArgument = false;
} }
static const std::string constructorBySequenceTemplate = R"( static const std::string constructorBySequenceTemplate = R"(
@ -11818,8 +11740,8 @@ std::string VulkanHppGenerator::generateUnion( std::pair<std::string, StructureD
{})"; {})";
constructors += "\n" + replaceWithMap( constructorBySequenceTemplate, constructors += "\n" + replaceWithMap( constructorBySequenceTemplate,
{ { "arguments", arguments }, { { "arguments", generateList( arguments, "", ", " ) },
{ "callArguments", callArguments }, { "callArguments", generateList( callArguments, "", ", " ) },
{ "memberName", memberIt->name }, { "memberName", memberIt->name },
{ "unionName", stripPrefix( structure.first, "Vk" ) } } ); { "unionName", stripPrefix( structure.first, "Vk" ) } } );
} }
@ -15568,6 +15490,20 @@ namespace
return arraySizes; return arraySizes;
} }
std::string generateList( std::vector<std::string> const & elements, std::string const & prefix, std::string const & separator )
{
std::string list;
if ( !elements.empty() )
{
list = prefix + elements[0];
for ( size_t i = 1; i < elements.size(); ++i )
{
list += separator + elements[i];
}
}
return list;
}
std::string generateNamespacedType( std::string const & type ) std::string generateNamespacedType( std::string const & type )
{ {
return type.starts_with( "Vk" ) ? ( "VULKAN_HPP_NAMESPACE::" + stripPrefix( type, "Vk" ) ) : type; return type.starts_with( "Vk" ) ? ( "VULKAN_HPP_NAMESPACE::" + stripPrefix( type, "Vk" ) ) : type;

View File

@ -860,7 +860,7 @@ private:
std::string generateStructCompareOperators( std::pair<std::string, StructureData> const & structure ) const; std::string generateStructCompareOperators( std::pair<std::string, StructureData> const & structure ) const;
std::string generateStructConstructors( std::pair<std::string, StructureData> const & structData ) const; std::string generateStructConstructors( std::pair<std::string, StructureData> const & structData ) const;
std::string generateStructConstructorsEnhanced( std::pair<std::string, StructureData> const & structData ) const; std::string generateStructConstructorsEnhanced( std::pair<std::string, StructureData> const & structData ) const;
std::string generateStructConstructorArgument( bool listedArgument, MemberData const & memberData, bool withDefault ) const; std::string generateStructConstructorArgument( MemberData const & memberData, bool withDefault ) const;
std::string generateStructHashStructure( std::pair<std::string, StructureData> const & structure, std::set<std::string> & listedStructs ) const; std::string generateStructHashStructure( std::pair<std::string, StructureData> const & structure, std::set<std::string> & listedStructs ) const;
std::string generateStructHashStructures() const; std::string generateStructHashStructures() const;
std::string generateStructHashSum( std::string const & structName, std::vector<MemberData> const & members ) const; std::string generateStructHashSum( std::string const & structName, std::vector<MemberData> const & members ) const;