From 403991357e0b8611fb1d5fd091f4dc6f116ddebf Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Fri, 5 Jun 2026 14:37:51 +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=E4=BB=A3=E7=A0=81=E7=94=9F?= =?UTF-8?q?=E6=88=90=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 替换原有的 ZVAL_UNDEF 初始化方式为类型特定的默认值设置 - 添加 getTypeDefaultValueCode 方法处理各种数据类型的默认值 - 支持整型、浮点型、布尔型、字符串、数组等类型的默认值初始化 - 为测试用例添加对象整型属性优化功能 - 确保可空类型正确初始化为 NULL 值 --- src/gen_stub.php | 29 ++++++++++++++++++- .../aot/optimizations/object-int-prop-02.phpt | 22 ++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 tests/aot/optimizations/object-int-prop-02.phpt diff --git a/src/gen_stub.php b/src/gen_stub.php index d6746cc7..06dffb0e 100755 --- a/src/gen_stub.php +++ b/src/gen_stub.php @@ -3257,7 +3257,8 @@ class PropertyInfo extends VariableLike $zvalName = "property_{$propertyName}_default_value"; if ($this->defaultValue === null && $this->type !== null) { - $code .= "\tzval $zvalName;\n\tZVAL_UNDEF(&$zvalName);\n"; + $code .= "\tzval $zvalName;\n"; + $code .= $this->getTypeDefaultValueCode($zvalName); } else { $code .= $defaultValue->initializeZval($zvalName, varName: $this->name->__toString()); } @@ -3344,6 +3345,32 @@ class PropertyInfo extends VariableLike $fieldsynopsisElement->appendChild($doc->createElement("modifier", "readonly")); } } + + private function getTypeDefaultValueCode(string $zvalName): string + { + if ($this->type->isNullable()) { + return "\tZVAL_NULL(&$zvalName);\n"; + } + $simpleType = $this->type->tryToSimpleType(); + if ($simpleType !== null) { + if ($simpleType->isInt()) { + return "\tZVAL_LONG(&$zvalName, 0);\n"; + } + if ($simpleType->isFloat()) { + return "\tZVAL_DOUBLE(&$zvalName, 0.0);\n"; + } + if ($simpleType->isBool()) { + return "\tZVAL_FALSE(&$zvalName);\n"; + } + if ($simpleType->isString()) { + return "\tZVAL_EMPTY_STRING(&$zvalName);\n"; + } + if ($simpleType->isArray()) { + return "\tZVAL_EMPTY_ARRAY(&$zvalName);\n"; + } + } + return "\tZVAL_NULL(&$zvalName);\n"; + } } class EnumCaseInfo { diff --git a/tests/aot/optimizations/object-int-prop-02.phpt b/tests/aot/optimizations/object-int-prop-02.phpt new file mode 100644 index 00000000..6634adc7 --- /dev/null +++ b/tests/aot/optimizations/object-int-prop-02.phpt @@ -0,0 +1,22 @@ +--TEST-- +SSA: int +--FILE-- +b += 2; + } + } +} +function main(): void { + $o = new Foo(); + $o->assign_add_prop(100000); + var_dump($o->b); +} +?> +--EXPECT-- +int(200000) \ No newline at end of file