refactor(php): 重构 SSA 构建器中的返回和抛出表达式检查

- 将直接 instanceof 检查替换为 isReturnExpr 和 isThrowExpr 方法调用
- 添加 AstNodeType trait 以支持新的表达式类型检查方法
- 统一终端语句的判断逻辑,提高代码可读性和可维护性
pull/1/head
韩天峰 3 months ago
parent 6a1fb828f8
commit c7c9a8e263
  1. 7
      src/Php/Analysis/SsaBuilder.php
  2. 2
      src/Php/CompilerBase.php

@ -25,6 +25,7 @@
namespace PhpAot\Php\Analysis; namespace PhpAot\Php\Analysis;
use PhpAot\Php\AstNodeType;
use PhpParser\Node; use PhpParser\Node;
use PhpParser\Node\Expr; use PhpParser\Node\Expr;
use PhpParser\Node\Stmt; use PhpParser\Node\Stmt;
@ -161,6 +162,8 @@ class VarState
*/ */
class SsaBuilder class SsaBuilder
{ {
use AstNodeType;
/** @var SsaBlock[] All basic blocks */ /** @var SsaBlock[] All basic blocks */
public array $blocks = []; public array $blocks = [];
@ -422,7 +425,7 @@ class SsaBuilder
} }
// Terminal statements: end the current block // 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; $currentBlock->stmts[] = $stmt;
if ($i < count($stmts) - 1) { if ($i < count($stmts) - 1) {
$currentBlock = $this->newBlock(); $currentBlock = $this->newBlock();
@ -584,7 +587,7 @@ class SsaBuilder
$this->blocks[$targetId]->predecessors[] = $i; $this->blocks[$targetId]->predecessors[] = $i;
} }
// Goto does NOT fall through // 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 // Terminal: connect to exit block
$block->successors[] = $this->exitBlockId; $block->successors[] = $this->exitBlockId;
$this->blocks[$this->exitBlockId]->predecessors[] = $i; $this->blocks[$this->exitBlockId]->predecessors[] = $i;

@ -3568,7 +3568,7 @@ class CompilerBase extends \PhpAot\Core\Translator
} }
$code .= $this->getIndent() . ' ' . $var . ' = ' . $listTmpVar . '.item(' . $k . ');' . PHP_EOL; $code .= $this->getIndent() . ' ' . $var . ' = ' . $listTmpVar . '.item(' . $k . ');' . PHP_EOL;
} else { } else {
abort($item); $this->fatalError($item, 'Unsupported foreach item type');
} }
} }
return $code; return $code;

Loading…
Cancel
Save