feat(php): 添加对PHP枚举类型的支持

- 在ClassDef实体中添加enum属性用于标识枚举类
- 修改编译器基类以支持枚举常量访问的特殊处理
- 移除旧的枚举示例文件并添加新的测试用例
- 更新翻译器以识别和标记枚举类型的类定义
pull/1/head
韩天峰 7 months ago
parent 9de6ca6a41
commit aa6be88c6c
  1. 4
      src/Php/CompilerBase.php
  2. 1
      src/Php/Entity/ClassDef.php
  3. 5
      src/Php/Translator.php
  4. 9
      tests/aot/enum1.phpt

@ -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) . ')';

@ -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 = '')

@ -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])) {

@ -1,3 +1,6 @@
--TEST--
enum 2
--FILE--
<?php
interface Colorful {
public function color(): string;
@ -24,3 +27,9 @@ function main()
echo Suit::Clubs->color() . "\n";
echo Suit::Spades->color() . "\n";
}
?>
--EXPECT--
Red
Red
Black
Black
Loading…
Cancel
Save