@@ -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 {
@@ -1493,6 +1496,17 @@ function (Expr $expr) use ($allConstInfos, &$cConstName, &$originatingConst, &$i
1493
1496
);
1494
1497
}
1495
1498
1499
+ public static function createFromExpressionRecursively (Expr $ expr , ?string $ cConstName , iterable $ allConstInfos ): EvaluatedValue
1500
+ {
1501
+ $ result = self ::createFromExpression ($ expr , $ cConstName , $ allConstInfos );
1502
+
1503
+ if ($ result ->originatingConst !== null ) {
1504
+ $ result = self ::createFromExpressionRecursively ($ result ->originatingConst ->value , $ result ->cConstName , $ allConstInfos );
1505
+ }
1506
+
1507
+ return $ result ;
1508
+ }
1509
+
1496
1510
public static function null (): EvaluatedValue
1497
1511
{
1498
1512
return new self (null , null , null , false );
@@ -1755,8 +1769,7 @@ public function getCDeclaration(iterable $allConstInfos): ?string
1755
1769
$ code .= $ value ->cConstName ;
1756
1770
} else {
1757
1771
if ($ value ->isUnknownConstValue ) {
1758
- echo "Skipping C declaration generation for constant $ this ->name , because it has an unknown constant value \n" ;
1759
- return "" ;
1772
+ throw new Exception ("Cannot generate C declaration for constant $ this ->name , because it has an unknown constant value " );
1760
1773
}
1761
1774
1762
1775
switch ($ value ->type ) {
@@ -1788,7 +1801,6 @@ public function getDeclaration(iterable $allConstInfos): string
1788
1801
{
1789
1802
$ value = EvaluatedValue::createFromExpression ($ this ->value , $ this ->cname , $ allConstInfos );
1790
1803
if ($ value ->isUnknownConstValue ) {
1791
- echo "Skipping code generation for constant $ this ->name , because it has an unknown constant default value \n" ;
1792
1804
return "" ;
1793
1805
}
1794
1806
@@ -1817,24 +1829,29 @@ private function getConstDeclaration(string $code, EvaluatedValue $value): strin
1817
1829
$ constName = str_replace ('\\' , '\\\\' , $ this ->name ->__toString ());
1818
1830
$ constValue = $ value ->value ;
1819
1831
1832
+ $ flags = "CONST_CS | CONST_PERSISTENT " ;
1833
+ if ($ this ->isDeprecated ) {
1834
+ $ flags .= " | CONST_DEPRECATED " ;
1835
+ }
1836
+
1820
1837
switch ($ value ->type ) {
1821
1838
case "NULL " :
1822
- $ code .= " REGISTER_NULL_CONSTANT( \"$ constName \", CONST_CS | CONST_PERSISTENT ); \n" ;
1839
+ $ code .= " REGISTER_NULL_CONSTANT( \"$ constName \", $ flags ); \n" ;
1823
1840
break ;
1824
1841
case "boolean " :
1825
- $ code .= " REGISTER_BOOL_CONSTANT( \"$ constName \", " . ($ cConstName ?: ($ constValue ? "true " : "false " )) . ", CONST_CS | CONST_PERSISTENT ); \n" ;
1842
+ $ code .= " REGISTER_BOOL_CONSTANT( \"$ constName \", " . ($ cConstName ?: ($ constValue ? "true " : "false " )) . ", $ flags ); \n" ;
1826
1843
break ;
1827
1844
case "integer " :
1828
- $ code .= " REGISTER_LONG_CONSTANT( \"$ constName \", " . ($ cConstName ?: (int ) $ constValue ) . ", CONST_CS | CONST_PERSISTENT ); \n" ;
1845
+ $ code .= " REGISTER_LONG_CONSTANT( \"$ constName \", " . ($ cConstName ?: (int ) $ constValue ) . ", $ flags ); \n" ;
1829
1846
break ;
1830
1847
case "double " :
1831
- $ code .= " REGISTER_DOUBLE_CONSTANT( \"$ constName \", " . ($ cConstName ?: (int ) $ constValue ) . ", CONST_CS | CONST_PERSISTENT ); \n" ;
1848
+ $ code .= " REGISTER_DOUBLE_CONSTANT( \"$ constName \", " . ($ cConstName ?: (int ) $ constValue ) . ", $ flags ); \n" ;
1832
1849
break ;
1833
1850
case "string " :
1834
- $ code .= " REGISTER_STRING_CONSTANT( \"$ constName \", " . ($ cConstName ?: '" ' . addslashes ($ constValue ) . '" ' ) . ", CONST_CS | CONST_PERSISTENT ); \n" ;
1851
+ $ code .= " REGISTER_STRING_CONSTANT( \"$ constName \", " . ($ cConstName ?: '" ' . addslashes ($ constValue ) . '" ' ) . ", $ flags ); \n" ;
1835
1852
break ;
1836
1853
default :
1837
- throw new Exception ("Not implemented constant type " );
1854
+ throw new Exception ("Unimplemented constant type " );
1838
1855
}
1839
1856
1840
1857
return $ code ;
@@ -1966,13 +1983,16 @@ public function getDeclaration(iterable $allConstInfos): string {
1966
1983
1967
1984
$ propertyName = $ this ->name ->property ;
1968
1985
1986
+ $ isUnknownConstValue = false ;
1969
1987
if ($ this ->defaultValue === null ) {
1970
1988
$ defaultValue = EvaluatedValue::null ();
1971
1989
} else {
1972
1990
$ defaultValue = EvaluatedValue::createFromExpression ($ this ->defaultValue , null , $ allConstInfos );
1991
+ $ recursiveDefaultValue = EvaluatedValue::createFromExpressionRecursively ($ this ->defaultValue , null , $ allConstInfos );
1992
+ $ isUnknownConstValue = $ recursiveDefaultValue ->isUnknownConstValue ;
1973
1993
}
1974
1994
1975
- if ($ defaultValue -> isUnknownConstValue ) {
1995
+ if ($ isUnknownConstValue ) {
1976
1996
echo "Skipping code generation for property $ this ->name , because it has an unknown constant default value \n" ;
1977
1997
return "" ;
1978
1998
}
@@ -4089,8 +4109,6 @@ function initPhpParser() {
4089
4109
$ funcMap = [];
4090
4110
/** @var array<string, FuncInfo> $aliasMap */
4091
4111
$ aliasMap = [];
4092
- /** @var iterable<ConstInfo> $constInfo */
4093
- $ constInfos = [];
4094
4112
4095
4113
foreach ($ fileInfos as $ fileInfo ) {
4096
4114
foreach ($ fileInfo ->getAllFuncInfos () as $ funcInfo ) {
@@ -4106,8 +4124,6 @@ function initPhpParser() {
4106
4124
foreach ($ fileInfo ->classInfos as $ classInfo ) {
4107
4125
$ classMap [$ classInfo ->name ->__toString ()] = $ classInfo ;
4108
4126
}
4109
-
4110
- $ constInfos = array_merge ($ constInfos , $ fileInfo ->getAllConstInfos ());
4111
4127
}
4112
4128
4113
4129
if ($ verify ) {
@@ -4198,7 +4214,7 @@ function(?ArgInfo $aliasArg, ?ArgInfo $aliasedArg) use ($aliasFunc, $aliasedFunc
4198
4214
if ($ generateClassSynopses ) {
4199
4215
$ classSynopsesDirectory = getcwd () . "/classsynopses " ;
4200
4216
4201
- $ classSynopses = generateClassSynopses ($ classMap , $ constInfos );
4217
+ $ classSynopses = generateClassSynopses ($ classMap , $ context -> allConstInfos );
4202
4218
if (!empty ($ classSynopses )) {
4203
4219
if (!file_exists ($ classSynopsesDirectory )) {
4204
4220
mkdir ($ classSynopsesDirectory );
@@ -4213,7 +4229,7 @@ function(?ArgInfo $aliasArg, ?ArgInfo $aliasedArg) use ($aliasFunc, $aliasedFunc
4213
4229
}
4214
4230
4215
4231
if ($ replaceClassSynopses ) {
4216
- $ classSynopses = replaceClassSynopses ($ targetSynopses , $ classMap , $ constInfos );
4232
+ $ classSynopses = replaceClassSynopses ($ targetSynopses , $ classMap , $ context -> allConstInfos );
4217
4233
4218
4234
foreach ($ classSynopses as $ filename => $ content ) {
4219
4235
if (file_put_contents ($ filename , $ content )) {
0 commit comments