feat(generator): 优化属性默认值生成逻辑支持数组类型

- 添加 simpleType 判断逻辑以识别数组类型
- 实现空数组默认值的特殊处理机制
- 新增 hasNullDefault 和 useEmptyArrayDefault 判断条件
- 修改默认值生成流程以支持数组类型的 ZVAL_EMPTY_ARRAY
- 添加静态类属性表达式测试用例验证功能
- 重构代码结构提高可读性和维护性
pull/3/head
韩天峰 2 months ago
parent dc5c90044b
commit 57517381a1
  1. 33
      src/gen_stub.php
  2. 18
      tests/aot/static/static-prop-expr.phpt

@ -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";

@ -0,0 +1,18 @@
--TEST--
Static Class Property Read/Write Test
--FILE--
<?php
final class A
{
public static array $a = [
'A' => __DIR__ . '/../../A',
];
}
function main(): void
{
var_dump(A::$a['A']);
}
?>
--EXPECTF--
string(%d) "%s/aot/static/../../A"
Loading…
Cancel
Save