From e4505c2ef1ca144060d71c8f4561befa00769c2c Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 12 Jan 2026 19:24:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E9=87=8D=E6=9E=84=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=92=8C=E6=96=B9=E6=B3=95=E5=AE=9A=E4=B9=89=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改 parseFunctionDeclaration 方法参数类型,支持 Function_ 和 ClassMethod - 简化函数定义创建流程,直接在构造函数中初始化 name 和 returnType - 更新 parseParams 方法接受 FunctionDef 参数,避免使用全局变量 - 重命名格式化提示信息从 formatting 为 format - 重构 FunctionDef 类添加构造函数并调整属性初始化 - 修改 MethodDef 类结构,使用 FunctionDef 对象替代单独的 returnType 属性 - 更新 Translator 中的方法调用以适配新的 MethodDef 结构 - 调整类方法解析流程以兼容新的数据结构 --- src/Php/CompilerBase.php | 33 ++++++++++++++------------------- src/Php/FunctionDef.php | 11 +++++++++-- src/Php/MethodDef.php | 22 ++++++++++++++++------ src/Php/Translator.php | 8 +++----- 4 files changed, 42 insertions(+), 32 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 24a7bcd3..8fd7666f 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -233,21 +233,16 @@ class CompilerBase extends \PhpAot\Core\Translator return implode(self::NAMESPACE_SEPARATOR, array_reverse($names)); } - protected function parseFunctionDeclaration(string $name, Node\FunctionLike $v): FunctionDef + protected function parseFunctionDeclaration(string $name, Node\Stmt\Function_|Node\Stmt\ClassMethod $v): FunctionDef { - $functionDef = new FunctionDef(); - $this->functionDef = $functionDef; - $functionDef->name = $name; - if ($v->returnType) { - $functionDef->returnType = $this->getTypeFromZendType($this->parseIdentifier($v->returnType)); - } else { - // .stub 存根定义 C++ Native 函数,必须设置返回值类型 - if ($this->stubFile) { - throw new Exception('No return type for ' . $name); - } - $functionDef->returnType = 'void'; + $returnType = $v->returnType ? $this->getTypeFromZendType($this->parseIdentifier($v->returnType)) : self::TYPE_VOID; + // .stub 存根定义 C++ Native 函数,必须设置返回值类型 + if ($returnType === self::TYPE_VOID && $this->stubFile) { + throw new Exception('No return type for ' . $name); } - $this->parseParams($v->params); + $functionDef = new FunctionDef($name, $returnType); + $this->functionDef = $functionDef; + $this->parseParams($v->params, $functionDef); return $functionDef; } @@ -359,10 +354,10 @@ class CompilerBase extends \PhpAot\Core\Translator } } - protected function parseParams($params): void + protected function parseParams($params, FunctionDef $functionDef): void { $list = []; - $this->functionDef->argCountRequired = count($params); + $functionDef->argCountRequired = count($params); foreach ($params as $param) { // .stub 存根定义 C++ Native 函数,必须设置函数的参数类型 if ($this->stubFile and !$param->type) { @@ -375,12 +370,12 @@ class CompilerBase extends \PhpAot\Core\Translator $argInfo->name = $name; $argInfo->type = $type; if (isset($param->default)) { - $this->functionDef->argCountRequired = count($list) - 1; + $functionDef->argCountRequired = count($list) - 1; $argInfo->default = $this->parseIdentifier($param->default); } - $this->functionDef->argInfoList[] = $argInfo; + $functionDef->argInfoList[] = $argInfo; } - $this->functionDef->params = implode(', ', $list); + $functionDef->params = implode(', ', $list); } protected function parseStmts(array $stmts): string @@ -1956,7 +1951,7 @@ class CompilerBase extends \PhpAot\Core\Translator protected function formatCppCode(string $file): void { $cmd = 'cd ' . $this->rootPath . ' && clang-format -i ' . $file; - $this->climate->info('formatting ' . $file ); + $this->climate->info('format: ' . $file ); $this->climate->comment($cmd); shell_exec($cmd); } diff --git a/src/Php/FunctionDef.php b/src/Php/FunctionDef.php index cf6e8667..91403f17 100644 --- a/src/Php/FunctionDef.php +++ b/src/Php/FunctionDef.php @@ -5,8 +5,15 @@ namespace PhpAot\Php; class FunctionDef { public string $name; - public array $argInfoList = []; - public string $params; public string $returnType; + public array $argInfoList = []; public int $argCountRequired = 0; + public string $params = ''; + + + public function __construct(string $name, string $returnType) + { + $this->name = $name; + $this->returnType = $returnType; + } } diff --git a/src/Php/MethodDef.php b/src/Php/MethodDef.php index da8ea4ac..8a82a14d 100644 --- a/src/Php/MethodDef.php +++ b/src/Php/MethodDef.php @@ -4,14 +4,24 @@ namespace PhpAot\Php; class MethodDef { - public string $name; public int $flags; - public string $returnType; + public string $name; + public FunctionDef $def; - public function __construct(string $name, int $flags, string $returnType) + public function __construct(int $flags, string $name, FunctionDef $def) { - $this->name = $name; $this->flags = $flags; - $this->returnType = $returnType; + $this->name = $name; + $this->def = $def; + } + + public function getFlags(): int + { + return $this->flags; + } + + public function getReturnType(): string + { + return $this->def->returnType; } -} \ No newline at end of file +} diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 64f95697..598998b9 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -475,7 +475,7 @@ class Translator extends Preprocessor $cppCode .= $this->getIndent() . self::TYPE_OBJECT . ' this_(&execute_data->This);' . PHP_EOL; $fn = self::PREFIX . $this->getNativeMethodName($classDef, $methodDef); - if ($methodDef->returnType !== self::TYPE_VOID) { + if ($methodDef->getReturnType() !== self::TYPE_VOID) { $cppCode .= $this->getIndent() . 'auto retval = ' . $fn . '(this_);' . PHP_EOL; $cppCode .= $this->getIndent() . 'php::move(retval, return_value);' . PHP_EOL; } else { @@ -679,11 +679,9 @@ class Translator extends Preprocessor private function parseClassMethod(Node\Stmt\ClassMethod $v, array &$methodCodes): void { $name = $this->getMethodName($v); - $flags = $v->flags; - $returnType = $v->returnType ? $this->getTypeFromZendType($this->parseIdentifier($v->returnType)) : self::TYPE_VOID; - $methodDef = new MethodDef($name, $flags, $returnType); - $this->classDef->methods[$name] = $methodDef; $methodCodes[$name] = $this->parseFunction($v); + $methodDef = new MethodDef($v->flags, $name, $this->functionDef); + $this->classDef->methods[$name] = $methodDef; } private function parseIdentifierList(array $implements): array