diff --git a/project.yml b/project.yml new file mode 100644 index 00000000..aba23544 --- /dev/null +++ b/project.yml @@ -0,0 +1,12 @@ +name: swoole-compiler +type: ext +version: 0.0.1 +cxxflags: | + -std=c++14 + -Wall +sources: + - ./src/Php + - ./src/Core + - ./src/functions.php + - ./src/gen_stub.php + - ./vendor/nikic/php-parser/lib/PhpParser/NodeVisitorAbstract.php diff --git a/src/Php/AstNodeType.php b/src/Php/AstNodeType.php index e77de8ed..38d966e7 100644 --- a/src/Php/AstNodeType.php +++ b/src/Php/AstNodeType.php @@ -55,6 +55,11 @@ trait AstNodeType return $expr instanceof Node\Name; } + protected function isFullNameExpr(NodeAbstract $expr): bool + { + return $expr instanceof Node\Name\FullyQualified; + } + protected function isNamedMethod(NodeAbstract $expr): bool { return $this->isIdExpr($expr); diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 0a2aa8f2..706a496f 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -515,9 +515,10 @@ class CompilerBase extends \PhpAot\Core\Translator case 'Expr_Yield': case 'Expr_YieldFrom': $this->fatalError($expr, 'The `' . $type . '` is not supported'); - // no break + break; default: abort($expr); + break; } } @@ -926,6 +927,7 @@ class CompilerBase extends \PhpAot\Core\Translator return $expr->hasAttribute('noLiteralString') ? $this->genCharPtr($expr->value) : $this->getLiteralString($expr->value); default: abort($expr); + break; } } @@ -1135,9 +1137,10 @@ class CompilerBase extends \PhpAot\Core\Translator break; case 'Stmt_Class': $this->fatalError($v, 'Cannot declare class in function'); - // no break + break; default: abort($v); + break; } $lines = array_merge($lines, $this->context->beforeStmtLines); $this->context->beforeStmtLines = []; @@ -3224,9 +3227,9 @@ class CompilerBase extends \PhpAot\Core\Translator return '"' . $this->escapeString($expr->value) . '"'; } - protected function parseGlobal(Node\Stmt\Global_ $v): string + protected function parseGlobal(Node\Stmt\Global_ $expr): string { - foreach ($v->vars as $v) { + foreach ($expr->vars as $v) { $name = $this->parseVariable($v); if (!$this->hasGlobalVar($name)) { $this->addGlobalVar($name, self::TYPE_VAR); @@ -3433,6 +3436,7 @@ class CompilerBase extends \PhpAot\Core\Translator return '"' . $this->escapeString($class) . '::' . $this->escapeString($this->method) . '"'; default: abort($expr); + break; } } @@ -3700,6 +3704,7 @@ class CompilerBase extends \PhpAot\Core\Translator break; default: $this->fatalError($expr, 'Invalid include type'); + break; } return 'php::include(' . $this->parseIdentifier($expr->expr) . ', ' . $type . ')'; @@ -4356,7 +4361,13 @@ class CompilerBase extends \PhpAot\Core\Translator $code .= $this->getIndent() . 'if (' . $var . ' && '; foreach ($types as $type) { - $code .= 'php::instanceOf(' . $var . ', "' . $this->parseIdentifier($type) . '")'; + if ($this->isNameExpr($type) or $this->isFullNameExpr($type)) { + $class = $this->getNamespacedClassName($this->parseIdentifier($type)); + $ce = $this->getClassEntryPtr($class); + $code .= Symbol::instanceOf() . '(' . $var . ', ' . $ce . ')'; + } else { + $this->fatalError($type, 'Unsupported catch type'); + } } $code .= ') {' . PHP_EOL; diff --git a/src/Php/Constants.php b/src/Php/Constants.php index b028dcb5..4a83d0ab 100644 --- a/src/Php/Constants.php +++ b/src/Php/Constants.php @@ -33,6 +33,11 @@ class Constants 'if', 'bool', 'int', + 'short', + 'long', + 'unsigned', + 'void', + 'signed', 'double', 'float', 'false', diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index 90f7a111..eb3672e3 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -143,9 +143,10 @@ class Preprocessor extends CompilerBase break; case 'Stmt_Expression': $this->foundStrayCode($v); - // no break + break; default: $this->fatalError($v, 'Unsupported statement: ' . $type); + break; } } } @@ -451,9 +452,10 @@ class Preprocessor extends CompilerBase break; case 'Stmt_Expression': $this->foundStrayCode($v); - // no break + break; default: abort($v); + break; } } diff --git a/src/Php/Symbol.php b/src/Php/Symbol.php index d23c03d7..d2ddb2e9 100644 --- a/src/Php/Symbol.php +++ b/src/Php/Symbol.php @@ -20,6 +20,11 @@ class Symbol return 'php::setStaticProperty'; } + public static function instanceOf(): string + { + return 'php::instanceOf'; + } + public static function getCalledCe(): string { return CompilerBase::PREFIX . 'get_called_ce(this_)'; diff --git a/src/Php/Translator.php b/src/Php/Translator.php index c015f5c7..349bea83 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -857,6 +857,7 @@ class Translator extends Preprocessor break; default: abort($v); + break; } } @@ -988,6 +989,7 @@ class Translator extends Preprocessor break; default: abort($v2); + break; } } $code .= $ns_end; @@ -1162,6 +1164,7 @@ class Translator extends Preprocessor break; default: abort($v); + break; } } $code = $this->genNativeMethod($methodCodes); diff --git a/tests/aot/try-catch.phpt b/tests/aot/exception/001.phpt similarity index 100% rename from tests/aot/try-catch.phpt rename to tests/aot/exception/001.phpt diff --git a/tests/aot/try-catch-2.phpt b/tests/aot/exception/002.phpt similarity index 100% rename from tests/aot/try-catch-2.phpt rename to tests/aot/exception/002.phpt diff --git a/tests/aot/exception/003.phpt b/tests/aot/exception/003.phpt new file mode 100644 index 00000000..dc8d72c5 --- /dev/null +++ b/tests/aot/exception/003.phpt @@ -0,0 +1,21 @@ +--TEST-- +try catch 2 +--FILE-- +getMessage()); + } +} +} +?> +--EXPECT-- +string(10) "test error"