@@ -1576,6 +1576,7 @@ class EvaluatedValue
1576
1576
*/
1577
1577
public static function createFromExpression (Expr $ expr , ?SimpleType $ constType , ?string $ cConstName , iterable $ allConstInfos ): EvaluatedValue
1578
1578
{
1579
+ // This visitor replaces the PHP constants by C constants. It allows direct expansion of the compiled constants, e.g. later in the pretty printer.
1579
1580
$ visitor = new class ($ allConstInfos ) extends PhpParser \NodeVisitorAbstract
1580
1581
{
1581
1582
public array $ visitedConstants = [];
@@ -1623,6 +1624,7 @@ public function enterNode(Node $expr)
1623
1624
1624
1625
$ evaluator = new ConstExprEvaluator (
1625
1626
function (Expr $ expr ) use ($ allConstInfos , &$ isUnknownConstValue ) {
1627
+ // $expr is a ConstFetch with a name of a C macro here
1626
1628
if (!$ expr instanceof Expr \ConstFetch) {
1627
1629
throw new Exception ($ this ->getVariableTypeName () . " " . $ this ->getVariableLikeName () . " has an unsupported value " );
1628
1630
}
@@ -1663,7 +1665,7 @@ function (Expr $expr) use ($allConstInfos, &$isUnknownConstValue) {
1663
1665
$ result = $ evaluator ->evaluateDirectly ($ expr );
1664
1666
1665
1667
return new EvaluatedValue (
1666
- $ result ,
1668
+ $ result , // note: we are generally not interested in the actual value of $result, unless it's a bare value, without constants
1667
1669
$ constType ?? SimpleType::fromValue ($ result ),
1668
1670
$ cConstName === null ? $ expr : new Expr \ConstFetch (new Node \Name ($ cConstName )),
1669
1671
$ visitor ->visitedConstants ,
@@ -1717,7 +1719,7 @@ public function initializeZval(string $zvalName): string
1717
1719
$ code .= "\tZVAL_STR(& $ zvalName, {$ zvalName }_str); \n" ;
1718
1720
}
1719
1721
} elseif ($ this ->type ->isArray ()) {
1720
- if ($ cExpr == '[] ' && empty ( $ this -> value ) ) {
1722
+ if ($ cExpr == '[] ' ) {
1721
1723
$ code .= "\tZVAL_EMPTY_ARRAY(& $ zvalName); \n" ;
1722
1724
} else {
1723
1725
throw new Exception ("Unimplemented default value " );
@@ -1731,6 +1733,7 @@ public function initializeZval(string $zvalName): string
1731
1733
1732
1734
public function getCExpr (): ?string
1733
1735
{
1736
+ // $this->expr has all its PHP constants replaced by C constants
1734
1737
$ prettyPrinter = new Standard ;
1735
1738
$ expr = $ prettyPrinter ->prettyPrintExpr ($ this ->expr );
1736
1739
return $ expr [0 ] == '" ' ? $ expr : preg_replace ('(\bnull\b) ' , 'NULL ' , str_replace ('\\' , '' , $ expr ));
@@ -1974,6 +1977,7 @@ public function getDeclaration(iterable $allConstInfos): string
1974
1977
}
1975
1978
1976
1979
$ value = EvaluatedValue::createFromExpression ($ this ->value , $ type , $ this ->cValue , $ allConstInfos );
1980
+ // i.e. const NAME = UNKNOWN;, without the annotation
1977
1981
if ($ value ->isUnknownConstValue && $ this ->cValue === null && $ value ->expr instanceof Expr \ConstFetch && $ value ->expr ->name ->__toString () == $ this ->name ->__toString ()) {
1978
1982
throw new Exception ("Constant " . $ this ->name ->__toString () . " must have a @cvalue annotation " );
1979
1983
}
0 commit comments