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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 1 addition & 4 deletions Zend/zend_constants_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Zend/zend_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,12 @@ EMPTY_SWITCH_DEFAULT_CASE()
_(ZEND_STR_SINCE, "since") \
_(ZEND_STR_GET, "get") \
_(ZEND_STR_SET, "set") \
_(ZEND_STR_8_DOT_0, "8.0") \
_(ZEND_STR_8_DOT_1, "8.1") \
_(ZEND_STR_8_DOT_2, "8.2") \
_(ZEND_STR_8_DOT_3, "8.3") \
_(ZEND_STR_8_DOT_4, "8.4") \
_(ZEND_STR_8_DOT_5, "8.5") \


typedef enum _zend_known_string_id {
Expand Down
221 changes: 129 additions & 92 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 @@ -3065,8 +3058,90 @@ class PropertyInfo extends VariableLike
"username" => "ZEND_STR_USERNAME",
"password" => "ZEND_STR_PASSWORD",
"clone" => "ZEND_STR_CLONE",
'8.0' => 'ZEND_STR_8_DOT_0',
'8.1' => 'ZEND_STR_8_DOT_1',
'8.2' => 'ZEND_STR_8_DOT_2',
'8.3' => 'ZEND_STR_8_DOT_3',
'8.4' => 'ZEND_STR_8_DOT_4',
'8.5' => 'ZEND_STR_8_DOT_5',
];

/**
* 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
* @param bool $interned
* @return string[]
*/
public static function getString(
string $varName,
string $content,
?int $minPHPCompatibility,
bool $interned = false
): array {
// Generally strings will not be known
$initFn = $interned ? 'zend_string_init_interned' : 'zend_string_init';
$result = [
"\tzend_string *$varName = $initFn(\"$content\", sizeof(\"$content\") - 1, 1);\n",
$varName,
"\tzend_string_release($varName);\n"
];
// For attribute values that are not freed
if ($varName === '') {
$result[0] = "$initFn(\"$content\", sizeof(\"$content\") - 1, 1);\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 +3222,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 +3261,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 Expand Up @@ -3326,40 +3351,52 @@ public function __construct(string $class, array $args) {

/** @param array<string, ConstInfo> $allConstInfos */
public function generateCode(string $invocation, string $nameSuffix, array $allConstInfos, ?int $phpVersionIdMinimumCompatibility): string {
$php82MinimumCompatibility = $phpVersionIdMinimumCompatibility === null || $phpVersionIdMinimumCompatibility >= PHP_82_VERSION_ID;
$php84MinimumCompatibility = $phpVersionIdMinimumCompatibility === null || $phpVersionIdMinimumCompatibility >= PHP_84_VERSION_ID;
/* see ZEND_KNOWN_STRINGS in Zend/strings.h */
$knowns = [
"message" => "ZEND_STR_MESSAGE",
];
if ($php82MinimumCompatibility) {
$knowns["SensitiveParameter"] = "ZEND_STR_SENSITIVEPARAMETER";
}
if ($php84MinimumCompatibility) {
$knowns["Deprecated"] = "ZEND_STR_DEPRECATED_CAPITALIZED";
$knowns["since"] = "ZEND_STR_SINCE";
}

$code = "\n";
$escapedAttributeName = strtr($this->class, '\\', '_');
if (isset($knowns[$escapedAttributeName])) {
$code .= "\t" . ($this->args ? "zend_attribute *attribute_{$escapedAttributeName}_$nameSuffix = " : "") . "$invocation, ZSTR_KNOWN({$knowns[$escapedAttributeName]}), " . count($this->args) . ");\n";
} else {
$code .= "\tzend_string *attribute_name_{$escapedAttributeName}_$nameSuffix = zend_string_init_interned(\"" . addcslashes($this->class, "\\") . "\", sizeof(\"" . addcslashes($this->class, "\\") . "\") - 1, 1);\n";
$code .= "\t" . ($this->args ? "zend_attribute *attribute_{$escapedAttributeName}_$nameSuffix = " : "") . "$invocation, attribute_name_{$escapedAttributeName}_$nameSuffix, " . count($this->args) . ");\n";
$code .= "\tzend_string_release(attribute_name_{$escapedAttributeName}_$nameSuffix);\n";
}
[$stringInit, $nameCode, $stringRelease] = StringBuilder::getString(
"attribute_name_{$escapedAttributeName}_$nameSuffix",
addcslashes($this->class, "\\"),
$phpVersionIdMinimumCompatibility,
true
);
$code = "\n";
$code .= $stringInit;
$code .= "\t" . ($this->args ? "zend_attribute *attribute_{$escapedAttributeName}_$nameSuffix = " : "") . "$invocation, $nameCode, " . count($this->args) . ");\n";
$code .= $stringRelease;

foreach ($this->args as $i => $arg) {
$value = EvaluatedValue::createFromExpression($arg->value, null, null, $allConstInfos);
$zvalName = "attribute_{$escapedAttributeName}_{$nameSuffix}_arg$i";
$code .= $value->initializeZval($zvalName);
$code .= "\tZVAL_COPY_VALUE(&attribute_{$escapedAttributeName}_{$nameSuffix}->args[$i].value, &$zvalName);\n";
$initValue = '';
if ($arg->value instanceof Node\Scalar\String_) {
$strVal = $arg->value->value;
[$strInit, $strUse, $strRelease] = StringBuilder::getString(
'unused',
$strVal,
$phpVersionIdMinimumCompatibility
);
if ($strInit === '') {
$initValue = "\tZVAL_STR(&attribute_{$escapedAttributeName}_{$nameSuffix}->args[$i].value, $strUse);\n";
}
}
if ($initValue === '') {
$value = EvaluatedValue::createFromExpression($arg->value, null, null, $allConstInfos);
$zvalName = "attribute_{$escapedAttributeName}_{$nameSuffix}_arg$i";
$code .= $value->initializeZval($zvalName);
$code .= "\tZVAL_COPY_VALUE(&attribute_{$escapedAttributeName}_{$nameSuffix}->args[$i].value, &$zvalName);\n";
} else {
$code .= $initValue;
}
if ($arg->name) {
if (isset($knowns[$arg->name->name])) {
$code .= "\tattribute_{$escapedAttributeName}_{$nameSuffix}->args[$i].name = ZSTR_KNOWN({$knowns[$arg->name->name]});\n";
[$stringInit, $nameCode, $stringRelease] = StringBuilder::getString(
"",
$arg->name->name,
$phpVersionIdMinimumCompatibility,
true
);
if ($stringInit === '') {
$nameCode .= ";\n";
} else {
$code .= "\tattribute_{$escapedAttributeName}_{$nameSuffix}->args[$i].name = zend_string_init_interned(\"{$arg->name->name}\", sizeof(\"{$arg->name->name}\") - 1, 1);\n";
$nameCode = $stringInit;
}
$code .= "\tattribute_{$escapedAttributeName}_{$nameSuffix}->args[$i].name = $nameCode";
}
}
return $code;
Expand Down
5 changes: 1 addition & 4 deletions ext/curl/curl_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading