TypePHP 编译器 https://swoole.com/aot/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

75 lines
1.9 KiB

<?php
/**
* This file is part of TypePHP.
*
* @link https://www.swoole.com/
* @contact service@swoole.com
*/
namespace TypePhp\Diagnostics;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PhpParser\NodeAbstract;
trait CompilerDiagnosticTrait
{
/**
* Report a compiler fatal error.
*/
public function error(string $msg): never
{
$this->getDiagnosticReporter()->fatal($msg);
}
public function fatalError(NodeAbstract $node, string $msg): never
{
$this->error("{$msg} in {$this->file}:{$node->getStartLine()}");
}
protected function warning(Node $node, string $msg): void
{
$this->getDiagnosticReporter()->warning($node, $this->file, $msg);
}
protected function fatalCompileTimeAttribute(
Node $target,
string $attribute,
string $message,
?Node $source = null,
?string $conflictAttribute = null,
?Node $conflictSource = null,
): never {
$this->error(CompileTimeAttributeDiagnostic::format(
$message,
$attribute,
$target,
$this->file,
$source,
$conflictAttribute,
$conflictSource,
));
}
protected function errorUndefinedVariable(Variable $node): never
{
$this->fatalError($node, "The variable `\${$node->name}` is undefined");
}
protected function warningUndefinedBehavior(NodeAbstract $expr): void
{
$this->warning($expr, 'Use this expression carefully, which may be inconsistent with the dynamic execution behavior');
}
protected function dump(NodeAbstract $node): void
{
if ($this->debugLine == $node->getStartLine()) {
var_dump($node);
}
}
protected function foundStrayCode(Node $node): never
{
$this->fatalError($node, 'All execution code must be within a function, found stray code');
}
}