Skip to content

Commit f1dd3f2

Browse files
gen_stub: move generateArgInfoCode() into FileInfo
Reduce the number of global functions by moving it to instance method `FileInfo::generateArgInfoCode()`. In the process, make the following parts of `FileInfo` private: - `$funcInfos` - `$generateFunctionEntries` - `$declarationPrefix` - `$generateClassEntries` - `$generateCEnums` - `::getMinimumPhpVersionIdCompatibility()`
1 parent 777fa5d commit f1dd3f2

1 file changed

Lines changed: 132 additions & 134 deletions

File tree

build/gen_stub.php

Lines changed: 132 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,8 @@ function processStubFile(string $stubFile, Context $context, bool $includeOnly =
144144
return $fileInfo;
145145
}
146146

147-
[$arginfoCode, $declCode] = generateArgInfoCode(
147+
[$arginfoCode, $declCode] = $fileInfo->generateArgInfoCode(
148148
basename($stubFilenameWithoutExtension),
149-
$fileInfo,
150149
$context->allConstInfos,
151150
$stubHash
152151
);
@@ -162,9 +161,8 @@ function processStubFile(string $stubFile, Context $context, bool $includeOnly =
162161
if ($fileInfo->shouldGenerateLegacyArginfo()) {
163162
$legacyFileInfo = $fileInfo->getLegacyVersion();
164163

165-
[$arginfoCode] = generateArgInfoCode(
164+
[$arginfoCode] = $legacyFileInfo->generateArgInfoCode(
166165
basename($stubFilenameWithoutExtension),
167-
$legacyFileInfo,
168166
$context->allConstInfos,
169167
$stubHash
170168
);
@@ -4270,13 +4268,13 @@ class FileInfo {
42704268
/** @var ConstInfo[] */
42714269
public array $constInfos = [];
42724270
/** @var FuncInfo[] */
4273-
public array $funcInfos = [];
4271+
private array $funcInfos = [];
42744272
/** @var ClassInfo[] */
42754273
public array $classInfos = [];
4276-
public bool $generateFunctionEntries = false;
4277-
public string $declarationPrefix = "";
4278-
public bool $generateClassEntries = false;
4279-
public bool $generateCEnums = false;
4274+
private bool $generateFunctionEntries = false;
4275+
private string $declarationPrefix = "";
4276+
private bool $generateClassEntries = false;
4277+
private bool $generateCEnums = false;
42804278
private bool $isUndocumentable = false;
42814279
private bool $legacyArginfoGeneration = false;
42824280
private ?int $minimumPhpVersionIdCompatibility = null;
@@ -4355,7 +4353,7 @@ public function __clone()
43554353
}
43564354
}
43574355

4358-
public function getMinimumPhpVersionIdCompatibility(): ?int {
4356+
private function getMinimumPhpVersionIdCompatibility(): ?int {
43594357
// Non-legacy arginfo files are always PHP 8.0+ compatible
43604358
if (!$this->legacyArginfoGeneration &&
43614359
$this->minimumPhpVersionIdCompatibility !== null &&
@@ -4619,6 +4617,130 @@ public function generateCDeclarations(): string {
46194617

46204618
return $code;
46214619
}
4620+
4621+
4622+
/**
4623+
* @param array<string, ConstInfo> $allConstInfos
4624+
* @return array{string, string}
4625+
*/
4626+
public function generateArgInfoCode(
4627+
string $stubFilenameWithoutExtension,
4628+
array $allConstInfos,
4629+
string $stubHash
4630+
): array {
4631+
$code = "";
4632+
4633+
$generatedFuncInfos = [];
4634+
4635+
$argInfoCode = generateCodeWithConditions(
4636+
$this->getAllFuncInfos(), "\n",
4637+
function (FuncInfo $funcInfo) use (&$generatedFuncInfos) {
4638+
/* If there already is an equivalent arginfo structure, only emit a #define */
4639+
if ($generatedFuncInfo = $funcInfo->findEquivalent($generatedFuncInfos)) {
4640+
$code = sprintf(
4641+
"#define %s %s\n",
4642+
$funcInfo->getArgInfoName(), $generatedFuncInfo->getArgInfoName()
4643+
);
4644+
} else {
4645+
$code = $funcInfo->toArgInfoCode($this->getMinimumPhpVersionIdCompatibility());
4646+
}
4647+
4648+
$generatedFuncInfos[] = $funcInfo;
4649+
return $code;
4650+
}
4651+
);
4652+
4653+
if ($argInfoCode !== "") {
4654+
$code .= "$argInfoCode\n";
4655+
}
4656+
4657+
if ($this->generateFunctionEntries) {
4658+
$framelessFunctionCode = generateCodeWithConditions(
4659+
$this->getAllFuncInfos(), "\n",
4660+
static function (FuncInfo $funcInfo) {
4661+
return $funcInfo->getFramelessDeclaration();
4662+
}
4663+
);
4664+
4665+
if ($framelessFunctionCode !== "") {
4666+
$code .= "$framelessFunctionCode\n";
4667+
}
4668+
4669+
$generatedFunctionDeclarations = [];
4670+
$code .= generateCodeWithConditions(
4671+
$this->getAllFuncInfos(), "",
4672+
function (FuncInfo $funcInfo) use (&$generatedFunctionDeclarations) {
4673+
$key = $funcInfo->getDeclarationKey();
4674+
if (isset($generatedFunctionDeclarations[$key])) {
4675+
return null;
4676+
}
4677+
4678+
$generatedFunctionDeclarations[$key] = true;
4679+
return $this->declarationPrefix . $funcInfo->getDeclaration();
4680+
}
4681+
);
4682+
4683+
$code .= generateFunctionEntries(null, $this->funcInfos);
4684+
4685+
foreach ($this->classInfos as $classInfo) {
4686+
$code .= generateFunctionEntries($classInfo->name, $classInfo->funcInfos, $classInfo->cond);
4687+
}
4688+
}
4689+
4690+
$php80MinimumCompatibility = $this->getMinimumPhpVersionIdCompatibility() === null || $this->getMinimumPhpVersionIdCompatibility() >= PHP_80_VERSION_ID;
4691+
4692+
if ($this->generateClassEntries) {
4693+
$declaredStrings = [];
4694+
$attributeInitializationCode = generateFunctionAttributeInitialization($this->funcInfos, $allConstInfos, $this->getMinimumPhpVersionIdCompatibility(), null, $declaredStrings);
4695+
$attributeInitializationCode .= generateGlobalConstantAttributeInitialization($this->constInfos, $allConstInfos, $this->getMinimumPhpVersionIdCompatibility(), null, $declaredStrings);
4696+
if ($attributeInitializationCode) {
4697+
if (!$php80MinimumCompatibility) {
4698+
$attributeInitializationCode = "\n#if (PHP_VERSION_ID >= " . PHP_80_VERSION_ID . ")" . $attributeInitializationCode . "#endif\n";
4699+
}
4700+
}
4701+
4702+
if ($attributeInitializationCode !== "" || !empty($this->constInfos)) {
4703+
$code .= "\nstatic void register_{$stubFilenameWithoutExtension}_symbols(int module_number)\n";
4704+
$code .= "{\n";
4705+
4706+
$code .= generateCodeWithConditions(
4707+
$this->constInfos,
4708+
'',
4709+
static fn (ConstInfo $constInfo): string => $constInfo->getDeclaration($allConstInfos)
4710+
);
4711+
4712+
if ($attributeInitializationCode !== "" && $this->constInfos) {
4713+
$code .= "\n";
4714+
}
4715+
4716+
$code .= $attributeInitializationCode;
4717+
$code .= "}\n";
4718+
}
4719+
4720+
$code .= $this->generateClassEntryCode($allConstInfos);
4721+
}
4722+
4723+
$hasDeclFile = false;
4724+
$declCode = $this->generateCDeclarations();
4725+
if ($declCode !== '') {
4726+
$hasDeclFile = true;
4727+
$headerName = "ZEND_" . strtoupper($stubFilenameWithoutExtension) . "_DECL_{$stubHash}_H";
4728+
$declCode = "/* This is a generated file, edit {$stubFilenameWithoutExtension}.stub.php instead.\n"
4729+
. " * Stub hash: $stubHash */\n"
4730+
. "\n"
4731+
. "#ifndef {$headerName}\n"
4732+
. "#define {$headerName}\n"
4733+
. $declCode . "\n"
4734+
. "#endif /* {$headerName} */\n";
4735+
}
4736+
4737+
$code = "/* This is a generated file, edit {$stubFilenameWithoutExtension}.stub.php instead.\n"
4738+
. " * Stub hash: $stubHash"
4739+
. ($hasDeclFile ? "\n * Has decl header: yes */\n" : " */\n")
4740+
. $code;
4741+
4742+
return [$code, $declCode];
4743+
}
46224744
}
46234745

46244746
class DocCommentTag {
@@ -5248,130 +5370,6 @@ function generateCodeWithConditions(
52485370
return $code;
52495371
}
52505372

5251-
/**
5252-
* @param array<string, ConstInfo> $allConstInfos
5253-
* @return array{string, string}
5254-
*/
5255-
function generateArgInfoCode(
5256-
string $stubFilenameWithoutExtension,
5257-
FileInfo $fileInfo,
5258-
array $allConstInfos,
5259-
string $stubHash
5260-
): array {
5261-
$code = "";
5262-
5263-
$generatedFuncInfos = [];
5264-
5265-
$argInfoCode = generateCodeWithConditions(
5266-
$fileInfo->getAllFuncInfos(), "\n",
5267-
static function (FuncInfo $funcInfo) use (&$generatedFuncInfos, $fileInfo) {
5268-
/* If there already is an equivalent arginfo structure, only emit a #define */
5269-
if ($generatedFuncInfo = $funcInfo->findEquivalent($generatedFuncInfos)) {
5270-
$code = sprintf(
5271-
"#define %s %s\n",
5272-
$funcInfo->getArgInfoName(), $generatedFuncInfo->getArgInfoName()
5273-
);
5274-
} else {
5275-
$code = $funcInfo->toArgInfoCode($fileInfo->getMinimumPhpVersionIdCompatibility());
5276-
}
5277-
5278-
$generatedFuncInfos[] = $funcInfo;
5279-
return $code;
5280-
}
5281-
);
5282-
5283-
if ($argInfoCode !== "") {
5284-
$code .= "$argInfoCode\n";
5285-
}
5286-
5287-
if ($fileInfo->generateFunctionEntries) {
5288-
$framelessFunctionCode = generateCodeWithConditions(
5289-
$fileInfo->getAllFuncInfos(), "\n",
5290-
static function (FuncInfo $funcInfo) {
5291-
return $funcInfo->getFramelessDeclaration();
5292-
}
5293-
);
5294-
5295-
if ($framelessFunctionCode !== "") {
5296-
$code .= "$framelessFunctionCode\n";
5297-
}
5298-
5299-
$generatedFunctionDeclarations = [];
5300-
$code .= generateCodeWithConditions(
5301-
$fileInfo->getAllFuncInfos(), "",
5302-
static function (FuncInfo $funcInfo) use ($fileInfo, &$generatedFunctionDeclarations) {
5303-
$key = $funcInfo->getDeclarationKey();
5304-
if (isset($generatedFunctionDeclarations[$key])) {
5305-
return null;
5306-
}
5307-
5308-
$generatedFunctionDeclarations[$key] = true;
5309-
return $fileInfo->declarationPrefix . $funcInfo->getDeclaration();
5310-
}
5311-
);
5312-
5313-
$code .= generateFunctionEntries(null, $fileInfo->funcInfos);
5314-
5315-
foreach ($fileInfo->classInfos as $classInfo) {
5316-
$code .= generateFunctionEntries($classInfo->name, $classInfo->funcInfos, $classInfo->cond);
5317-
}
5318-
}
5319-
5320-
$php80MinimumCompatibility = $fileInfo->getMinimumPhpVersionIdCompatibility() === null || $fileInfo->getMinimumPhpVersionIdCompatibility() >= PHP_80_VERSION_ID;
5321-
5322-
if ($fileInfo->generateClassEntries) {
5323-
$declaredStrings = [];
5324-
$attributeInitializationCode = generateFunctionAttributeInitialization($fileInfo->funcInfos, $allConstInfos, $fileInfo->getMinimumPhpVersionIdCompatibility(), null, $declaredStrings);
5325-
$attributeInitializationCode .= generateGlobalConstantAttributeInitialization($fileInfo->constInfos, $allConstInfos, $fileInfo->getMinimumPhpVersionIdCompatibility(), null, $declaredStrings);
5326-
if ($attributeInitializationCode) {
5327-
if (!$php80MinimumCompatibility) {
5328-
$attributeInitializationCode = "\n#if (PHP_VERSION_ID >= " . PHP_80_VERSION_ID . ")" . $attributeInitializationCode . "#endif\n";
5329-
}
5330-
}
5331-
5332-
if ($attributeInitializationCode !== "" || !empty($fileInfo->constInfos)) {
5333-
$code .= "\nstatic void register_{$stubFilenameWithoutExtension}_symbols(int module_number)\n";
5334-
$code .= "{\n";
5335-
5336-
$code .= generateCodeWithConditions(
5337-
$fileInfo->constInfos,
5338-
'',
5339-
static fn (ConstInfo $constInfo): string => $constInfo->getDeclaration($allConstInfos)
5340-
);
5341-
5342-
if ($attributeInitializationCode !== "" && $fileInfo->constInfos) {
5343-
$code .= "\n";
5344-
}
5345-
5346-
$code .= $attributeInitializationCode;
5347-
$code .= "}\n";
5348-
}
5349-
5350-
$code .= $fileInfo->generateClassEntryCode($allConstInfos);
5351-
}
5352-
5353-
$hasDeclFile = false;
5354-
$declCode = $fileInfo->generateCDeclarations();
5355-
if ($declCode !== '') {
5356-
$hasDeclFile = true;
5357-
$headerName = "ZEND_" . strtoupper($stubFilenameWithoutExtension) . "_DECL_{$stubHash}_H";
5358-
$declCode = "/* This is a generated file, edit {$stubFilenameWithoutExtension}.stub.php instead.\n"
5359-
. " * Stub hash: $stubHash */\n"
5360-
. "\n"
5361-
. "#ifndef {$headerName}\n"
5362-
. "#define {$headerName}\n"
5363-
. $declCode . "\n"
5364-
. "#endif /* {$headerName} */\n";
5365-
}
5366-
5367-
$code = "/* This is a generated file, edit {$stubFilenameWithoutExtension}.stub.php instead.\n"
5368-
. " * Stub hash: $stubHash"
5369-
. ($hasDeclFile ? "\n * Has decl header: yes */\n" : " */\n")
5370-
. $code;
5371-
5372-
return [$code, $declCode];
5373-
}
5374-
53755373
/** @param FuncInfo[] $funcInfos */
53765374
function generateFunctionEntries(?Name $className, array $funcInfos, ?string $cond = null): string {
53775375
// No need to add anything if there are no function entries

0 commit comments

Comments
 (0)