From c7c9a8e2633f22eca050566f1c4154ea68c28ce0 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 9 Jun 2026 17:25:14 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E9=87=8D=E6=9E=84=20SSA=20?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E5=99=A8=E4=B8=AD=E7=9A=84=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E5=92=8C=E6=8A=9B=E5=87=BA=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=A3=80?= =?UTF-8?q?=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将直接 instanceof 检查替换为 isReturnExpr 和 isThrowExpr 方法调用 - 添加 AstNodeType trait 以支持新的表达式类型检查方法 - 统一终端语句的判断逻辑,提高代码可读性和可维护性 --- src/Php/Analysis/SsaBuilder.php | 7 +++++-- src/Php/CompilerBase.php | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Php/Analysis/SsaBuilder.php b/src/Php/Analysis/SsaBuilder.php index 7e50cf7d..102b070a 100644 --- a/src/Php/Analysis/SsaBuilder.php +++ b/src/Php/Analysis/SsaBuilder.php @@ -25,6 +25,7 @@ namespace PhpAot\Php\Analysis; +use PhpAot\Php\AstNodeType; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Stmt; @@ -161,6 +162,8 @@ class VarState */ class SsaBuilder { + use AstNodeType; + /** @var SsaBlock[] All basic blocks */ public array $blocks = []; @@ -422,7 +425,7 @@ class SsaBuilder } // Terminal statements: end the current block - if ($stmt instanceof Stmt\Return_ || $stmt instanceof Stmt\Throw_) { + if ($this->isReturnExpr($stmt) || $this->isThrowExpr($stmt)) { $currentBlock->stmts[] = $stmt; if ($i < count($stmts) - 1) { $currentBlock = $this->newBlock(); @@ -584,7 +587,7 @@ class SsaBuilder $this->blocks[$targetId]->predecessors[] = $i; } // Goto does NOT fall through - } elseif ($lastStmt instanceof Stmt\Return_ || $lastStmt instanceof Stmt\Throw_) { + } elseif ($this->isReturnExpr($lastStmt) || $this->isThrowExpr($lastStmt)) { // Terminal: connect to exit block $block->successors[] = $this->exitBlockId; $this->blocks[$this->exitBlockId]->predecessors[] = $i; diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 1b3b953a..daf94763 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -3568,7 +3568,7 @@ class CompilerBase extends \PhpAot\Core\Translator } $code .= $this->getIndent() . ' ' . $var . ' = ' . $listTmpVar . '.item(' . $k . ');' . PHP_EOL; } else { - abort($item); + $this->fatalError($item, 'Unsupported foreach item type'); } } return $code;