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