From aa6be88c6ca228fea78b78c83c6d37258239d898 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 10 Feb 2026 20:31:53 +0800 Subject: [PATCH] =?UTF-8?q?feat(php):=20=E6=B7=BB=E5=8A=A0=E5=AF=B9PHP?= =?UTF-8?q?=E6=9E=9A=E4=B8=BE=E7=B1=BB=E5=9E=8B=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在ClassDef实体中添加enum属性用于标识枚举类 - 修改编译器基类以支持枚举常量访问的特殊处理 - 移除旧的枚举示例文件并添加新的测试用例 - 更新翻译器以识别和标记枚举类型的类定义 --- src/Php/CompilerBase.php | 4 ++++ src/Php/Entity/ClassDef.php | 1 + src/Php/Translator.php | 5 +++++ examples/enum1.php => tests/aot/enum1.phpt | 11 ++++++++++- 4 files changed, 20 insertions(+), 1 deletion(-) rename examples/enum1.php => tests/aot/enum1.phpt (89%) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 21241274..f9f05a1d 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -3203,6 +3203,10 @@ class CompilerBase extends \PhpAot\Core\Translator if ($classDef->hasConstant($const)) { return $classDef->getConstant($const)->value; } + if ($classDef->enum) { + $ce = $this->getClassEntryPtr($class); + return 'php::getEnumCase(' . $ce . ', ' . $this->getLiteralString($const) . ')'; + } } $ce = $this->getClassEntryPtr($class); return 'php::constant(' . $ce . ', ' . $this->getLiteralString($const) . ')'; diff --git a/src/Php/Entity/ClassDef.php b/src/Php/Entity/ClassDef.php index 8018b8bc..d91a5df0 100644 --- a/src/Php/Entity/ClassDef.php +++ b/src/Php/Entity/ClassDef.php @@ -27,6 +27,7 @@ class ClassDef extends ClassLikeDef public array $implements = []; public string $extends = ''; public bool $requireCtor = false; + public bool $enum = false; public int $flags; public function __construct(string $name, int $flags, string $namespace = '') diff --git a/src/Php/Translator.php b/src/Php/Translator.php index c07afe59..cee79e9a 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -660,7 +660,12 @@ class Translator extends Preprocessor $flags = $class->flags; $extends = $class->extends; } + $this->classDef = new ClassDef($this->class, $flags, $this->namespace); + if ($class instanceof Node\Stmt\Enum_) { + $this->classDef->enum = true; + } + if ($extends) { $this->classDef->extends = $this->parseIdentifier($class->extends); if (isset($this->classes[$this->classDef->extends])) { diff --git a/examples/enum1.php b/tests/aot/enum1.phpt similarity index 89% rename from examples/enum1.php rename to tests/aot/enum1.phpt index b59e3119..71af4f6f 100644 --- a/examples/enum1.php +++ b/tests/aot/enum1.phpt @@ -1,3 +1,6 @@ +--TEST-- +enum 2 +--FILE-- color() . "\n"; echo Suit::Clubs->color() . "\n"; echo Suit::Spades->color() . "\n"; -} \ No newline at end of file +} +?> +--EXPECT-- +Red +Red +Black +Black