refactor(php): 重构函数和方法定义解析逻辑

- 修改 parseFunctionDeclaration 方法参数类型,支持 Function_ 和 ClassMethod
- 简化函数定义创建流程,直接在构造函数中初始化 name 和 returnType
- 更新 parseParams 方法接受 FunctionDef 参数,避免使用全局变量
- 重命名格式化提示信息从 formatting 为 format
- 重构 FunctionDef 类添加构造函数并调整属性初始化
- 修改 MethodDef 类结构,使用 FunctionDef 对象替代单独的 returnType 属性
- 更新 Translator 中的方法调用以适配新的 MethodDef 结构
- 调整类方法解析流程以兼容新的数据结构
pull/1/head
韩天峰 8 months ago
parent 5fd9215509
commit e4505c2ef1
  1. 33
      src/Php/CompilerBase.php
  2. 11
      src/Php/FunctionDef.php
  3. 22
      src/Php/MethodDef.php
  4. 8
      src/Php/Translator.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);
}

@ -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;
}
}

@ -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;
}
}
}

@ -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

Loading…
Cancel
Save