diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index 921c0642..90f7a111 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -15,7 +15,6 @@ use PhpAot\Php\Entity\FunctionDef; use PhpAot\Php\Entity\MethodDef; use PhpAot\Php\Entity\PropertyDef; use PhpAot\Php\Exception\SyntaxError; -use PhpAot\Php\Exception\Unsupported; use PhpParser\Modifiers; use PhpParser\Node; use PhpParser\Node\NullableType; diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 7cf693fe..c015f5c7 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -215,10 +215,10 @@ class Translator extends Preprocessor try { $this->prepareFile($file); } catch (Unsupported $e) { - $this->output(" unsupported syntax: " . $e->getMessage() . "\n" . " skip: " . $file . "\n", 'error'); + $this->output(' unsupported syntax: ' . $e->getMessage() . "\n" . ' skip: ' . $file . "\n", 'error'); unset($files[$k]); } catch (SyntaxError $e) { - $this->output(" syntax error: " . $e->getMessage() . "\n" . " skip: " . $file . "\n", 'error'); + $this->output(' syntax error: ' . $e->getMessage() . "\n" . ' skip: ' . $file . "\n", 'error'); unset($files[$k]); } } @@ -242,14 +242,14 @@ class Translator extends Preprocessor } $sourceFiles[] = $cppFile; } catch (Unsupported $e) { - echo " unsupported syntax: " . $e->getMessage() . "\n"; - echo " skip: " . $file . "\n"; + echo ' unsupported syntax: ' . $e->getMessage() . "\n"; + echo ' skip: ' . $file . "\n"; unset($files[$k]); } } if (empty($sourceFiles)) { - $this->stop("No valid source file found"); + $this->stop('No valid source file found'); } return $sourceFiles; } @@ -637,7 +637,7 @@ class Translator extends Preprocessor return $this->genCValue(constant($constName)); } } - throw new \Exception("Class constant `{$class}::{$name}` not found"); + $this->fatalError($expr, "Class constant `{$class}::{$name}` not found"); } public function getConstValue(string $name): mixed diff --git a/src/gen_stub.php b/src/gen_stub.php index a79db747..4ad837a2 100755 --- a/src/gen_stub.php +++ b/src/gen_stub.php @@ -1,6 +1,7 @@ toLowerString() === 'self') { - return new SimpleType(ClassInfo::$currentClassName, false); + return new SimpleType(ClassInfo::getCurrentClassName(), false); } $class = $node->isFullyQualified() ? $node->toString() : getTranslator()->getNamespacedClassName($node->toString()); @@ -3443,7 +3444,6 @@ class AttributeInfo { class ClassInfo { static public ?self $currentClass = null; - static public ?string $currentClassName = null; public /* readonly */ Name $name; private int $flags; public string $type; @@ -3523,6 +3523,11 @@ class ClassInfo { self::$currentClass = $this; } + static public function getCurrentClassName(): string + { + return self::$currentClass->name->toString(); + } + /** @param array $allConstInfos */ public function getRegistration(array $allConstInfos): string { @@ -4434,7 +4439,6 @@ class FileInfo { if ($stmt instanceof Stmt\ClassLike) { $className = $stmt->namespacedName; - ClassInfo::$currentClassName = $className->toString(); $constInfos = []; $propertyInfos = []; $methodInfos = []; @@ -4576,6 +4580,7 @@ class FileInfo { $code = ""; foreach ($this->classInfos as $class) { + ClassInfo::$currentClass = $class; $code .= "\n" . $class->getRegistration($allConstInfos); } @@ -5110,12 +5115,17 @@ function parseClass( } $attributes = AttributeInfo::createFromGroups($class->attrGroups); - foreach ($attributes as $attribute) { - switch ($attribute->class) { - case 'AllowDynamicProperties': - $allowsDynamicProperties = true; - break 2; + + try { + foreach ($attributes as $attribute) { + switch ($attribute->class) { + case 'AllowDynamicProperties': + $allowsDynamicProperties = true; + throw new Skip(); + } } + } catch (Skip) { + } if ($isStrictProperties && $allowsDynamicProperties) { @@ -5385,7 +5395,8 @@ function generateFunctionAttributeInitialization(iterable $funcInfos, array $all // conditionally available; string reuse is only among declarations // that are always there if ($funcInfo->cond) { - $useDeclared = []; + $empty = []; + $useDeclared = &$empty; } else { $useDeclared = &$declaredStrings; } @@ -5448,7 +5459,8 @@ function generateGlobalConstantAttributeInitialization( // conditionally available; string reuse is only among declarations // that are always there if ($constInfo->cond) { - $useDeclared = []; + $empty = []; + $useDeclared = &$empty; } else { $useDeclared = &$declaredStrings; } @@ -5504,7 +5516,8 @@ function generateConstantAttributeInitialization( // conditionally available; string reuse is only among declarations // that are always there if ($constInfo->cond) { - $useDeclared = []; + $empty = []; + $useDeclared = &$empty; } else { $useDeclared = &$declaredStrings; }