Skip to content

Commit d2c92d7

Browse files
committed
Stubs: Store information per-class
We'll need this if we want to generate method entries.
1 parent 2bfcd88 commit d2c92d7

File tree

4 files changed

+308
-284
lines changed

4 files changed

+308
-284
lines changed

build/gen_stub.php

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -350,16 +350,39 @@ public function getArgInfoName(): string {
350350
}
351351
}
352352

353+
class ClassInfo {
354+
/** @var string */
355+
public $name;
356+
/** @var FuncInfo[] */
357+
public $funcInfos;
358+
359+
public function __construct(string $name, array $funcInfos) {
360+
$this->name = $name;
361+
$this->funcInfos = $funcInfos;
362+
}
363+
}
364+
353365
class FileInfo {
354366
/** @var FuncInfo[] */
355367
public $funcInfos;
368+
/** @var ClassInfo[] */
369+
public $classInfos;
356370
/** @var bool */
357371
public $generateFunctionEntries;
358372

359-
public function __construct(array $funcInfos, bool $generateFunctionEntries) {
373+
public function __construct(
374+
array $funcInfos, array $classInfos, bool $generateFunctionEntries) {
360375
$this->funcInfos = $funcInfos;
376+
$this->classInfos = $classInfos;
361377
$this->generateFunctionEntries = $generateFunctionEntries;
362378
}
379+
380+
public function getAllFuncInfos(): iterable {
381+
yield from $this->funcInfos;
382+
foreach ($this->classInfos as $classInfo) {
383+
yield from $classInfo->funcInfos;
384+
}
385+
}
363386
}
364387

365388
function parseFunctionLike(
@@ -507,6 +530,7 @@ function parseStubFile(string $fileName): FileInfo {
507530
}
508531

509532
$funcInfos = [];
533+
$classInfos = [];
510534
$conds = [];
511535
foreach ($stmts as $stmt) {
512536
$cond = handlePreprocessorConditions($conds, $stmt);
@@ -521,6 +545,7 @@ function parseStubFile(string $fileName): FileInfo {
521545

522546
if ($stmt instanceof Stmt\ClassLike) {
523547
$className = $stmt->name->toString();
548+
$methodInfos = [];
524549
foreach ($stmt->stmts as $classStmt) {
525550
$cond = handlePreprocessorConditions($conds, $classStmt);
526551
if ($classStmt instanceof Stmt\Nop) {
@@ -531,16 +556,18 @@ function parseStubFile(string $fileName): FileInfo {
531556
throw new Exception("Not implemented {$classStmt->getType()}");
532557
}
533558

534-
$funcInfos[] = parseFunctionLike(
559+
$methodInfos[] = parseFunctionLike(
535560
$classStmt->name->toString(), $className, $classStmt, $cond);
536561
}
562+
563+
$classInfos[] = new ClassInfo($className, $methodInfos);
537564
continue;
538565
}
539566

540567
throw new Exception("Unexpected node {$stmt->getType()}");
541568
}
542569

543-
return new FileInfo($funcInfos, $generateFunctionEntries);
570+
return new FileInfo($funcInfos, $classInfos, $generateFunctionEntries);
544571
}
545572

546573
function funcInfoToCode(FuncInfo $funcInfo): string {
@@ -638,10 +665,11 @@ function findEquivalentFuncInfo(array $generatedFuncInfos, $funcInfo): ?FuncInfo
638665
return null;
639666
}
640667

668+
/** @param FuncInfo[] $funcInfos */
641669
function generateCodeWithConditions(
642-
FileInfo $fileInfo, string $separator, Closure $codeGenerator): string {
670+
iterable $funcInfos, string $separator, Closure $codeGenerator): string {
643671
$code = "";
644-
foreach ($fileInfo->funcInfos as $funcInfo) {
672+
foreach ($funcInfos as $funcInfo) {
645673
$funcCode = $codeGenerator($funcInfo);
646674
if ($funcCode === null) {
647675
continue;
@@ -665,7 +693,7 @@ function generateArgInfoCode(FileInfo $fileInfo): string {
665693
$code = "/* This is a generated file, edit the .stub.php file instead. */\n";
666694
$generatedFuncInfos = [];
667695
$code .= generateCodeWithConditions(
668-
$fileInfo, "\n",
696+
$fileInfo->getAllFuncInfos(), "\n",
669697
function(FuncInfo $funcInfo) use(&$generatedFuncInfos) {
670698
/* If there already is an equivalent arginfo structure, only emit a #define */
671699
if ($generatedFuncInfo = findEquivalentFuncInfo($generatedFuncInfos, $funcInfo)) {
@@ -684,20 +712,16 @@ function(FuncInfo $funcInfo) use(&$generatedFuncInfos) {
684712

685713
if ($fileInfo->generateFunctionEntries) {
686714
$code .= "\n\n";
687-
$code .= generateCodeWithConditions($fileInfo, "", function(FuncInfo $funcInfo) {
688-
if ($funcInfo->className || $funcInfo->alias) {
715+
$code .= generateCodeWithConditions($fileInfo->funcInfos, "", function(FuncInfo $funcInfo) {
716+
if ($funcInfo->alias) {
689717
return null;
690718
}
691719

692720
return "ZEND_FUNCTION($funcInfo->name);\n";
693721
});
694722

695723
$code .= "\n\nstatic const zend_function_entry ext_functions[] = {\n";
696-
$code .= generateCodeWithConditions($fileInfo, "", function(FuncInfo $funcInfo) {
697-
if ($funcInfo->className) {
698-
return null;
699-
}
700-
724+
$code .= generateCodeWithConditions($fileInfo->funcInfos, "", function(FuncInfo $funcInfo) {
701725
if ($funcInfo->alias) {
702726
return sprintf(
703727
"\tZEND_FALIAS(%s, %s, %s)\n",

ext/fileinfo/fileinfo_arginfo.h

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,10 @@
11
/* This is a generated file, edit the .stub.php file instead. */
22

3-
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_finfo___construct, 0, 0, 0)
3+
ZEND_BEGIN_ARG_INFO_EX(arginfo_finfo_open, 0, 0, 0)
44
ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0)
55
ZEND_ARG_TYPE_INFO(0, arg, IS_STRING, 0)
66
ZEND_END_ARG_INFO()
77

8-
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_finfo_file, 0, 0, 1)
9-
ZEND_ARG_TYPE_INFO(0, file_name, IS_STRING, 0)
10-
ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0)
11-
ZEND_ARG_INFO(0, context)
12-
ZEND_END_ARG_INFO()
13-
14-
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_finfo_buffer, 0, 0, 1)
15-
ZEND_ARG_TYPE_INFO(0, string, IS_STRING, 0)
16-
ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0)
17-
ZEND_ARG_INFO(0, context)
18-
ZEND_END_ARG_INFO()
19-
20-
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_finfo_set_flags, 0, 0, 1)
21-
ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0)
22-
ZEND_END_ARG_INFO()
23-
24-
#define arginfo_finfo_open arginfo_class_finfo___construct
25-
268
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_finfo_close, 0, 1, _IS_BOOL, 0)
279
ZEND_ARG_INFO(0, finfo)
2810
ZEND_END_ARG_INFO()
@@ -49,3 +31,21 @@ ZEND_END_ARG_INFO()
4931
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mime_content_type, 0, 1, MAY_BE_STRING|MAY_BE_FALSE)
5032
ZEND_ARG_INFO(0, filename)
5133
ZEND_END_ARG_INFO()
34+
35+
#define arginfo_class_finfo___construct arginfo_finfo_open
36+
37+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_finfo_file, 0, 0, 1)
38+
ZEND_ARG_TYPE_INFO(0, file_name, IS_STRING, 0)
39+
ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0)
40+
ZEND_ARG_INFO(0, context)
41+
ZEND_END_ARG_INFO()
42+
43+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_finfo_buffer, 0, 0, 1)
44+
ZEND_ARG_TYPE_INFO(0, string, IS_STRING, 0)
45+
ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0)
46+
ZEND_ARG_INFO(0, context)
47+
ZEND_END_ARG_INFO()
48+
49+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_finfo_set_flags, 0, 0, 1)
50+
ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0)
51+
ZEND_END_ARG_INFO()

0 commit comments

Comments
 (0)