Skip to content

Commit 2750276

Browse files
committed
Fix child names for virtual nodes
1 parent 7a3886c commit 2750276

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

ast.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ static inline zend_ast **ast_get_children(zend_ast *ast, uint32_t *count) {
166166
}
167167

168168
static void ast_create_virtual_node(
169-
zval *zv, zend_ast_kind kind, zend_ast *ast, zend_string *child_name) {
169+
zval *zv, zend_ast_kind kind, zend_ast *ast, zend_long version) {
170170
zval tmp_zv, tmp_zv2;
171171

172172
object_init_ex(zv, ast_node_ce);
@@ -184,8 +184,8 @@ static void ast_create_virtual_node(
184184
ast_update_property(zv, AST_STR(children), &tmp_zv, AST_CACHE_SLOT_CHILDREN);
185185

186186
ZVAL_COPY(&tmp_zv2, zend_ast_get_zval(ast));
187-
if (child_name) {
188-
zend_hash_add_new(Z_ARRVAL(tmp_zv), child_name, &tmp_zv2);
187+
if (version >= 30) {
188+
zend_hash_add_new(Z_ARRVAL(tmp_zv), ast_kind_child_name(kind, 0), &tmp_zv2);
189189
} else {
190190
zend_hash_next_index_insert(Z_ARRVAL(tmp_zv), &tmp_zv2);
191191
}
@@ -304,9 +304,9 @@ static void ast_to_zval(zval *zv, zend_ast *ast, zend_long version) {
304304
zval child_zv;
305305

306306
if (ast_is_name(child, ast, i)) {
307-
ast_create_virtual_node(&child_zv, AST_NAME, child, child_name);
307+
ast_create_virtual_node(&child_zv, AST_NAME, child, version);
308308
} else if (ast->kind == ZEND_AST_CLOSURE_USES) {
309-
ast_create_virtual_node(&child_zv, AST_CLOSURE_VAR, child, child_name);
309+
ast_create_virtual_node(&child_zv, AST_CLOSURE_VAR, child, version);
310310
} else {
311311
ast_to_zval(&child_zv, child, version);
312312
}

tests/named_children.phpt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
Named child nodes
3+
--FILE--
4+
<?php
5+
6+
require __DIR__ . '/../util.php';
7+
8+
$code = <<<'PHP'
9+
<?php
10+
11+
$fn = function() use(&$var) {
12+
$var += func();
13+
};
14+
PHP;
15+
16+
echo ast_dump(ast\parse_code($code, $version=30));
17+
18+
?>
19+
--EXPECT--
20+
AST_STMT_LIST
21+
0: AST_ASSIGN
22+
var: AST_VAR
23+
name: "fn"
24+
expr: AST_CLOSURE
25+
flags: 0
26+
name: {closure}
27+
params: AST_PARAM_LIST
28+
uses: AST_CLOSURE_USES
29+
0: AST_CLOSURE_VAR
30+
flags: 1
31+
name: "var"
32+
stmts: AST_STMT_LIST
33+
0: AST_ASSIGN_OP
34+
flags: BINARY_ADD (1)
35+
var: AST_VAR
36+
name: "var"
37+
expr: AST_CALL
38+
expr: AST_NAME
39+
flags: NAME_NOT_FQ (1)
40+
name: "func"
41+
args: AST_ARG_LIST
42+
returnType: null

0 commit comments

Comments
 (0)