From 96ecb8aae0d221afcfaec54b2912cd230fa2ef82 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 27 Jan 2026 19:31:33 +0800 Subject: [PATCH] =?UTF-8?q?feat(php):=20=E6=94=AF=E6=8C=81=E8=81=94?= =?UTF-8?q?=E5=90=88=E7=B1=BB=E5=9E=8B=E5=B9=B6=E4=BC=98=E5=8C=96=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E5=99=A8=E7=8A=B6=E6=80=81=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 UnionType 支持以处理 PHP 联合类型语法 - 实现 resetClass 和 resetFunction 方法来重置编译器状态 - 在适当位置调用状态重置方法确保编译器状态一致性 - 修复类方法定义中的访问修饰符处理逻辑 - 更新示例文件以演示联合类型功能 - 调整项目配置文件中的源码路径设置 --- examples/fcall.php | 10 ++++++---- examples/prime/project.yml | 2 +- src/Php/CompilerBase.php | 13 +++++++++++-- src/Php/Translator.php | 10 ++++++++-- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/examples/fcall.php b/examples/fcall.php index ad9e5614..8b160626 100644 --- a/examples/fcall.php +++ b/examples/fcall.php @@ -4,7 +4,7 @@ function foo(int $a, int $b): int { } class A { - function foo(int $a, int $b): int + function foo(int|float $a, int|float $b): int { return $a + $b; } @@ -13,6 +13,8 @@ class A { //foo(1, 2); //max(1, 2, 3); - -$o = new A; -$o->foo(1, 2); \ No newline at end of file +function main() +{ + $o = new A; + $o->foo(1, 2); +} diff --git a/examples/prime/project.yml b/examples/prime/project.yml index 48bd6ce7..a5c1099f 100644 --- a/examples/prime/project.yml +++ b/examples/prime/project.yml @@ -8,6 +8,6 @@ ldflags: | -L/usr/lib/x86_64-linux-gnu/ -lssl sources: - - php-src* + - php-src - ./cpp-src - main.php \ No newline at end of file diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index a01977eb..97f4172f 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -12,6 +12,7 @@ use PhpParser\Error; use PhpParser\Node\NullableType; use PhpParser\Node\Scalar\MagicConst; use PhpParser\Node\Stmt\Foreach_; +use PhpParser\Node\UnionType; use PhpParser\NodeAbstract; use PhpParser\NodeFinder; use PhpParser\NodeTraverser; @@ -244,6 +245,14 @@ class CompilerBase extends \PhpAot\Core\Translator $this->objectWrappers = []; $this->tmpVarIndex = 0; $this->inLoop = false; + $this->function = ''; + } + + protected function resetClass(): void + { + $this->class = ''; + $this->interface = ''; + $this->method = ''; } protected function resetFile(): void @@ -358,7 +367,7 @@ class CompilerBase extends \PhpAot\Core\Translator $code .= $stmts; $code .= "}\n"; - $this->function = ''; + $this->resetFunction(); return $code; } @@ -1166,7 +1175,7 @@ class CompilerBase extends \PhpAot\Core\Translator if ($type == null) { return self::TYPE_VAR; } - if ($type instanceof NullableType) { + if ($type instanceof NullableType or $type instanceof UnionType) { return self::TYPE_VAR; } $name = $type->name; diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 51b3fb3d..f695822e 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -277,6 +277,8 @@ class Translator extends Preprocessor $stmts = $traverser->traverse($ast); $this->resetFile(); + $this->resetFunction(); + $this->resetClass(); $this->resetNamespace(); $cppCode = ''; @@ -643,7 +645,7 @@ class Translator extends Preprocessor } } $code = $this->genNativeMethod($methodCodes); - $this->class = ''; + $this->resetClass(); return $code; } @@ -928,7 +930,11 @@ class Translator extends Preprocessor $name = $this->getMethodName($v); $this->method = $name; $methodCodes[$name] = $this->parseFunction($v); - $methodDef = new MethodDef($v->flags, $name, $this->functionDef); + $flags = $v->flags; + if (!($flags & Modifiers::PRIVATE) and !($flags & Modifiers::PROTECTED)) { + $flags |= Modifiers::PUBLIC; + } + $methodDef = new MethodDef($flags, $name, $this->functionDef); $this->checkRequiredArgNum($name, $methodDef, $v); $this->classDef->methods[$name] = $methodDef; $this->method = '';