Add support of attribute "len" for array-sized structure members. (#1779)

This commit is contained in:
Andreas Süßenbach 2024-02-02 12:53:29 +01:00 committed by GitHub
parent d4b36b8223
commit 78bfb316aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 203 additions and 56 deletions

View File

@ -7969,8 +7969,9 @@ std::string VulkanHppGenerator::generateLenInitializer(
assert( ( arrayIt->lenExpressions.front() == litit->first->name ) || assert( ( arrayIt->lenExpressions.front() == litit->first->name ) ||
( ( arrayIt->lenExpressions.front() == "codeSize / 4" ) && ( litit->first->name == "codeSize" ) ) ); ( ( arrayIt->lenExpressions.front() == "codeSize / 4" ) && ( litit->first->name == "codeSize" ) ) );
assert( arrayIt->name.starts_with( "p" ) ); assert( arrayIt->type.isPointer() || !arrayIt->arraySizes.empty() );
std::string argumentName = startLowerCase( stripPrefix( arrayIt->name, "p" ) ) + "_"; assert( !arrayIt->type.isPointer() || arrayIt->name.starts_with( "p" ) );
std::string argumentName = ( arrayIt->type.isPointer() ? startLowerCase( stripPrefix( arrayIt->name, "p" ) ) : arrayIt->name ) + "_";
assert( mit->type.prefix.empty() && mit->type.postfix.empty() ); assert( mit->type.prefix.empty() && mit->type.postfix.empty() );
initializer = argumentName + ".size()"; initializer = argumentName + ".size()";
@ -7988,6 +7989,11 @@ std::string VulkanHppGenerator::generateLenInitializer(
{ {
initializer = "static_cast<" + mit->type.type + ">( " + initializer + " )"; initializer = "static_cast<" + mit->type.type + ">( " + initializer + " )";
} }
if ( !litit->second.front()->arraySizes.empty() )
{
assert( litit->second.front()->arraySizes.size() == 1 );
initializer = "std::min( " + initializer + ", " + litit->second.front()->arraySizes[0] + " )";
}
return initializer; return initializer;
} }
@ -10390,14 +10396,28 @@ std::string VulkanHppGenerator::generateStructCompareOperators( std::pair<std::s
if ( member.lenExpressions.size() == 1 ) if ( member.lenExpressions.size() == 1 )
{ {
assert( member.lenExpressions[0] == "null-terminated" ); assert( member.lenExpressions[0] == "null-terminated" );
compareMembers += intro + "( ( " + member.name + " == rhs." + member.name + " ) || ( strcmp( " + member.name + ", rhs." + member.name + " ) == 0 ) )"; if ( member.arraySizes.empty() )
{
compareMembers += intro + "( ( " + member.name + " == rhs." + member.name + " ) || ( strcmp( " + member.name + ", rhs." + member.name + " ) == 0 ) )";
static const std::string spaceshipMemberTemplate = static const std::string spaceshipMemberTemplate =
R"( if ( ${name} != rhs.${name} ) R"( if ( ${name} != rhs.${name} )
if ( auto cmp = strcmp( ${name}, rhs.${name} ); cmp != 0 ) if ( auto cmp = strcmp( ${name}, rhs.${name} ); cmp != 0 )
return ( cmp < 0 ) ? ${ordering}::less : ${ordering}::greater; return ( cmp < 0 ) ? ${ordering}::less : ${ordering}::greater;
)"; )";
spaceshipMembers += replaceWithMap( spaceshipMemberTemplate, { { "name", member.name }, { "ordering", spaceshipOrdering } } ); spaceshipMembers += replaceWithMap( spaceshipMemberTemplate, { { "name", member.name }, { "ordering", spaceshipOrdering } } );
}
else
{
assert( member.arraySizes.size() == 1 );
compareMembers += intro + "( strcmp( " + member.name + ", rhs." + member.name + " ) == 0 )";
static const std::string spaceshipMemberTemplate =
R"( if ( auto cmp = strcmp( ${name}, rhs.${name} ); cmp != 0 )
return ( cmp < 0 ) ? ${ordering}::less : ${ordering}::greater;
)";
spaceshipMembers += replaceWithMap( spaceshipMemberTemplate, { { "name", member.name }, { "ordering", spaceshipOrdering } } );
}
} }
else else
{ {
@ -10418,6 +10438,27 @@ std::string VulkanHppGenerator::generateStructCompareOperators( std::pair<std::s
replaceWithMap( spaceshipMemberTemplate, { { "count", member.lenExpressions[0] }, { "name", member.name }, { "ordering", spaceshipOrdering } } ); replaceWithMap( spaceshipMemberTemplate, { { "count", member.lenExpressions[0] }, { "name", member.name }, { "ordering", spaceshipOrdering } } );
} }
} }
else if ( !member.arraySizes.empty() && !member.lenExpressions.empty() )
{
nonDefaultCompare = true;
assert( ( member.arraySizes.size() == 1 ) && ( member.lenExpressions.size() == 1 ) );
assert( std::find_if( structData.second.members.begin(),
structData.second.members.end(),
[&member]( MemberData const & m ) { return m.name == member.lenExpressions[0]; } ) != structData.second.members.end() );
std::string type = member.type.type.starts_with( "Vk" ) ? member.type.compose( "VULKAN_HPP_NAMESPACE" ) : member.type.type;
static const std::string compareMemberTemplate = R"(( memcmp( ${name}, rhs.${name}, ${count} * sizeof( ${type} ) ) == 0 ))";
compareMembers += intro + replaceWithMap( compareMemberTemplate, { { "count", member.lenExpressions[0] }, { "name", member.name }, { "type", type } } );
static const std::string spaceshipMemberTemplate = R"( for ( size_t i = 0; i < ${count}; ++i )
{
if ( auto cmp = ${name}[i] <=> rhs.${name}[i]; cmp != 0 ) return cmp;
}
)";
spaceshipMembers += replaceWithMap( spaceshipMemberTemplate, { { "count", member.lenExpressions[0] }, { "name", member.name } } );
}
else else
{ {
// for all others, we use the operator== of that type // for all others, we use the operator== of that type
@ -10565,7 +10606,7 @@ std::string VulkanHppGenerator::generateStructConstructorsEnhanced( std::pair<st
bool listedArgument = false; bool listedArgument = false;
bool firstArgument = true; bool firstArgument = true;
bool arrayListed = false; bool arrayListed = false;
std::string templateHeader, sizeChecks; 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
@ -10579,18 +10620,21 @@ std::string VulkanHppGenerator::generateStructConstructorsEnhanced( std::pair<st
auto litit = lenIts.find( mit ); auto litit = lenIts.find( mit );
if ( litit != lenIts.end() ) if ( litit != lenIts.end() )
{ {
// len arguments just have an initalizer, from the ArrayProxyNoTemporaries size // len arguments just have an initalizer, from the array size
initializers += initializers +=
( firstArgument ? ": " : ", " ) + mit->name + "( " + generateLenInitializer( mit, litit, structData.second.mutualExclusiveLens ) + " )"; ( firstArgument ? ": " : ", " ) + 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 ) ) else if ( hasLen( *mit ) )
{ {
assert( mit->name.starts_with( "p" ) ); assert( mit->type.isPointer() || !mit->arraySizes.empty() );
std::string argumentName = startLowerCase( stripPrefix( mit->name, "p" ) ) + "_"; std::string argumentName = ( mit->type.isPointer() ? startLowerCase( stripPrefix( mit->name, "p" ) ) : mit->name ) + "_";
assert( mit->type.postfix.ends_with( "*" ) ); std::string argumentType = mit->type.compose( "VULKAN_HPP_NAMESPACE" );
std::string argumentType = trimEnd( stripPostfix( mit->type.compose( "VULKAN_HPP_NAMESPACE" ), "*" ) ); if ( mit->type.isPointer() )
{
argumentType = trimEnd( stripPostfix( argumentType, "*" ) );
}
if ( ( mit->type.type == "void" ) && ( argumentType.find( '*' ) == std::string::npos ) ) if ( ( mit->type.type == "void" ) && ( argumentType.find( '*' ) == std::string::npos ) )
{ {
// the argument after stripping one pointer is just void // the argument after stripping one pointer is just void
@ -10603,7 +10647,20 @@ std::string VulkanHppGenerator::generateStructConstructorsEnhanced( std::pair<st
} }
arguments += listedArgument ? ", " : ""; arguments += listedArgument ? ", " : "";
arguments += "VULKAN_HPP_NAMESPACE::ArrayProxyNoTemporaries<" + argumentType + "> const & " + argumentName; if ( mit->lenExpressions[0] == "null-terminated" )
{
assert( ( mit->type.type == "char" ) && ( mit->arraySizes.size() == 1 ) );
arguments += "std::string const & " + argumentName;
}
else if ( mit->arraySizes.empty() )
{
arguments += "VULKAN_HPP_NAMESPACE::ArrayProxyNoTemporaries<" + argumentType + "> const & " + argumentName;
}
else
{
assert( mit->arraySizes.size() == 1 );
arguments += "VULKAN_HPP_NAMESPACE::ArrayProxy<" + argumentType + "> const & " + argumentName;
}
if ( arrayListed ) if ( arrayListed )
{ {
arguments += " = {}"; arguments += " = {}";
@ -10611,7 +10668,26 @@ std::string VulkanHppGenerator::generateStructConstructorsEnhanced( std::pair<st
listedArgument = true; listedArgument = true;
arrayListed = true; arrayListed = true;
initializers += ( firstArgument ? ": " : ", " ) + mit->name + "( " + argumentName + ".data() )"; if ( mit->type.isPointer() )
{
initializers += ( firstArgument ? ": " : ", " ) + mit->name + "( " + argumentName + ".data() )";
}
else
{
assert( mit->arraySizes.size() == 1 );
static const std::string copyOpsTemplate = R"(
VULKAN_HPP_ASSERT( ${memberName}_.size() < ${arraySize} );
${copyOp}( ${memberName}, ${memberName}_.data(), ${arraySizeExpression} );)";
std::string arraySizeExpression = ( mit->lenExpressions[0] == "null-terminated" )
? ( "std::min( " + mit->name + "_.size(), " + mit->arraySizes[0] + " )" )
: ( mit->lenExpressions[0] + " * sizeof( " + argumentType + " )" );
copyOps += replaceWithMap( copyOpsTemplate,
{ { "arraySize", mit->arraySizes[0] },
{ "arraySizeExpression", arraySizeExpression },
{ "copyOp", mit->lenExpressions[0] == "null-terminated" ? "strncpy" : "memcpy" },
{ "memberName", mit->name } } );
}
} }
else else
{ {
@ -10639,12 +10715,13 @@ std::string VulkanHppGenerator::generateStructConstructorsEnhanced( std::pair<st
#if !defined( VULKAN_HPP_DISABLE_ENHANCED_MODE ) #if !defined( VULKAN_HPP_DISABLE_ENHANCED_MODE )
${templateHeader} ${structName}( ${arguments} ) ${templateHeader} ${structName}( ${arguments} )
${initializers} ${initializers}
{${sizeChecks}} {${sizeChecks}${copyOps}}
#endif /*VULKAN_HPP_DISABLE_ENHANCED_MODE*/ #endif /*VULKAN_HPP_DISABLE_ENHANCED_MODE*/
)"; )";
return replaceWithMap( constructorTemplate, return replaceWithMap( constructorTemplate,
{ { "arguments", arguments }, { { "arguments", arguments },
{ "copyOps", copyOps },
{ "initializers", initializers }, { "initializers", initializers },
{ "sizeChecks", sizeChecks }, { "sizeChecks", sizeChecks },
{ "structName", stripPrefix( structData.first, "Vk" ) }, { "structName", stripPrefix( structData.first, "Vk" ) },
@ -11228,45 +11305,81 @@ std::string VulkanHppGenerator::generateStructSetter( std::string const & struct
if ( hasLen( member ) ) if ( hasLen( member ) )
{ {
assert( member.name.front() == 'p' ); assert( member.type.isPointer() || !member.arraySizes.empty() );
std::string arrayName = startLowerCase( stripPrefix( member.name, "p" ) ); assert( !member.type.isPointer() || member.name.starts_with( "p" ) );
std::string arrayName = member.type.isPointer() ? startLowerCase( stripPrefix( member.name, "p" ) ) : member.name;
std::string lenName, lenValue; if ( member.lenExpressions[0] == "null-terminated" )
if ( member.lenExpressions[0] == "codeSize / 4" )
{ {
lenName = "codeSize"; assert( member.lenMembers.empty() && ( member.lenExpressions.size() == 1 ) && ( member.arraySizes.size() == 1 ) && ( member.type.type == "char" ) );
lenValue = arrayName + "_.size() * 4";
static const std::string setStringTemplate = R"(
#if !defined( VULKAN_HPP_DISABLE_ENHANCED_MODE )
${structureName} & set${ArrayName}( std::string const & ${arrayName}_ ) VULKAN_HPP_NOEXCEPT
{
VULKAN_HPP_ASSERT( ${arrayName}_.size() < ${arraySize} );
strncpy( ${arrayName}, ${arrayName}_.data(), std::min( ${arrayName}_.size(), ${arraySize} );
return *this;
}
#endif /*VULKAN_HPP_DISABLE_ENHANCED_MODE*/
)";
str += replaceWithMap( setStringTemplate,
{ { "arrayName", arrayName },
{ "ArrayName", startUpperCase( arrayName ) },
{ "arraySize", member.arraySizes[0] },
{ "structureName", structureName } } );
} }
else else
{ {
lenName = member.lenExpressions[0]; assert( ( member.lenExpressions[0] == member.lenMembers[0].first ) || ( member.lenExpressions[0] == "codeSize / 4" ) );
lenValue = arrayName + "_.size()"; std::string lenName, lenValue;
} if ( member.lenExpressions[0] == "codeSize / 4" )
{
lenName = "codeSize";
lenValue = arrayName + "_.size() * 4";
}
else
{
lenName = member.lenExpressions[0];
lenValue = arrayName + "_.size()";
}
assert( memberType.back() == '*' ); if ( member.type.isPointer() )
memberType = trimEnd( stripPostfix( memberType, "*" ) ); {
assert( memberType.back() == '*' );
memberType = trimEnd( stripPostfix( memberType, "*" ) );
}
std::string templateHeader; std::string templateHeader;
if ( ( member.type.type == "void" ) && ( memberType.find( '*' ) == std::string::npos ) ) if ( ( member.type.type == "void" ) && ( memberType.find( '*' ) == std::string::npos ) )
{ {
assert( templateHeader.empty() ); assert( templateHeader.empty() );
templateHeader = "template <typename T>\n "; templateHeader = "template <typename T>\n ";
const size_t pos = memberType.find( "void" ); const size_t pos = memberType.find( "void" );
assert( pos != std::string::npos ); assert( pos != std::string::npos );
memberType.replace( pos, strlen( "void" ), "T" ); memberType.replace( pos, strlen( "void" ), "T" );
lenValue += " * sizeof(T)"; lenValue += " * sizeof(T)";
} }
auto lenMember = findStructMemberIt( lenName, memberData ); auto lenMember = findStructMemberIt( lenName, memberData );
assert( lenMember != memberData.end() && lenMember->type.prefix.empty() && lenMember->type.postfix.empty() ); assert( lenMember != memberData.end() && lenMember->type.prefix.empty() && lenMember->type.postfix.empty() );
if ( lenMember->type.type != "size_t" ) if ( lenMember->type.type != "size_t" )
{ {
lenValue = "static_cast<" + lenMember->type.type + ">( " + lenValue + " )"; lenValue = "static_cast<" + lenMember->type.type + ">( " + lenValue + " )";
} }
static const std::string setArrayTemplate = R"( if ( !member.arraySizes.empty() )
{
assert( member.arraySizes.size() == 1 );
lenValue = "std::min( " + lenValue + ", " + member.arraySizes[0] + " )";
}
if ( member.type.isPointer() )
{
static const std::string setArrayTemplate = R"(
#if !defined( VULKAN_HPP_DISABLE_ENHANCED_MODE ) #if !defined( VULKAN_HPP_DISABLE_ENHANCED_MODE )
${templateHeader}${structureName} & set${ArrayName}( VULKAN_HPP_NAMESPACE::ArrayProxyNoTemporaries<${memberType}> const & ${arrayName}_ ) VULKAN_HPP_NOEXCEPT ${templateHeader}${structureName} & set${ArrayName}( VULKAN_HPP_NAMESPACE::ArrayProxyNoTemporaries<${memberType}> const & ${arrayName}_ ) VULKAN_HPP_NOEXCEPT
{ {
@ -11277,15 +11390,43 @@ std::string VulkanHppGenerator::generateStructSetter( std::string const & struct
#endif /*VULKAN_HPP_DISABLE_ENHANCED_MODE*/ #endif /*VULKAN_HPP_DISABLE_ENHANCED_MODE*/
)"; )";
str += replaceWithMap( setArrayTemplate, str += replaceWithMap( setArrayTemplate,
{ { "arrayName", arrayName }, { { "arrayName", arrayName },
{ "ArrayName", startUpperCase( arrayName ) }, { "ArrayName", startUpperCase( arrayName ) },
{ "lenName", lenName }, { "lenName", lenName },
{ "lenValue", lenValue }, { "lenValue", lenValue },
{ "memberName", member.name }, { "memberName", member.name },
{ "memberType", memberType }, { "memberType", memberType },
{ "structureName", structureName }, { "structureName", structureName },
{ "templateHeader", templateHeader } } ); { "templateHeader", templateHeader } } );
}
else
{
assert( member.arraySizes.size() == 1 );
static const std::string setArrayTemplate = R"(
#if !defined( VULKAN_HPP_DISABLE_ENHANCED_MODE )
${templateHeader}${structureName} & set${ArrayName}( VULKAN_HPP_NAMESPACE::ArrayProxy<${memberType}> const & ${arrayName}_ ) VULKAN_HPP_NOEXCEPT
{
VULKAN_HPP_ASSERT( ${arrayName}_.size() <= ${arraySize} );
${lenName} = ${lenValue};
memcpy( ${arrayName}, ${arrayName}_.data(), ${lenName} );
return *this;
}
#endif /*VULKAN_HPP_DISABLE_ENHANCED_MODE*/
)";
str += replaceWithMap( setArrayTemplate,
{ { "arrayName", arrayName },
{ "ArrayName", startUpperCase( arrayName ) },
{ "arraySize", member.arraySizes[0] },
{ "lenName", lenName },
{ "lenValue", lenValue },
{ "memberType", member.type.compose( "VULKAN_HPP_NAMESPACE" ) },
{ "structureName", structureName },
{ "templateHeader", templateHeader } } );
}
}
} }
} }
return str; return str;
@ -12249,8 +12390,9 @@ bool VulkanHppGenerator::handleRemovalType( std::string const & type, std::vecto
bool VulkanHppGenerator::hasLen( MemberData const & memberData ) const bool VulkanHppGenerator::hasLen( MemberData const & memberData ) const
{ {
assert( memberData.lenMembers.size() <= memberData.lenExpressions.size() ); assert( memberData.lenMembers.size() <= memberData.lenExpressions.size() );
return !memberData.lenMembers.empty() && ( ( memberData.lenExpressions[0] == memberData.lenMembers[0].first ) || return ( !memberData.lenMembers.empty() && ( ( memberData.lenExpressions[0] == memberData.lenMembers[0].first ) ||
( memberData.lenExpressions[0] == ( memberData.lenMembers[0].first + " / 4" ) ) ); ( memberData.lenExpressions[0] == ( memberData.lenMembers[0].first + " / 4" ) ) ) ) ||
( !memberData.lenExpressions.empty() && ( memberData.lenExpressions[0] == "null-terminated" ) && !memberData.arraySizes.empty() );
} }
bool VulkanHppGenerator::hasParentHandle( std::string const & handle, std::string const & parent ) const bool VulkanHppGenerator::hasParentHandle( std::string const & handle, std::string const & parent ) const

View File

@ -82,12 +82,17 @@ struct TypeInfo
bool isConstPointer() const noexcept bool isConstPointer() const noexcept
{ {
return ( prefix.find( "const" ) != std::string::npos ) && ( postfix.find( '*' ) != std::string::npos ); return isPointer() && ( prefix.find( "const" ) != std::string::npos );
} }
bool isNonConstPointer() const noexcept bool isNonConstPointer() const noexcept
{ {
return ( prefix.find( "const" ) == std::string::npos ) && ( postfix.find( '*' ) != std::string::npos ); return isPointer() && ( prefix.find( "const" ) == std::string::npos );
}
bool isPointer() const noexcept
{
return postfix.find( '*' ) != std::string::npos;
} }
bool isValue() const noexcept bool isValue() const noexcept