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