Skip to content

Commit 1bcd8d3

Browse files
committed
Update gen_stub to support #if around classes
1 parent bbc0c4c commit 1bcd8d3

File tree

10 files changed

+81
-70
lines changed

10 files changed

+81
-70
lines changed

build/gen_stub.php

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2277,6 +2277,8 @@ class ClassInfo {
22772277
public $funcInfos;
22782278
/** @var EnumCaseInfo[] */
22792279
public $enumCaseInfos;
2280+
/** @var string|null */
2281+
public $cond;
22802282

22812283
/**
22822284
* @param Name[] $extends
@@ -2300,7 +2302,8 @@ public function __construct(
23002302
array $constInfos,
23012303
array $propertyInfos,
23022304
array $funcInfos,
2303-
array $enumCaseInfos
2305+
array $enumCaseInfos,
2306+
?string $cond
23042307
) {
23052308
$this->name = $name;
23062309
$this->flags = $flags;
@@ -2316,6 +2319,7 @@ public function __construct(
23162319
$this->propertyInfos = $propertyInfos;
23172320
$this->funcInfos = $funcInfos;
23182321
$this->enumCaseInfos = $enumCaseInfos;
2322+
$this->cond = $cond;
23192323
}
23202324

23212325
/**
@@ -2333,7 +2337,13 @@ public function getRegistration(iterable $allConstInfos): string
23332337

23342338
$escapedName = implode("_", $this->name->parts);
23352339

2336-
$code = "static zend_class_entry *register_class_$escapedName(" . (empty($params) ? "void" : implode(", ", $params)) . ")\n";
2340+
$code = '';
2341+
2342+
if ($this->cond) {
2343+
$code .= "#if {$this->cond}\n";
2344+
}
2345+
2346+
$code .= "static zend_class_entry *register_class_$escapedName(" . (empty($params) ? "void" : implode(", ", $params)) . ")\n";
23372347

23382348
$code .= "{\n";
23392349
if ($this->type == "enum") {
@@ -2390,10 +2400,18 @@ function (Name $item) {
23902400
$code .= $property->getDeclaration($allConstInfos);
23912401
}
23922402

2403+
if ($attributeInitializationCode = generateAttributeInitialization($this->funcInfos, $this->cond)) {
2404+
$code .= "\n" . $attributeInitializationCode;
2405+
}
2406+
23932407
$code .= "\n\treturn class_entry;\n";
23942408

23952409
$code .= "}\n";
23962410

2411+
if ($this->cond) {
2412+
$code .= "#endif\n";
2413+
}
2414+
23972415
return $code;
23982416
}
23992417

@@ -3237,7 +3255,8 @@ function parseClass(
32373255
array $consts,
32383256
array $properties,
32393257
array $methods,
3240-
array $enumCases
3258+
array $enumCases,
3259+
?string $cond
32413260
): ClassInfo {
32423261
$flags = $class instanceof Class_ ? $class->flags : 0;
32433262
$comment = $class->getDocComment();
@@ -3297,7 +3316,8 @@ function parseClass(
32973316
$consts,
32983317
$properties,
32993318
$methods,
3300-
$enumCases
3319+
$enumCases,
3320+
$cond
33013321
);
33023322
}
33033323

@@ -3447,7 +3467,7 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac
34473467
}
34483468

34493469
$fileInfo->classInfos[] = parseClass(
3450-
$className, $stmt, $constInfos, $propertyInfos, $methodInfos, $enumCaseInfos
3470+
$className, $stmt, $constInfos, $propertyInfos, $methodInfos, $enumCaseInfos, $cond
34513471
);
34523472
continue;
34533473
}
@@ -3613,9 +3633,10 @@ function findEquivalentFuncInfo(array $generatedFuncInfos, FuncInfo $funcInfo):
36133633
* @template T
36143634
* @param iterable<T> $infos
36153635
* @param Closure(T): string|null $codeGenerator
3636+
* @param ?string $parentCond
36163637
*/
36173638
function generateCodeWithConditions(
3618-
iterable $infos, string $separator, Closure $codeGenerator): string {
3639+
iterable $infos, string $separator, Closure $codeGenerator, ?string $parentCond = null): string {
36193640
$code = "";
36203641
foreach ($infos as $info) {
36213642
$infoCode = $codeGenerator($info);
@@ -3624,7 +3645,7 @@ function generateCodeWithConditions(
36243645
}
36253646

36263647
$code .= $separator;
3627-
if ($info->cond) {
3648+
if ($info->cond && $info->cond !== $parentCond) {
36283649
$code .= "#if {$info->cond}\n";
36293650
$code .= $infoCode;
36303651
$code .= "#endif\n";
@@ -3689,21 +3710,15 @@ static function (FuncInfo $funcInfo) use ($fileInfo, &$generatedFunctionDeclarat
36893710
}
36903711

36913712
foreach ($fileInfo->classInfos as $classInfo) {
3692-
$code .= generateFunctionEntries($classInfo->name, $classInfo->funcInfos);
3713+
$code .= generateFunctionEntries($classInfo->name, $classInfo->funcInfos, $classInfo->cond);
36933714
}
36943715
}
36953716

36963717
if ($fileInfo->generateClassEntries) {
3697-
$classEntriesWithSensitiveParams = [];
3698-
$attributeInitializationCode = generateAttributeInitialization($fileInfo, $classEntriesWithSensitiveParams);
3718+
$attributeInitializationCode = generateAttributeInitialization($fileInfo->funcInfos);
36993719

37003720
if ($attributeInitializationCode !== "" || !empty($fileInfo->constInfos)) {
3701-
$classEntriesWithSensitiveParams = array_unique($classEntriesWithSensitiveParams);
3702-
$code .= "\nstatic void register_{$stubFilenameWithoutExtension}_symbols(int module_number";
3703-
foreach ($classEntriesWithSensitiveParams as $ce) {
3704-
$code .= ", zend_class_entry *$ce";
3705-
}
3706-
$code .= ")\n";
3721+
$code .= "\nstatic void register_{$stubFilenameWithoutExtension}_symbols(int module_number)\n";
37073722
$code .= "{\n";
37083723

37093724
foreach ($fileInfo->constInfos as $constInfo) {
@@ -3738,30 +3753,40 @@ function generateClassEntryCode(FileInfo $fileInfo, iterable $allConstInfos): st
37383753
}
37393754

37403755
/** @param FuncInfo[] $funcInfos */
3741-
function generateFunctionEntries(?Name $className, array $funcInfos): string {
3742-
$code = "";
3756+
function generateFunctionEntries(?Name $className, array $funcInfos, ?string $cond = null): string {
3757+
$code = "\n\n";
3758+
3759+
if ($cond) {
3760+
$code .= "#if {$cond}\n";
3761+
}
37433762

37443763
$functionEntryName = "ext_functions";
37453764
if ($className) {
37463765
$underscoreName = implode("_", $className->parts);
37473766
$functionEntryName = "class_{$underscoreName}_methods";
37483767
}
37493768

3750-
$code .= "\n\nstatic const zend_function_entry {$functionEntryName}[] = {\n";
3769+
$code .= "static const zend_function_entry {$functionEntryName}[] = {\n";
37513770
$code .= generateCodeWithConditions($funcInfos, "", static function (FuncInfo $funcInfo) {
37523771
return $funcInfo->getFunctionEntry();
3753-
});
3772+
}, $cond);
37543773
$code .= "\tZEND_FE_END\n";
37553774
$code .= "};\n";
37563775

3776+
if ($cond) {
3777+
$code .= "#endif\n";
3778+
}
3779+
37573780
return $code;
37583781
}
3759-
3760-
function generateAttributeInitialization(FileInfo $fileInfo, array &$classEntriesWithSensitiveParams): string {
3782+
/**
3783+
* @param iterable<FuncInfo> $funcInfos
3784+
*/
3785+
function generateAttributeInitialization(iterable $funcInfos, ?string $parentCond = null): string {
37613786
return generateCodeWithConditions(
3762-
$fileInfo->getAllFuncInfos(),
3787+
$funcInfos,
37633788
"",
3764-
static function (FuncInfo $funcInfo) use (&$classEntriesWithSensitiveParams) {
3789+
static function (FuncInfo $funcInfo) {
37653790
$code = null;
37663791

37673792
foreach ($funcInfo->args as $index => $arg) {
@@ -3770,9 +3795,7 @@ static function (FuncInfo $funcInfo) use (&$classEntriesWithSensitiveParams) {
37703795
}
37713796

37723797
if ($funcInfo->name instanceof MethodName) {
3773-
$classEntry = "class_entry_" . str_replace("\\", "_", $funcInfo->name->className->toString());
3774-
$functionTable = "&{$classEntry}->function_table";
3775-
$classEntriesWithSensitiveParams[] = $classEntry;
3798+
$functionTable = "&class_entry->function_table";
37763799
} else {
37773800
$functionTable = "CG(function_table)";
37783801
}
@@ -3781,7 +3804,8 @@ static function (FuncInfo $funcInfo) use (&$classEntriesWithSensitiveParams) {
37813804
}
37823805

37833806
return $code;
3784-
}
3807+
},
3808+
$parentCond
37853809
);
37863810
}
37873811

ext/com_dotnet/com_extension_arginfo.h

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/dom/php_dom_arginfo.h

Lines changed: 4 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/mysqli/mysqli.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ PHP_MINIT_FUNCTION(mysqli)
694694
REGISTER_BOOL_CONSTANT("MYSQLI_IS_MARIADB", 0, CONST_CS | CONST_PERSISTENT);
695695
#endif
696696

697-
register_mysqli_symbols(module_number, mysqli_link_class_entry);
697+
register_mysqli_symbols(module_number);
698698

699699
mysqlnd_reverse_api_register_api(&mysqli_reverse_api);
700700

ext/mysqli/mysqli_arginfo.h

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/pdo/pdo_dbh.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,8 +1424,6 @@ void pdo_dbh_init(int module_number)
14241424

14251425
REGISTER_PDO_CLASS_CONST_LONG("CURSOR_FWDONLY", (zend_long)PDO_CURSOR_FWDONLY);
14261426
REGISTER_PDO_CLASS_CONST_LONG("CURSOR_SCROLL", (zend_long)PDO_CURSOR_SCROLL);
1427-
1428-
register_pdo_dbh_symbols(module_number, pdo_dbh_ce);
14291427
}
14301428

14311429
static void dbh_free(pdo_dbh_t *dbh, bool free_persistent)

ext/pdo/pdo_dbh_arginfo.h

Lines changed: 2 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/spl/spl_directory_arginfo.h

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/zip/php_zip.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3256,8 +3256,6 @@ static PHP_MINIT_FUNCTION(zip)
32563256
le_zip_dir = zend_register_list_destructors_ex(php_zip_free_dir, NULL, le_zip_dir_name, module_number);
32573257
le_zip_entry = zend_register_list_destructors_ex(php_zip_free_entry, NULL, le_zip_entry_name, module_number);
32583258

3259-
register_php_zip_symbols(module_number, zip_class_entry);
3260-
32613259
return SUCCESS;
32623260
}
32633261
/* }}} */

0 commit comments

Comments
 (0)