Skip to content

Commit 3b2a0e7

Browse files
committed
Create AttributeInfo class instead of array tuple
Signed-off-by: Bob Weinand <bobwei9@hotmail.com>
1 parent 6cd4a63 commit 3b2a0e7

File tree

5 files changed

+49
-30
lines changed

5 files changed

+49
-30
lines changed

Zend/zend_attributes_arginfo.h

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Zend/zend_builtin_functions_arginfo.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/gen_stub.php

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,6 +2257,38 @@ public function getDeclaration(iterable $allConstInfos): string {
22572257
}
22582258
}
22592259

2260+
class AttributeInfo {
2261+
/** @var string */
2262+
public $class;
2263+
/** @var \PhpParser\Node\Arg[] */
2264+
public $args;
2265+
2266+
/** @param \PhpParser\Node\Arg[] $args */
2267+
public function __construct(string $class, array $args) {
2268+
$this->class = $class;
2269+
$this->args = $args;
2270+
}
2271+
2272+
/** @param iterable<ConstInfo> $allConstInfos */
2273+
public function generateCode(string $invocation, string $nameSuffix, iterable $allConstInfos): string {
2274+
$code = "\n";
2275+
$escapedAttributeName = strtr($this->class, '\\', '_');
2276+
$code .= "\tzend_string *attribute_name_{$escapedAttributeName}_$nameSuffix = zend_string_init(\"" . addcslashes($this->class, "\\") . "\", sizeof(\"" . addcslashes($this->class, "\\") . "\") - 1, 1);\n";
2277+
$code .= "\t" . ($this->args ? "zend_attribute *attribute_{$escapedAttributeName}_$nameSuffix = " : "") . "$invocation, attribute_name_{$escapedAttributeName}_$nameSuffix, " . count($this->args) . ");\n";
2278+
$code .= "\tzend_string_release(attribute_name_{$escapedAttributeName}_$nameSuffix);\n";
2279+
foreach ($this->args as $i => $arg) {
2280+
$value = EvaluatedValue::createFromExpression($arg->value, null, null, $allConstInfos);
2281+
$zvalName = "attribute_{$escapedAttributeName}_{$nameSuffix}_arg$i";
2282+
$code .= $value->initializeZval($zvalName, $allConstInfos);
2283+
$code .= "\tZVAL_COPY_VALUE(&attribute_{$escapedAttributeName}_{$nameSuffix}->args[$i].value, &$zvalName);\n";
2284+
if ($arg->name) {
2285+
$code .= "\tattribute_{$escapedAttributeName}_{$nameSuffix}->args[$i].name = zend_string_init(\"{$arg->name->name}\", sizeof(\"{$arg->name->name}\") - 1, 1);\n";
2286+
}
2287+
}
2288+
return $code;
2289+
}
2290+
}
2291+
22602292
class ClassInfo {
22612293
/** @var Name */
22622294
public $name;
@@ -2272,7 +2304,7 @@ class ClassInfo {
22722304
public $isDeprecated;
22732305
/** @var bool */
22742306
public $isStrictProperties;
2275-
/** @var array{0: string, 1: \PhpParser\Node\Arg[]}[] */
2307+
/** @var AttributeInfo[] */
22762308
public $attributes;
22772309
/** @var bool */
22782310
public $isNotSerializable;
@@ -2292,7 +2324,7 @@ class ClassInfo {
22922324
public $cond;
22932325

22942326
/**
2295-
* @param array{0: string, 1: \PhpParser\Node\Arg[]}[] $attributes
2327+
* @param AttributeInfo[] $attributes
22962328
* @param Name[] $extends
22972329
* @param Name[] $implements
22982330
* @param ConstInfo[] $constInfos
@@ -2414,7 +2446,9 @@ function (Name $item) {
24142446
$code .= $property->getDeclaration($allConstInfos);
24152447
}
24162448

2417-
$code .= generateAttributesCode($this->attributes, "zend_add_class_attribute(class_entry", "class_$escapedName", $allConstInfos);
2449+
foreach ($this->attributes as $attribute) {
2450+
$code .= $attribute->generateCode("zend_add_class_attribute(class_entry", "class_$escapedName", $allConstInfos);
2451+
}
24182452

24192453
if ($attributeInitializationCode = generateAttributeInitialization($this->funcInfos, $this->cond)) {
24202454
$code .= "\n" . $attributeInitializationCode;
@@ -2459,8 +2493,8 @@ private function getFlagsAsString(): string
24592493
$flags[] = "ZEND_ACC_NO_DYNAMIC_PROPERTIES";
24602494
}
24612495

2462-
foreach ($this->attributes as list($name)) {
2463-
if ($name === "AllowDynamicProperties") {
2496+
foreach ($this->attributes as $attr) {
2497+
if ($attr->class === "AllowDynamicProperties") {
24642498
$flags[] = "ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES";
24652499
}
24662500
}
@@ -2980,30 +3014,6 @@ public function getVariableName(): string {
29803014
}
29813015
}
29823016

2983-
/**
2984-
* @param array{0: string, 1: \PhpParser\Node\Arg[]}[] $attributes
2985-
* @param iterable<ConstInfo> $allConstInfos
2986-
*/
2987-
function generateAttributesCode(array $attributes, string $invocation, string $nameSuffix, iterable $allConstInfos): string {
2988-
$code = "";
2989-
foreach ($attributes as list($name, $args)) {
2990-
$escapedAttributeName = strtr($name, '\\', '_');
2991-
$code .= "\tzend_string *attribute_name_{$escapedAttributeName}_$nameSuffix = zend_string_init(\"" . addcslashes($name, "\\") . "\", sizeof(\"" . addcslashes($name, "\\") . "\") - 1, 1);\n";
2992-
$code .= "\t" . ($args ? "zend_attribute *attribute_{$escapedAttributeName}_$nameSuffix = " : "") . "$invocation, attribute_name_{$escapedAttributeName}_$nameSuffix, " . count($args) . ");\n";
2993-
$code .= "\tzend_string_release(attribute_name_{$escapedAttributeName}_$nameSuffix);\n";
2994-
foreach ($args as $i => $arg) {
2995-
$value = EvaluatedValue::createFromExpression($arg->value, null, null, $allConstInfos);
2996-
$zvalName = "attribute_{$escapedAttributeName}_{$nameSuffix}_arg$i";
2997-
$code .= $value->initializeZval($zvalName, $allConstInfos);
2998-
$code .= "\tZVAL_COPY_VALUE(&attribute_{$escapedAttributeName}_{$nameSuffix}->args[$i].value, &$zvalName);\n";
2999-
if ($arg->name) {
3000-
$code .= "\tattribute_{$escapedAttributeName}_{$nameSuffix}->args[$i].name = zend_string_init(\"{$arg->name->name}\", sizeof(\"{$arg->name->name}\") - 1, 1);\n";
3001-
}
3002-
}
3003-
}
3004-
return $code;
3005-
}
3006-
30073017
/** @return DocCommentTag[] */
30083018
function parseDocComment(DocComment $comment): array {
30093019
$commentText = substr($comment->getText(), 2, -2);
@@ -3330,7 +3340,7 @@ function parseClass(
33303340

33313341
foreach ($class->attrGroups as $attrGroup) {
33323342
foreach ($attrGroup->attrs as $attr) {
3333-
$attributes[] = [$attr->name->toString(), $attr->args];
3343+
$attributes[] = new AttributeInfo($attr->name->toString(), $attr->args);
33343344
switch ($attr->name->toString()) {
33353345
case 'AllowDynamicProperties':
33363346
$allowsDynamicProperties = true;

ext/oci8/oci8_arginfo.h

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/zend_test/test_arginfo.h

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)