Skip to content

Commit 9606c0c

Browse files
committed
gen_stub: Add support for generation C #include statements
This is useful to include the necessary files for a constant’s `@cvalue` from within the arginfo itself.
1 parent 9625eb1 commit 9606c0c

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

build/gen_stub.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,19 @@ public function generateVersionDependentFlagCode(
12991299
}
13001300
}
13011301

1302+
class IncludeInfo {
1303+
public /* readonly */ ?string $cond;
1304+
public /* readonly */ string $include;
1305+
1306+
public function __construct(
1307+
string $include,
1308+
?string $cond,
1309+
) {
1310+
$this->include = $include;
1311+
$this->cond = $cond;
1312+
}
1313+
}
1314+
13021315
class FuncInfo {
13031316
public /* readonly */ FunctionOrMethodName $name;
13041317
private /* readonly */ int $classFlags;
@@ -4189,6 +4202,8 @@ class FileInfo {
41894202
public array $funcInfos = [];
41904203
/** @var ClassInfo[] */
41914204
public array $classInfos = [];
4205+
/** @var IncludeInfo[] */
4206+
public array $includeInfos = [];
41924207
public bool $generateFunctionEntries = false;
41934208
public string $declarationPrefix = "";
41944209
public bool $generateClassEntries = false;
@@ -4337,6 +4352,16 @@ private function handleStatements(array $stmts, PrettyPrinterAbstract $prettyPri
43374352
$conds = [];
43384353
foreach ($stmts as $stmt) {
43394354
$cond = self::handlePreprocessorConditions($conds, $stmt);
4355+
4356+
if ($stmt instanceof Stmt\Declare_) {
4357+
foreach ($stmt->declares as $declare) {
4358+
if ($declare->key->name !== 'c_include') {
4359+
throw new Exception("Unexpected declare {$declare->key->name}");
4360+
}
4361+
$this->includeInfos[] = new IncludeInfo((string)EvaluatedValue::createFromExpression($declare->value, null, null, [])->value, $cond);
4362+
}
4363+
continue;
4364+
}
43404365

43414366
if ($stmt instanceof Stmt\Nop) {
43424367
continue;
@@ -5162,6 +5187,17 @@ function generateArgInfoCode(
51625187

51635188
$generatedFuncInfos = [];
51645189

5190+
$argInfoCode = generateCodeWithConditions(
5191+
$fileInfo->includeInfos, "\n",
5192+
static function (IncludeInfo $includeInfo) {
5193+
return sprintf("#include %s\n", $includeInfo->include);
5194+
}
5195+
);
5196+
5197+
if ($argInfoCode !== "") {
5198+
$code .= "$argInfoCode\n";
5199+
}
5200+
51655201
$argInfoCode = generateCodeWithConditions(
51665202
$fileInfo->getAllFuncInfos(), "\n",
51675203
static function (FuncInfo $funcInfo) use (&$generatedFuncInfos, $fileInfo) {

ext/zend_test/test.stub.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
* @generate-legacy-arginfo 80000
66
* @undocumentable
77
*/
8+
9+
#if 0
10+
declare(
11+
c_include='<stdint.h>',
12+
c_include='"zend_attributes.h"'
13+
);
14+
#endif
15+
816
namespace {
917
require "Zend/zend_attributes.stub.php";
1018

ext/zend_test/test_arginfo.h

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

0 commit comments

Comments
 (0)