@@ -66,10 +66,11 @@ function processStubFile(string $stubFile, Context $context): ?FileInfo {
66
66
67
67
initPhpParser ();
68
68
$ fileInfo = parseStubFile ($ stubCode );
69
- $ allConstInfos = $ fileInfo ->getAllConstInfos ();
69
+ $ constInfos = $ fileInfo ->getAllConstInfos ();
70
+ $ context ->allConstInfos = array_merge ($ context ->allConstInfos , $ constInfos );
70
71
71
72
$ generatedConstCode = false ;
72
- $ constCode = generateConstCode ($ allConstInfos , $ stubHash );
73
+ $ constCode = generateConstCode ($ constInfos , $ stubHash );
73
74
if (($ context ->forceRegeneration || $ stubHash !== $ oldStubHash ) && $ constCode !== "" && file_put_contents ($ constFile , $ constCode )) {
74
75
echo "Saved $ constFile \n" ;
75
76
$ generatedConstCode = true ;
@@ -78,7 +79,7 @@ function processStubFile(string $stubFile, Context $context): ?FileInfo {
78
79
$ arginfoCode = generateArgInfoCode (
79
80
basename ($ stubFilenameWithoutExtension ),
80
81
$ fileInfo ,
81
- $ allConstInfos ,
82
+ $ context -> allConstInfos ,
82
83
$ generatedConstCode ? basename ($ constFile ) : null ,
83
84
$ stubHash
84
85
);
@@ -99,7 +100,7 @@ function processStubFile(string $stubFile, Context $context): ?FileInfo {
99
100
$ arginfoCode = generateArgInfoCode (
100
101
basename ($ stubFilenameWithoutExtension ),
101
102
$ legacyFileInfo ,
102
- $ allConstInfos ,
103
+ $ context -> allConstInfos ,
103
104
$ generatedConstCode ? basename ($ constFile ) : null ,
104
105
$ stubHash
105
106
);
@@ -137,6 +138,8 @@ class Context {
137
138
public $ forceParse = false ;
138
139
/** @var bool */
139
140
public $ forceRegeneration = false ;
141
+ /** @var iterable<ConstInfo> */
142
+ public iterable $ allConstInfos = [];
140
143
}
141
144
142
145
class ArrayType extends SimpleType {
@@ -1486,6 +1489,17 @@ function (Expr $expr) use ($allConstInfos, &$cConstName, &$originatingConst, &$i
1486
1489
);
1487
1490
}
1488
1491
1492
+ public static function createFromExpressionRecursively (Expr $ expr , ?string $ cConstName , iterable $ allConstInfos ): EvaluatedValue
1493
+ {
1494
+ $ result = self ::createFromExpression ($ expr , $ cConstName , $ allConstInfos );
1495
+
1496
+ if ($ result ->originatingConst !== null ) {
1497
+ $ result = self ::createFromExpressionRecursively ($ result ->originatingConst ->value , $ result ->cConstName , $ allConstInfos );
1498
+ }
1499
+
1500
+ return $ result ;
1501
+ }
1502
+
1489
1503
public static function null (): EvaluatedValue
1490
1504
{
1491
1505
return new self (null , null , null , false );
@@ -1748,8 +1762,7 @@ public function getCDeclaration(iterable $allConstInfos): ?string
1748
1762
$ code .= $ value ->cConstName ;
1749
1763
} else {
1750
1764
if ($ value ->isUnknownConstValue ) {
1751
- echo "Skipping C declaration generation for constant $ this ->name , because it has an unknown constant value \n" ;
1752
- return "" ;
1765
+ throw new Exception ("Cannot generate C declaration for constant $ this ->name , because it has an unknown constant value " );
1753
1766
}
1754
1767
1755
1768
switch ($ value ->type ) {
@@ -1781,7 +1794,6 @@ public function getDeclaration(iterable $allConstInfos): string
1781
1794
{
1782
1795
$ value = EvaluatedValue::createFromExpression ($ this ->value , $ this ->cname , $ allConstInfos );
1783
1796
if ($ value ->isUnknownConstValue ) {
1784
- echo "Skipping code generation for constant $ this ->name , because it has an unknown constant default value \n" ;
1785
1797
return "" ;
1786
1798
}
1787
1799
@@ -1810,24 +1822,29 @@ private function getConstDeclaration(string $code, EvaluatedValue $value): strin
1810
1822
$ constName = str_replace ('\\' , '\\\\' , $ this ->name ->__toString ());
1811
1823
$ constValue = $ value ->value ;
1812
1824
1825
+ $ flags = "CONST_CS | CONST_PERSISTENT " ;
1826
+ if ($ this ->isDeprecated ) {
1827
+ $ flags .= " | CONST_DEPRECATED " ;
1828
+ }
1829
+
1813
1830
switch ($ value ->type ) {
1814
1831
case "NULL " :
1815
- $ code .= " REGISTER_NULL_CONSTANT( \"$ constName \", CONST_CS | CONST_PERSISTENT ); \n" ;
1832
+ $ code .= " REGISTER_NULL_CONSTANT( \"$ constName \", $ flags ); \n" ;
1816
1833
break ;
1817
1834
case "boolean " :
1818
- $ code .= " REGISTER_BOOL_CONSTANT( \"$ constName \", " . ($ cConstName ?: ($ constValue ? "true " : "false " )) . ", CONST_CS | CONST_PERSISTENT ); \n" ;
1835
+ $ code .= " REGISTER_BOOL_CONSTANT( \"$ constName \", " . ($ cConstName ?: ($ constValue ? "true " : "false " )) . ", $ flags ); \n" ;
1819
1836
break ;
1820
1837
case "integer " :
1821
- $ code .= " REGISTER_LONG_CONSTANT( \"$ constName \", " . ($ cConstName ?: (int ) $ constValue ) . ", CONST_CS | CONST_PERSISTENT ); \n" ;
1838
+ $ code .= " REGISTER_LONG_CONSTANT( \"$ constName \", " . ($ cConstName ?: (int ) $ constValue ) . ", $ flags ); \n" ;
1822
1839
break ;
1823
1840
case "double " :
1824
- $ code .= " REGISTER_DOUBLE_CONSTANT( \"$ constName \", " . ($ cConstName ?: (int ) $ constValue ) . ", CONST_CS | CONST_PERSISTENT ); \n" ;
1841
+ $ code .= " REGISTER_DOUBLE_CONSTANT( \"$ constName \", " . ($ cConstName ?: (int ) $ constValue ) . ", $ flags ); \n" ;
1825
1842
break ;
1826
1843
case "string " :
1827
- $ code .= " REGISTER_STRING_CONSTANT( \"$ constName \", " . ($ cConstName ?: '" ' . addslashes ($ constValue ) . '" ' ) . ", CONST_CS | CONST_PERSISTENT ); \n" ;
1844
+ $ code .= " REGISTER_STRING_CONSTANT( \"$ constName \", " . ($ cConstName ?: '" ' . addslashes ($ constValue ) . '" ' ) . ", $ flags ); \n" ;
1828
1845
break ;
1829
1846
default :
1830
- throw new Exception ("Not implemented constant type " );
1847
+ throw new Exception ("Unimplemented constant type " );
1831
1848
}
1832
1849
1833
1850
return $ code ;
@@ -1959,13 +1976,16 @@ public function getDeclaration(iterable $allConstInfos): string {
1959
1976
1960
1977
$ propertyName = $ this ->name ->property ;
1961
1978
1979
+ $ isUnknownConstValue = false ;
1962
1980
if ($ this ->defaultValue === null ) {
1963
1981
$ defaultValue = EvaluatedValue::null ();
1964
1982
} else {
1965
1983
$ defaultValue = EvaluatedValue::createFromExpression ($ this ->defaultValue , null , $ allConstInfos );
1984
+ $ recursiveDefaultValue = EvaluatedValue::createFromExpressionRecursively ($ this ->defaultValue , null , $ allConstInfos );
1985
+ $ isUnknownConstValue = $ recursiveDefaultValue ->isUnknownConstValue ;
1966
1986
}
1967
1987
1968
- if ($ defaultValue -> isUnknownConstValue ) {
1988
+ if ($ isUnknownConstValue ) {
1969
1989
echo "Skipping code generation for property $ this ->name , because it has an unknown constant default value \n" ;
1970
1990
return "" ;
1971
1991
}
@@ -4082,8 +4102,6 @@ function initPhpParser() {
4082
4102
$ funcMap = [];
4083
4103
/** @var array<string, FuncInfo> $aliasMap */
4084
4104
$ aliasMap = [];
4085
- /** @var iterable<ConstInfo> $constInfo */
4086
- $ constInfos = [];
4087
4105
4088
4106
foreach ($ fileInfos as $ fileInfo ) {
4089
4107
foreach ($ fileInfo ->getAllFuncInfos () as $ funcInfo ) {
@@ -4099,8 +4117,6 @@ function initPhpParser() {
4099
4117
foreach ($ fileInfo ->classInfos as $ classInfo ) {
4100
4118
$ classMap [$ classInfo ->name ->__toString ()] = $ classInfo ;
4101
4119
}
4102
-
4103
- $ constInfos = array_merge ($ constInfos , $ fileInfo ->getAllConstInfos ());
4104
4120
}
4105
4121
4106
4122
if ($ verify ) {
@@ -4191,7 +4207,7 @@ function(?ArgInfo $aliasArg, ?ArgInfo $aliasedArg) use ($aliasFunc, $aliasedFunc
4191
4207
if ($ generateClassSynopses ) {
4192
4208
$ classSynopsesDirectory = getcwd () . "/classsynopses " ;
4193
4209
4194
- $ classSynopses = generateClassSynopses ($ classMap , $ constInfos );
4210
+ $ classSynopses = generateClassSynopses ($ classMap , $ context -> allConstInfos );
4195
4211
if (!empty ($ classSynopses )) {
4196
4212
if (!file_exists ($ classSynopsesDirectory )) {
4197
4213
mkdir ($ classSynopsesDirectory );
@@ -4206,7 +4222,7 @@ function(?ArgInfo $aliasArg, ?ArgInfo $aliasedArg) use ($aliasFunc, $aliasedFunc
4206
4222
}
4207
4223
4208
4224
if ($ replaceClassSynopses ) {
4209
- $ classSynopses = replaceClassSynopses ($ targetSynopses , $ classMap , $ constInfos );
4225
+ $ classSynopses = replaceClassSynopses ($ targetSynopses , $ classMap , $ context -> allConstInfos );
4210
4226
4211
4227
foreach ($ classSynopses as $ filename => $ content ) {
4212
4228
if (file_put_contents ($ filename , $ content )) {
0 commit comments