Skip to content

Commit 6ba2117

Browse files
committed
Represent ?? as BINARY_OP
1 parent d3e5d66 commit 6ba2117

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ ast\flags\BINARY_IS_SMALLER_OR_EQUAL
263263
ast\flags\BINARY_IS_GREATER // since version 20
264264
ast\flags\BINARY_IS_GREATER_OR_EQUAL // since version 20
265265
ast\flags\BINARY_SPACESHIP
266+
ast\flags\BINARY_COALESCE // since version 40
266267
267268
// Used by ast\AST_ASSIGN_OP in versions before 20 (exclusive)
268269
ast\flags\ASSIGN_BITWISE_OR
@@ -308,7 +309,7 @@ This section lists the AST node kinds that are supported and the names of their
308309
version >= 30).
309310

310311
```
311-
AST_AND: left, right // prior to version 10
312+
AST_AND: left, right // prior to version 20
312313
AST_ARRAY_ELEM: value, key
313314
AST_ASSIGN: var, expr
314315
AST_ASSIGN_OP: var, expr
@@ -323,7 +324,7 @@ AST_CLASS_CONST: class, const
323324
AST_CLONE: expr
324325
AST_CLOSURE: params, uses, stmts, returnType
325326
AST_CLOSURE_VAR: name
326-
AST_COALESCE: left, right
327+
AST_COALESCE: left, right // prior to version 40
327328
AST_CONDITIONAL: cond, true, false
328329
AST_CONST: name
329330
AST_CONST_ELEM: name, value
@@ -413,6 +414,10 @@ ZEND_AST_USE
413414
Version changelog
414415
-----------------
415416

417+
### 40 (in development)
418+
419+
* `AST_COALESCE` is now represented as an `AST_BINARY_OP` with flag `BINARY_COALESCE`.
420+
416421
### 30 (current)
417422

418423
Supported since 2015-03-10.

ast.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#define AST_BINARY_IS_GREATER_OR_EQUAL 257
3636
#define AST_BINARY_BOOL_OR 258
3737
#define AST_BINARY_BOOL_AND 259
38+
#define AST_BINARY_COALESCE 260
3839

3940
/* Flags for UNARY_OP to use instead of AST_SILENCE, AST_UNARY_PLUS, AST_UNARY_MINUS */
4041
#define AST_SILENCE 260
@@ -274,6 +275,12 @@ static void ast_to_zval(zval *zv, zend_ast *ast, zend_long version) {
274275
ast->kind = ZEND_AST_BINARY_OP;
275276
ast->attr = AST_BINARY_BOOL_AND;
276277
break;
278+
case ZEND_AST_COALESCE:
279+
if (version >= 40) {
280+
ast->kind = ZEND_AST_BINARY_OP;
281+
ast->attr = AST_BINARY_COALESCE;
282+
}
283+
break;
277284
case ZEND_AST_SILENCE:
278285
ast->kind = ZEND_AST_UNARY_OP;
279286
ast->attr = AST_SILENCE;
@@ -362,7 +369,7 @@ static void ast_to_zval(zval *zv, zend_ast *ast, zend_long version) {
362369
ast_fill_children_ht(Z_ARRVAL(tmp_zv), ast, version);
363370
}
364371

365-
static const zend_long versions[] = {10, 15, 20, 30};
372+
static const zend_long versions[] = {10, 15, 20, 30, 40};
366373
static const size_t versions_count = sizeof(versions)/sizeof(versions[0]);
367374

368375
static zend_string *ast_version_info() {
@@ -621,6 +628,7 @@ PHP_MINIT_FUNCTION(ast) {
621628
ast_register_flag_constant("BINARY_IS_GREATER", AST_BINARY_IS_GREATER);
622629
ast_register_flag_constant("BINARY_IS_GREATER_OR_EQUAL", AST_BINARY_IS_GREATER_OR_EQUAL);
623630
ast_register_flag_constant("BINARY_SPACESHIP", ZEND_SPACESHIP);
631+
ast_register_flag_constant("BINARY_COALESCE", AST_BINARY_COALESCE);
624632

625633
ast_register_flag_constant("ASSIGN_BITWISE_OR", ZEND_ASSIGN_BW_OR);
626634
ast_register_flag_constant("ASSIGN_BITWISE_AND", ZEND_ASSIGN_BW_AND);

tests/coalesce.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Null-coalesce operator
3+
--FILE--
4+
<?php
5+
6+
require __DIR__ . '/../util.php';
7+
8+
$code = <<<'PHP'
9+
<?php
10+
$a ?? $b;
11+
PHP;
12+
13+
echo ast_dump(ast\parse_code($code, $version=30)), "\n";
14+
echo ast_dump(ast\parse_code($code, $version=40)), "\n";
15+
16+
?>
17+
--EXPECT--
18+
AST_STMT_LIST
19+
0: AST_COALESCE
20+
left: AST_VAR
21+
name: "a"
22+
right: AST_VAR
23+
name: "b"
24+
AST_STMT_LIST
25+
0: AST_BINARY_OP
26+
flags: BINARY_COALESCE (260)
27+
left: AST_VAR
28+
name: "a"
29+
right: AST_VAR
30+
name: "b"

util.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ function get_flag_info() : array {
8888
flags\BINARY_IS_GREATER => 'BINARY_IS_GREATER',
8989
flags\BINARY_IS_GREATER_OR_EQUAL => 'BINARY_IS_GREATER_OR_EQUAL',
9090
flags\BINARY_SPACESHIP => 'BINARY_SPACESHIP',
91+
flags\BINARY_COALESCE => 'BINARY_COALESCE',
9192
],
9293
ast\AST_ASSIGN_OP => $sharedBinaryOps + [
9394
// Old version 10 flags

0 commit comments

Comments
 (0)