refactor(generator): 重构代码生成器的类信息管理和错误处理

- 使用 ClassInfo::getCurrentClassName() 方法替代静态属性访问
- 移除 ClassInfo::$currentClassName 静态属性,统一通过实例方法获取类名
- 在属性处理中引入 Skip 异常机制优化 AllowDynamicProperties 处理逻辑
- 统一字符串引用格式从双引号改为单引号以保持一致性
- 将条件声明的空数组初始化改为引用赋值以优化内存使用
- 移除未使用的 Unsupported 异常导入
- 将 Translator 中的异常抛出改为调用 fatalError 方法进行错误处理
pull/1/head
韩天峰 4 months ago
parent ee8c40698b
commit d1a06ac8d4
  1. 1
      src/Php/Preprocessor.php
  2. 12
      src/Php/Translator.php
  3. 35
      src/gen_stub.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;

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

@ -1,6 +1,7 @@
<?php
declare(strict_types=1);
use PhpAot\Php\Exception\Skip;
use PhpAot\Php\Translator;
use PhpParser\Comment\Doc as DocComment;
use PhpParser\ConstExprEvaluator;
@ -225,7 +226,7 @@ class SimpleType {
}
if ($node->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<string, ConstInfo> $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;
}

Loading…
Cancel
Save