From cf5e889a75f7b34fdc7f2e0db273af2c87698d5d Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 26 Jan 2026 20:08:06 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E9=87=8D=E6=9E=84PHP=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E5=99=A8=E5=87=BD=E6=95=B0=E5=A3=B0=E6=98=8E=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E5=92=8C=E5=8C=85=E8=A3=85=E7=94=9F=E6=88=90=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改parseFunctionDeclaration方法移除name参数,直接从节点获取函数名 - 修复错误消息中的变量引用,使用$v->name替代传入的$name参数 - 统一FunctionDef创建时的名称解析,使用parseIdentifier处理节点名称 - 更新parseFunctionDeclaration调用处,传递节点对象而非名称字符串 - 重命名genMethodWrapper为genWrapperFunctionArgs并重构参数处理逻辑 - 添加对方法和函数的不同参数处理逻辑,区分是否包含this_参数 - 重构genMethodWrapper方法以复用新的genWrapperFunctionArgs功能 - 在Constants.php中添加char关键字到保留字列表 --- src/Php/CompilerBase.php | 8 +++--- src/Php/Constants.php | 1 + src/Php/Preprocessor.php | 2 +- src/Php/Translator.php | 54 ++++++++++++++++------------------------ 4 files changed, 28 insertions(+), 37 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index ffbc2adb..ba290961 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -276,14 +276,14 @@ class CompilerBase extends \PhpAot\Core\Translator return implode(self::NAMESPACE_SEPARATOR, array_reverse($names)); } - protected function parseFunctionDeclaration(string $name, Node\Stmt\Function_|Node\Stmt\ClassMethod $v): FunctionDef + protected function parseFunctionDeclaration(Node\Stmt\Function_|Node\Stmt\ClassMethod $v): FunctionDef { $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); + throw new Exception('No return type for ' . $v->name); } - $functionDef = new FunctionDef($name, $returnType); + $functionDef = new FunctionDef($this->parseIdentifier($v->name), $returnType); $this->functionDef = $functionDef; $this->parseParams($v->params, $functionDef); return $functionDef; @@ -297,7 +297,7 @@ class CompilerBase extends \PhpAot\Core\Translator if (isset($this->nativeFunctions[$name])) { $this->functionDef = $this->nativeFunctions[$name]; } else { - $this->nativeFunctions[$name] = $this->parseFunctionDeclaration($name, $v); + $this->nativeFunctions[$name] = $this->parseFunctionDeclaration($v); if (isset($this->redoAfterDeclare[$name])) { unset($this->redoAfterDeclare[$name]); $this->climate->cyan('Received redo request, retrying...'); diff --git a/src/Php/Constants.php b/src/Php/Constants.php index d3e0dd88..fd787523 100644 --- a/src/Php/Constants.php +++ b/src/Php/Constants.php @@ -35,6 +35,7 @@ class Constants 'new', 'null', 'var', + 'char', 'or', 'and', 'private', diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index 970d8139..71218e95 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -140,7 +140,7 @@ class Preprocessor extends CompilerBase { $name = $this->getFunctionName($v); if ($this->stubFile) { - $this->nativeFunctions[$name] = $this->parseFunctionDeclaration($name, $v); + $this->nativeFunctions[$name] = $this->parseFunctionDeclaration($v); } else { $this->functionDeclInFile[$name] = $this->file; } diff --git a/src/Php/Translator.php b/src/Php/Translator.php index dad53cf8..fbcff471 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -657,15 +657,11 @@ class Translator extends Preprocessor return $code; } - protected function genMethodWrapper(ClassDef $classDef, MethodDef $methodDef): string + protected function genWrapperFunctionArgs(string $fn, FunctionDef $functionDef): string { - $name = $classDef->getNamespacedName(); - $cppCode = 'ZEND_METHOD(' . $name . ', ' . $methodDef->name . '){' . PHP_EOL; - $cppCode .= $this->getIndent() . self::TYPE_OBJECT . ' this_(&execute_data->This);' . PHP_EOL; - $fn = self::PREFIX . $this->getNativeMethodName($classDef, $methodDef); - + $cppCode = ''; $callParams = ''; - foreach ($methodDef->functionDef->argInfoList as $k => $argInfo) { + foreach ($functionDef->argInfoList as $k => $argInfo) { if ($argInfo->default) { $argExpr = 'php::getCallArg(' . $k . ', ' . $argInfo->default . ')'; } else { @@ -675,9 +671,14 @@ class Translator extends Preprocessor $cppCode .= $this->getIndent() . $argInfo->type . ' arg_' . $argInfo->name . ' = ' . $expr . ';' . PHP_EOL; $callParams .= 'arg_' . $argInfo->name . ','; } - $callParams = $methodDef->functionDef->argInfoList ? 'this_, ' . rtrim($callParams, ',') : 'this_'; - if ($methodDef->getReturnType() !== self::TYPE_VOID) { + if ($functionDef->method) { + $callParams = $functionDef->argInfoList ? 'this_, ' . rtrim($callParams, ',') : 'this_'; + } else { + $callParams = $functionDef->argInfoList ? rtrim($callParams, ',') : ''; + } + + if ($functionDef->returnType !== self::TYPE_VOID) { $cppCode .= $this->getIndent() . 'auto retval = ' . $fn . '(' . $callParams . ');' . PHP_EOL; $cppCode .= $this->getIndent() . 'php::move(retval, return_value);' . PHP_EOL; } else { @@ -688,33 +689,22 @@ class Translator extends Preprocessor return $cppCode; } - private function genFunctionWrapper(FunctionDef $functionDef) + protected function genMethodWrapper(ClassDef $classDef, MethodDef $methodDef): string + { + $name = $classDef->getNamespacedName(); + $cppCode = 'ZEND_METHOD(' . $name . ', ' . $methodDef->name . '){' . PHP_EOL; + $cppCode .= $this->getIndent() . self::TYPE_OBJECT . ' this_(&execute_data->This);' . PHP_EOL; + $fn = self::PREFIX . $this->getNativeMethodName($classDef, $methodDef); + $cppCode .= $this->genWrapperFunctionArgs($fn, $methodDef->functionDef); + return $cppCode; + } + + private function genFunctionWrapper(FunctionDef $functionDef): string { $name = $functionDef->name; $cppCode = 'ZEND_FUNCTION(' . $name . '){' . PHP_EOL; $fn = self::PREFIX . $this->getNativeName($functionDef->name); - - $callParams = ''; - foreach ($functionDef->argInfoList as $k => $argInfo) { - if ($argInfo->default) { - $argExpr = 'php::getCallArg(' . $k . ', ' . $argInfo->default . ')'; - } else { - $argExpr = 'php::getCallArg(' . $k . ')'; - } - $expr = $this->convertExprFromType($argInfo->type, $argExpr); - $cppCode .= $this->getIndent() . $argInfo->type . ' arg_' . $argInfo->name . ' = ' . $expr . ';' . PHP_EOL; - $callParams .= 'arg_' . $argInfo->name . ','; - } - $callParams = $functionDef->argInfoList ? rtrim($callParams, ',') : ''; - - if ($functionDef->returnType !== self::TYPE_VOID) { - $cppCode .= $this->getIndent() . 'auto retval = ' . $fn . '(' . $callParams . ');' . PHP_EOL; - $cppCode .= $this->getIndent() . 'php::move(retval, return_value);' . PHP_EOL; - } else { - $cppCode .= $this->getIndent() . $fn . '(' . $callParams . ');' . PHP_EOL; - } - $cppCode .= '}' . PHP_EOL . PHP_EOL; - + $cppCode .= $this->genWrapperFunctionArgs($fn, $functionDef); return $cppCode; }