From 57517381a10255dad2b13a1aa8a180a6f7f7d6ce Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Sat, 20 Jun 2026 20:29:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(generator):=20=E4=BC=98=E5=8C=96=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E9=BB=98=E8=AE=A4=E5=80=BC=E7=94=9F=E6=88=90=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E6=94=AF=E6=8C=81=E6=95=B0=E7=BB=84=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 simpleType 判断逻辑以识别数组类型 - 实现空数组默认值的特殊处理机制 - 新增 hasNullDefault 和 useEmptyArrayDefault 判断条件 - 修改默认值生成流程以支持数组类型的 ZVAL_EMPTY_ARRAY - 添加静态类属性表达式测试用例验证功能 - 重构代码结构提高可读性和维护性 --- src/gen_stub.php | 33 +++++++++++++++++++------- tests/aot/static/static-prop-expr.phpt | 18 ++++++++++++++ 2 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 tests/aot/static/static-prop-expr.phpt diff --git a/src/gen_stub.php b/src/gen_stub.php index c314aba1..b3cbe908 100755 --- a/src/gen_stub.php +++ b/src/gen_stub.php @@ -3262,20 +3262,35 @@ class PropertyInfo extends VariableLike $code = "\n"; $propertyName = $this->name->getDeclarationName(); + $simpleType = $this->type !== null ? $this->type->tryToSimpleType() : null; + $hasNullDefault = $this->defaultValue instanceof Expr\ConstFetch + && $this->defaultValue->name->toLowerString() === 'null'; + $useEmptyArrayDefault = $this->defaultValue !== null + && !$this->defaultValue instanceof Expr\New_ + && !$hasNullDefault + && ( + $this->defaultValue instanceof Expr\Array_ + || ($simpleType !== null && $simpleType->isArray()) + ); - // New 操作作为属性的默认值,需编译器处理 gen_stub 作为 null 值 - if ($this->defaultValue === null || $this->defaultValue instanceof Expr\New_) { - $defaultValue = EvaluatedValue::null(); - } else { - $defaultValue = EvaluatedValue::createFromExpression($this->defaultValue, null, null, $allConstInfos); - if ($defaultValue->isUnknownConstValue || ($defaultValue->originatingConsts && $defaultValue->getCExpr() === null)) { - echo "Skipping code generation for property $this->name, because it has an unknown constant default value\n"; - return ""; + if (!$useEmptyArrayDefault) { + // New 操作作为属性的默认值,需编译器处理 gen_stub 作为 null 值 + if ($this->defaultValue === null || $this->defaultValue instanceof Expr\New_) { + $defaultValue = EvaluatedValue::null(); + } else { + $defaultValue = EvaluatedValue::createFromExpression($this->defaultValue, null, null, $allConstInfos); + if ($defaultValue->isUnknownConstValue || ($defaultValue->originatingConsts && $defaultValue->getCExpr() === null)) { + echo "Skipping code generation for property $this->name, because it has an unknown constant default value\n"; + return ""; + } } } $zvalName = "property_{$propertyName}_default_value"; - if ($this->defaultValue === null && $this->type !== null) { + if ($useEmptyArrayDefault) { + $code .= "\tzval $zvalName;\n"; + $code .= "\tZVAL_EMPTY_ARRAY(&$zvalName);\n"; + } elseif ($this->defaultValue === null && $this->type !== null) { $code .= "\tzval $zvalName;\n"; if ($this->flags & Modifiers::READONLY || $this->classFlags & Modifiers::READONLY) { $code .= "\tZVAL_UNDEF(&$zvalName);\n"; diff --git a/tests/aot/static/static-prop-expr.phpt b/tests/aot/static/static-prop-expr.phpt new file mode 100644 index 00000000..a953690b --- /dev/null +++ b/tests/aot/static/static-prop-expr.phpt @@ -0,0 +1,18 @@ +--TEST-- +Static Class Property Read/Write Test +--FILE-- + __DIR__ . '/../../A', + ]; +} + +function main(): void +{ + var_dump(A::$a['A']); +} +?> +--EXPECTF-- +string(%d) "%s/aot/static/../../A" \ No newline at end of file