@@ -1576,6 +1576,7 @@ class EvaluatedValue
15761576 */
15771577 public static function createFromExpression (Expr $ expr , ?SimpleType $ constType , ?string $ cConstName , iterable $ allConstInfos ): EvaluatedValue
15781578 {
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.
15791580 $ visitor = new class ($ allConstInfos ) extends PhpParser \NodeVisitorAbstract
15801581 {
15811582 public array $ visitedConstants = [];
@@ -1623,6 +1624,7 @@ public function enterNode(Node $expr)
16231624
16241625 $ evaluator = new ConstExprEvaluator (
16251626 function (Expr $ expr ) use ($ allConstInfos , &$ isUnknownConstValue ) {
1627+ // $expr is a ConstFetch with a name of a C macro here
16261628 if (!$ expr instanceof Expr \ConstFetch) {
16271629 throw new Exception ($ this ->getVariableTypeName () . " " . $ this ->getVariableLikeName () . " has an unsupported value " );
16281630 }
@@ -1663,7 +1665,7 @@ function (Expr $expr) use ($allConstInfos, &$isUnknownConstValue) {
16631665 $ result = $ evaluator ->evaluateDirectly ($ expr );
16641666
16651667 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
16671669 $ constType ?? SimpleType::fromValue ($ result ),
16681670 $ cConstName === null ? $ expr : new Expr \ConstFetch (new Node \Name ($ cConstName )),
16691671 $ visitor ->visitedConstants ,
@@ -1717,7 +1719,7 @@ public function initializeZval(string $zvalName): string
17171719 $ code .= "\tZVAL_STR(& $ zvalName, {$ zvalName }_str); \n" ;
17181720 }
17191721 } elseif ($ this ->type ->isArray ()) {
1720- if ($ cExpr == '[] ' && empty ( $ this -> value ) ) {
1722+ if ($ cExpr == '[] ' ) {
17211723 $ code .= "\tZVAL_EMPTY_ARRAY(& $ zvalName); \n" ;
17221724 } else {
17231725 throw new Exception ("Unimplemented default value " );
@@ -1731,6 +1733,7 @@ public function initializeZval(string $zvalName): string
17311733
17321734 public function getCExpr (): ?string
17331735 {
1736+ // $this->expr has all its PHP constants replaced by C constants
17341737 $ prettyPrinter = new Standard ;
17351738 $ expr = $ prettyPrinter ->prettyPrintExpr ($ this ->expr );
17361739 return $ expr [0 ] == '" ' ? $ expr : preg_replace ('(\bnull\b) ' , 'NULL ' , str_replace ('\\' , '' , $ expr ));
@@ -1974,6 +1977,7 @@ public function getDeclaration(iterable $allConstInfos): string
19741977 }
19751978
19761979 $ value = EvaluatedValue::createFromExpression ($ this ->value , $ type , $ this ->cValue , $ allConstInfos );
1980+ // i.e. const NAME = UNKNOWN;, without the annotation
19771981 if ($ value ->isUnknownConstValue && $ this ->cValue === null && $ value ->expr instanceof Expr \ConstFetch && $ value ->expr ->name ->__toString () == $ this ->name ->__toString ()) {
19781982 throw new Exception ("Constant " . $ this ->name ->__toString () . " must have a @cvalue annotation " );
19791983 }
0 commit comments