Skip to content

Update gen_stub to support #if around classes #8736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 52 additions & 28 deletions build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2277,6 +2277,8 @@ class ClassInfo {
public $funcInfos;
/** @var EnumCaseInfo[] */
public $enumCaseInfos;
/** @var string|null */
public $cond;

/**
* @param Name[] $extends
Expand All @@ -2300,7 +2302,8 @@ public function __construct(
array $constInfos,
array $propertyInfos,
array $funcInfos,
array $enumCaseInfos
array $enumCaseInfos,
?string $cond
) {
$this->name = $name;
$this->flags = $flags;
Expand All @@ -2316,6 +2319,7 @@ public function __construct(
$this->propertyInfos = $propertyInfos;
$this->funcInfos = $funcInfos;
$this->enumCaseInfos = $enumCaseInfos;
$this->cond = $cond;
}

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

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

$code = "static zend_class_entry *register_class_$escapedName(" . (empty($params) ? "void" : implode(", ", $params)) . ")\n";
$code = '';

if ($this->cond) {
$code .= "#if {$this->cond}\n";
}

$code .= "static zend_class_entry *register_class_$escapedName(" . (empty($params) ? "void" : implode(", ", $params)) . ")\n";

$code .= "{\n";
if ($this->type == "enum") {
Expand Down Expand Up @@ -2390,10 +2400,18 @@ function (Name $item) {
$code .= $property->getDeclaration($allConstInfos);
}

if ($attributeInitializationCode = generateAttributeInitialization($this->funcInfos, $this->cond)) {
$code .= "\n" . $attributeInitializationCode;
}

$code .= "\n\treturn class_entry;\n";

$code .= "}\n";

if ($this->cond) {
$code .= "#endif\n";
}

return $code;
}

Expand Down Expand Up @@ -3237,7 +3255,8 @@ function parseClass(
array $consts,
array $properties,
array $methods,
array $enumCases
array $enumCases,
?string $cond
): ClassInfo {
$flags = $class instanceof Class_ ? $class->flags : 0;
$comment = $class->getDocComment();
Expand Down Expand Up @@ -3297,7 +3316,8 @@ function parseClass(
$consts,
$properties,
$methods,
$enumCases
$enumCases,
$cond
);
}

Expand Down Expand Up @@ -3447,7 +3467,7 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac
}

$fileInfo->classInfos[] = parseClass(
$className, $stmt, $constInfos, $propertyInfos, $methodInfos, $enumCaseInfos
$className, $stmt, $constInfos, $propertyInfos, $methodInfos, $enumCaseInfos, $cond
);
continue;
}
Expand Down Expand Up @@ -3613,9 +3633,10 @@ function findEquivalentFuncInfo(array $generatedFuncInfos, FuncInfo $funcInfo):
* @template T
* @param iterable<T> $infos
* @param Closure(T): string|null $codeGenerator
* @param ?string $parentCond
*/
function generateCodeWithConditions(
iterable $infos, string $separator, Closure $codeGenerator): string {
iterable $infos, string $separator, Closure $codeGenerator, ?string $parentCond = null): string {
$code = "";
foreach ($infos as $info) {
$infoCode = $codeGenerator($info);
Expand All @@ -3624,7 +3645,7 @@ function generateCodeWithConditions(
}

$code .= $separator;
if ($info->cond) {
if ($info->cond && $info->cond !== $parentCond) {
$code .= "#if {$info->cond}\n";
$code .= $infoCode;
$code .= "#endif\n";
Expand Down Expand Up @@ -3689,21 +3710,15 @@ static function (FuncInfo $funcInfo) use ($fileInfo, &$generatedFunctionDeclarat
}

foreach ($fileInfo->classInfos as $classInfo) {
$code .= generateFunctionEntries($classInfo->name, $classInfo->funcInfos);
$code .= generateFunctionEntries($classInfo->name, $classInfo->funcInfos, $classInfo->cond);
}
}

if ($fileInfo->generateClassEntries) {
$classEntriesWithSensitiveParams = [];
$attributeInitializationCode = generateAttributeInitialization($fileInfo, $classEntriesWithSensitiveParams);
$attributeInitializationCode = generateAttributeInitialization($fileInfo->funcInfos);

if ($attributeInitializationCode !== "" || !empty($fileInfo->constInfos)) {
$classEntriesWithSensitiveParams = array_unique($classEntriesWithSensitiveParams);
$code .= "\nstatic void register_{$stubFilenameWithoutExtension}_symbols(int module_number";
foreach ($classEntriesWithSensitiveParams as $ce) {
$code .= ", zend_class_entry *$ce";
}
$code .= ")\n";
$code .= "\nstatic void register_{$stubFilenameWithoutExtension}_symbols(int module_number)\n";
$code .= "{\n";

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

/** @param FuncInfo[] $funcInfos */
function generateFunctionEntries(?Name $className, array $funcInfos): string {
$code = "";
function generateFunctionEntries(?Name $className, array $funcInfos, ?string $cond = null): string {
$code = "\n\n";

if ($cond) {
$code .= "#if {$cond}\n";
}

$functionEntryName = "ext_functions";
if ($className) {
$underscoreName = implode("_", $className->parts);
$functionEntryName = "class_{$underscoreName}_methods";
}

$code .= "\n\nstatic const zend_function_entry {$functionEntryName}[] = {\n";
$code .= "static const zend_function_entry {$functionEntryName}[] = {\n";
$code .= generateCodeWithConditions($funcInfos, "", static function (FuncInfo $funcInfo) {
return $funcInfo->getFunctionEntry();
});
}, $cond);
$code .= "\tZEND_FE_END\n";
$code .= "};\n";

if ($cond) {
$code .= "#endif\n";
}

return $code;
}

function generateAttributeInitialization(FileInfo $fileInfo, array &$classEntriesWithSensitiveParams): string {
/**
* @param iterable<FuncInfo> $funcInfos
*/
function generateAttributeInitialization(iterable $funcInfos, ?string $parentCond = null): string {
return generateCodeWithConditions(
$fileInfo->getAllFuncInfos(),
$funcInfos,
"",
static function (FuncInfo $funcInfo) use (&$classEntriesWithSensitiveParams) {
static function (FuncInfo $funcInfo) {
$code = null;

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

if ($funcInfo->name instanceof MethodName) {
$classEntry = "class_entry_" . str_replace("\\", "_", $funcInfo->name->className->toString());
$functionTable = "&{$classEntry}->function_table";
$classEntriesWithSensitiveParams[] = $classEntry;
$functionTable = "&class_entry->function_table";
} else {
$functionTable = "CG(function_table)";
}
Expand All @@ -3781,7 +3804,8 @@ static function (FuncInfo $funcInfo) use (&$classEntriesWithSensitiveParams) {
}

return $code;
}
},
$parentCond
);
}

Expand Down
6 changes: 4 additions & 2 deletions ext/com_dotnet/com_extension_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 4 additions & 10 deletions ext/dom/php_dom_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ext/mysqli/mysqli.c
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ PHP_MINIT_FUNCTION(mysqli)
REGISTER_BOOL_CONSTANT("MYSQLI_IS_MARIADB", 0, CONST_CS | CONST_PERSISTENT);
#endif

register_mysqli_symbols(module_number, mysqli_link_class_entry);
register_mysqli_symbols(module_number);

mysqlnd_reverse_api_register_api(&mysqli_reverse_api);

Expand Down
11 changes: 6 additions & 5 deletions ext/mysqli/mysqli_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions ext/pdo/pdo_dbh.c
Original file line number Diff line number Diff line change
Expand Up @@ -1424,8 +1424,6 @@ void pdo_dbh_init(int module_number)

REGISTER_PDO_CLASS_CONST_LONG("CURSOR_FWDONLY", (zend_long)PDO_CURSOR_FWDONLY);
REGISTER_PDO_CLASS_CONST_LONG("CURSOR_SCROLL", (zend_long)PDO_CURSOR_SCROLL);

register_pdo_dbh_symbols(module_number, pdo_dbh_ce);
}

static void dbh_free(pdo_dbh_t *dbh, bool free_persistent)
Expand Down
7 changes: 2 additions & 5 deletions ext/pdo/pdo_dbh_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions ext/spl/spl_directory_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions ext/zip/php_zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -3256,8 +3256,6 @@ static PHP_MINIT_FUNCTION(zip)
le_zip_dir = zend_register_list_destructors_ex(php_zip_free_dir, NULL, le_zip_dir_name, module_number);
le_zip_entry = zend_register_list_destructors_ex(php_zip_free_entry, NULL, le_zip_entry_name, module_number);

register_php_zip_symbols(module_number, zip_class_entry);

return SUCCESS;
}
/* }}} */
Expand Down
Loading