Skip to content

Arginfo: add and use known strings for attributes #19075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 15, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gen_stub: add StringBuilder class for managing known strings
Split out from the PropertyInfo class so that known strings can also be used
for attributes in a follow-up commit.
  • Loading branch information
DanielEScherzer committed Jul 8, 2025
commit adc9bcf9b1dbe857935434df01f815305ebd7269
138 changes: 75 additions & 63 deletions build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2955,14 +2955,7 @@ protected function addModifiersToFieldSynopsis(DOMDocument $doc, DOMElement $fie
}
}

class PropertyInfo extends VariableLike
{
private /* readonly */ int $classFlags;
public /* readonly */ PropertyName $name;
private /* readonly */ ?Expr $defaultValue;
private /* readonly */ ?string $defaultValueString;
private /* readonly */ bool $isDocReadonly;
private /* readonly */ bool $isVirtual;
class StringBuilder {

// Map possible variable names to the known string constant, see
// ZEND_KNOWN_STRINGS
Expand Down Expand Up @@ -3067,6 +3060,75 @@ class PropertyInfo extends VariableLike
"clone" => "ZEND_STR_CLONE",
];

/**
* Get an array of three strings:
* - declaration of zend_string, if needed, or empty otherwise
* - usage of that zend_string, or usage with ZSTR_KNOWN()
* - freeing the zend_string, if needed
*
* @param string $varName
* @param string $strContent
* @param ?int $minPHPCompatibility
* @return string[]
*/
public static function getString(
string $varName,
string $content,
?int $minPHPCompatibility
): array {
// Generally strings will not be known
$result = [
"\tzend_string *$varName = zend_string_init(\"$content\", sizeof(\"$content\") - 1, 1);\n",
$varName,
"\tzend_string_release($varName);\n"
];
// If not set, use the current latest version
$allVersions = ALL_PHP_VERSION_IDS;
$minPhp = $minPHPCompatibility ?? end($allVersions);
if ($minPhp < PHP_80_VERSION_ID) {
// No known strings in 7.0
return $result;
}
$include = self::PHP_80_KNOWN;
switch ($minPhp) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why no match?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because gen_stub supports running on PHP 7.4, I have a todo to update that eventually

case PHP_85_VERSION_ID:
$include = array_merge($include, self::PHP_85_KNOWN);
// Intentional fall through

case PHP_84_VERSION_ID:
$include = array_merge($include, self::PHP_84_KNOWN);
// Intentional fall through

case PHP_83_VERSION_ID:
case PHP_82_VERSION_ID:
$include = array_merge($include, self::PHP_82_KNOWN);
// Intentional fall through

case PHP_81_VERSION_ID:
$include = array_merge($include, self::PHP_81_KNOWN);
break;
}
if (array_key_exists($content, $include)) {
$knownStr = $include[$content];
return [
'',
"ZSTR_KNOWN($knownStr)",
'',
];
}
return $result;
}
}

class PropertyInfo extends VariableLike
{
private /* readonly */ int $classFlags;
public /* readonly */ PropertyName $name;
private /* readonly */ ?Expr $defaultValue;
private /* readonly */ ?string $defaultValueString;
private /* readonly */ bool $isDocReadonly;
private /* readonly */ bool $isVirtual;

/**
* @param AttributeInfo[] $attributes
*/
Expand Down Expand Up @@ -3147,7 +3209,11 @@ public function getDeclaration(array $allConstInfos): string {
$code .= $defaultValue->initializeZval($zvalName);
}

[$stringInit, $nameCode, $stringRelease] = $this->getString($propertyName);
[$stringInit, $nameCode, $stringRelease] = StringBuilder::getString(
"property_{$propertyName}_name",
$propertyName,
$this->phpVersionIdMinimumCompatibility
);
$code .= $stringInit;

if ($this->exposedDocComment) {
Expand Down Expand Up @@ -3182,60 +3248,6 @@ public function getDeclaration(array $allConstInfos): string {
return $code;
}

/**
* Get an array of three strings:
* - declaration of zend_string, if needed, or empty otherwise
* - usage of that zend_string, or usage with ZSTR_KNOWN()
* - freeing the zend_string, if needed
*
* @param string $propName
* @return string[]
*/
private function getString(string $propName): array {
// Generally strings will not be known
$nameCode = "property_{$propName}_name";
$result = [
"\tzend_string *$nameCode = zend_string_init(\"$propName\", sizeof(\"$propName\") - 1, 1);\n",
$nameCode,
"\tzend_string_release($nameCode);\n"
];
// If not set, use the current latest version
$allVersions = ALL_PHP_VERSION_IDS;
$minPhp = $this->phpVersionIdMinimumCompatibility ?? end($allVersions);
if ($minPhp < PHP_80_VERSION_ID) {
// No known strings in 7.0
return $result;
}
$include = self::PHP_80_KNOWN;
switch ($minPhp) {
case PHP_85_VERSION_ID:
$include = array_merge($include, self::PHP_85_KNOWN);
// Intentional fall through

case PHP_84_VERSION_ID:
$include = array_merge($include, self::PHP_84_KNOWN);
// Intentional fall through

case PHP_83_VERSION_ID:
case PHP_82_VERSION_ID:
$include = array_merge($include, self::PHP_82_KNOWN);
// Intentional fall through

case PHP_81_VERSION_ID:
$include = array_merge($include, self::PHP_81_KNOWN);
break;
}
if (array_key_exists($propName, $include)) {
$knownStr = $include[$propName];
return [
'',
"ZSTR_KNOWN($knownStr)",
'',
];
}
return $result;
}

/**
* @return array<int, string[]>
*/
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy