Skip to content

Commit ed51bba

Browse files
committed
Add support for "iterable" type
1 parent fc5048f commit ed51bba

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ ast\flags\TYPE_BOOL // since version 40
224224
ast\flags\TYPE_LONG // since version 40
225225
ast\flags\TYPE_DOUBLE // since version 40
226226
ast\flags\TYPE_STRING // since version 40
227+
ast\flags\TYPE_ITERABLE // since version 40
227228
228229
// Used by ast\AST_CAST (exclusive)
229230
ast\flags\TYPE_NULL
@@ -428,8 +429,8 @@ Version changelog
428429
call would return it as `'bar'`. Now always the latter form is used.
429430
* `null` elements are now stripped from `AST_STMT_LIST`. Previously these could be caused by nop
430431
statements (`;`).
431-
* Type hints `int`, `float`, `string`, `bool` and `void` will now be represented as `AST_TYPE`
432-
nodes with a respective flag.
432+
* Type hints `int`, `float`, `string`, `bool`, `void` and `iterable` will now be represented as
433+
`AST_TYPE` nodes with a respective flag.
433434
* Many `stmts` children could previously hold one of `null`, a single node or an `AST_STMT_LIST`.
434435
These will now be normalized to always use an `AST_STMT_LIST`. A `null` is only allowed if it is
435436
semantically meaningful, e.g. in the case of `declare(ticks=1);` vs `declare(ticks=1) {}`.

ast.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@
4343
#define AST_MINUS 262
4444

4545
/* Define "void" for PHP 7.0 */
46-
#ifndef IS_VOID
47-
#define IS_VOID 18
46+
#if PHP_VERSION_ID < 70100
47+
# define IS_VOID 18
48+
# define IS_ITERABLE 19
4849
#endif
4950

5051
static inline void ast_update_property(zval *object, zend_string *name, zval *value, void **cache_slot) {
@@ -194,6 +195,7 @@ static const builtin_type_info builtin_types[] = {
194195
{ZEND_STRL("string"), IS_STRING},
195196
{ZEND_STRL("bool"), _IS_BOOL},
196197
{ZEND_STRL("void"), IS_VOID},
198+
{ZEND_STRL("iterable"), IS_ITERABLE},
197199
{NULL, 0, IS_UNDEF}
198200
};
199201
static inline zend_uchar lookup_builtin_type(const zend_string *name) {
@@ -725,6 +727,7 @@ PHP_MINIT_FUNCTION(ast) {
725727
ast_register_flag_constant("TYPE_OBJECT", IS_OBJECT);
726728
ast_register_flag_constant("TYPE_CALLABLE", IS_CALLABLE);
727729
ast_register_flag_constant("TYPE_VOID", IS_VOID);
730+
ast_register_flag_constant("TYPE_ITERABLE", IS_ITERABLE);
728731

729732
ast_register_flag_constant("UNARY_BOOL_NOT", ZEND_BOOL_NOT);
730733
ast_register_flag_constant("UNARY_BITWISE_NOT", ZEND_BW_NOT);

tests/type_hints.phpt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require __DIR__ . '/../util.php';
88
$code = <<<'PHP'
99
<?php
1010
function test(
11-
A $a, array $b, callable $c, INT $d, Float $e, string $f, bool $g
11+
A $a, array $b, callable $c, INT $d, Float $e, string $f, bool $g, iterable $h
1212
) : void {
1313
}
1414
PHP;
@@ -70,6 +70,13 @@ AST_STMT_LIST
7070
name: "bool"
7171
name: "g"
7272
default: null
73+
7: AST_PARAM
74+
flags: 0
75+
type: AST_NAME
76+
flags: NAME_NOT_FQ (1)
77+
name: "iterable"
78+
name: "h"
79+
default: null
7380
uses: null
7481
stmts: AST_STMT_LIST
7582
returnType: AST_NAME
@@ -123,6 +130,12 @@ AST_STMT_LIST
123130
flags: TYPE_BOOL (13)
124131
name: "g"
125132
default: null
133+
7: AST_PARAM
134+
flags: 0
135+
type: AST_TYPE
136+
flags: TYPE_ITERABLE (19)
137+
name: "h"
138+
default: null
126139
uses: null
127140
stmts: AST_STMT_LIST
128141
returnType: AST_TYPE

util.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function get_flag_info() : array {
2929
flags\TYPE_OBJECT => 'TYPE_OBJECT',
3030
flags\TYPE_CALLABLE => 'TYPE_CALLABLE',
3131
flags\TYPE_VOID => 'TYPE_VOID',
32+
flags\TYPE_ITERABLE => 'TYPE_ITERABLE',
3233
];
3334
$useTypes = [
3435
flags\USE_NORMAL => 'USE_NORMAL',

0 commit comments

Comments
 (0)