feat(php): 支持联合类型并优化编译器状态管理

- 添加 UnionType 支持以处理 PHP 联合类型语法
- 实现 resetClass 和 resetFunction 方法来重置编译器状态
- 在适当位置调用状态重置方法确保编译器状态一致性
- 修复类方法定义中的访问修饰符处理逻辑
- 更新示例文件以演示联合类型功能
- 调整项目配置文件中的源码路径设置
pull/1/head
韩天峰 7 months ago
parent 1a8082ce2f
commit 96ecb8aae0
  1. 10
      examples/fcall.php
  2. 2
      examples/prime/project.yml
  3. 13
      src/Php/CompilerBase.php
  4. 10
      src/Php/Translator.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);
function main()
{
$o = new A;
$o->foo(1, 2);
}

@ -8,6 +8,6 @@ ldflags: |
-L/usr/lib/x86_64-linux-gnu/
-lssl
sources:
- php-src*
- php-src
- ./cpp-src
- main.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;

@ -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 = '';

Loading…
Cancel
Save