-
Notifications
You must be signed in to change notification settings - Fork 78
/
ast.c
1618 lines (1419 loc) · 47 KB
/
ast.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_ast.h"
#include "ext/spl/spl_exceptions.h"
#include "zend_language_scanner.h"
#include "zend_language_scanner_defs.h"
#include "zend_language_parser.h"
#include "zend_exceptions.h"
#include "zend_hash.h"
#include "zend_smart_str.h"
#if PHP_VERSION_ID >= 80200
/* Used for AllowDynamicProperties */
#include "zend_attributes.h"
#endif
#if PHP_VERSION_ID < 70200
#error "The php-ast 1.1 release dropped support for php 7.0-7.1. Use php-ast 1.0.16 instead."
#endif
#ifndef ZEND_THIS
#define ZEND_THIS getThis()
#endif
#ifndef ZEND_ARG_INFO_WITH_DEFAULT_VALUE
#define ZEND_ARG_INFO_WITH_DEFAULT_VALUE(pass_by_ref, name, default_value) \
ZEND_ARG_INFO(pass_by_ref, name)
#endif
#ifndef ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX
#define ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(name, return_reference, required_num_args, class_name, allow_null) \
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, class_name, allow_null)
#endif
#ifndef ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE
#define ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(pass_by_ref, name, type_hint, allow_null, default_value) \
ZEND_ARG_TYPE_INFO(pass_by_ref, name, type_hint, allow_null)
#endif
#include "ast_arginfo.h"
#define ast_throw_exception(exception_ce, ...) \
zend_throw_exception_ex(exception_ce, 0, __VA_ARGS__)
#define ast_declare_property(ce, name, value) \
zend_declare_property_ex((ce), (name), (value), ZEND_ACC_PUBLIC, NULL)
#define ast_register_flag_constant(name, value) \
REGISTER_NS_LONG_CONSTANT("ast\\flags", name, value, CONST_CS | CONST_PERSISTENT)
// These are in the order the properties were declared with ast_declare_property.
#define AST_NODE_PROP_KIND(object) OBJ_PROP_NUM((object), 0)
#define AST_NODE_PROP_FLAGS(object) OBJ_PROP_NUM((object), 1)
#define AST_NODE_PROP_LINENO(object) OBJ_PROP_NUM((object), 2)
#define AST_NODE_PROP_CHILDREN(object) OBJ_PROP_NUM((object), 3)
#define AST_NODE_SET_PROP_KIND(object, num) ZVAL_LONG(AST_NODE_PROP_KIND((object)), (num))
#define AST_NODE_SET_PROP_FLAGS(object, num) ZVAL_LONG(AST_NODE_PROP_FLAGS((object)), (num))
#define AST_NODE_SET_PROP_LINENO(object, num) ZVAL_LONG(AST_NODE_PROP_LINENO((object)), (num))
// Set the ast\Node->children array to the given reference-counted array without incrementing the reference count of the array.
// Do not use this macro with immutable arrays.
#define AST_NODE_SET_PROP_CHILDREN_ARRAY(object, array) ZVAL_ARR(AST_NODE_PROP_CHILDREN((object)), (array))
#define AST_METADATA_PROP_KIND(object) OBJ_PROP_NUM((object), 0)
#define AST_METADATA_PROP_NAME(object) OBJ_PROP_NUM((object), 1)
#define AST_METADATA_PROP_FLAGS(object) OBJ_PROP_NUM((object), 2)
#define AST_METADATA_PROP_FLAGS_COMBINABLE(object) OBJ_PROP_NUM((object), 3)
#define AST_CURRENT_VERSION 110
/* Additional flags for BINARY_OP */
#define AST_BINARY_IS_GREATER 256
#define AST_BINARY_IS_GREATER_OR_EQUAL 257
#define AST_BINARY_BOOL_OR 258
#define AST_BINARY_BOOL_AND 259
#define AST_BINARY_COALESCE 260
/* Flags for UNARY_OP to use instead of AST_SILENCE, AST_UNARY_PLUS, AST_UNARY_MINUS */
#define AST_SILENCE 260
#define AST_PLUS 261
#define AST_MINUS 262
#if PHP_VERSION_ID < 70300
# define ZEND_BIND_REF 1
#endif
#if PHP_VERSION_ID < 70400
# define ZEND_DIM_ALTERNATIVE_SYNTAX (1<<1)
# define ZEND_PARENTHESIZED_CONDITIONAL 1
#endif
/* Make IS_STATIC follow IS_ITERABLE in php 7.0 */
#if PHP_VERSION_ID < 80000
# define IS_STATIC 20
# define IS_MIXED 21
/* In PHP 7.0-7.4, PARAM_REF and PARAM_VARIADIC were 1 and 2. */
# define PARAM_MODIFIER_PUBLIC (1 << 2)
# define PARAM_MODIFIER_PROTECTED (1 << 3)
# define PARAM_MODIFIER_PRIVATE (1 << 4)
#else
# define PARAM_MODIFIER_PUBLIC ZEND_ACC_PUBLIC
# define PARAM_MODIFIER_PROTECTED ZEND_ACC_PROTECTED
# define PARAM_MODIFIER_PRIVATE ZEND_ACC_PRIVATE
#endif
#if PHP_VERSION_ID < 80100
# define IS_NEVER 22
#endif
#if PHP_VERSION_ID < 80200
# define ZEND_ENCAPS_VAR_DOLLAR_CURLY (1<<0)
# define ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR (1<<1)
#endif
#if PHP_VERSION_ID >= 80400
# define ZEND_DIM_ALTERNATIVE_SYNTAX (1<<1)
#endif
/* This contains state of the ast Node creator. */
typedef struct ast_state_info {
zend_long version;
zend_long declIdCounter;
} ast_state_info_t;
typedef struct _ast_flag_info {
uint16_t ast_kind;
zend_bool combinable;
const char **flags;
} ast_flag_info;
ZEND_DECLARE_MODULE_GLOBALS(ast)
ast_str_globals str_globals;
static zend_class_entry *ast_node_ce;
static zend_class_entry *ast_metadata_ce;
#define AST_FLAG(name) "ast\\flags\\" #name
static const char *name_flags[] = {
AST_FLAG(NAME_FQ),
AST_FLAG(NAME_NOT_FQ),
AST_FLAG(NAME_RELATIVE),
NULL
};
static const char *class_flags[] = {
AST_FLAG(CLASS_ABSTRACT),
AST_FLAG(CLASS_FINAL),
AST_FLAG(CLASS_TRAIT),
AST_FLAG(CLASS_INTERFACE),
AST_FLAG(CLASS_ANONYMOUS),
AST_FLAG(CLASS_ENUM),
AST_FLAG(CLASS_READONLY),
NULL
};
static const char *param_flags[] = {
AST_FLAG(PARAM_REF),
AST_FLAG(PARAM_VARIADIC),
AST_FLAG(PARAM_MODIFIER_PUBLIC),
AST_FLAG(PARAM_MODIFIER_PROTECTED),
AST_FLAG(PARAM_MODIFIER_PRIVATE),
NULL
};
static const char *type_flags[] = {
AST_FLAG(TYPE_NULL),
AST_FLAG(TYPE_FALSE),
AST_FLAG(TYPE_TRUE),
AST_FLAG(TYPE_BOOL),
AST_FLAG(TYPE_LONG),
AST_FLAG(TYPE_DOUBLE),
AST_FLAG(TYPE_STRING),
AST_FLAG(TYPE_ARRAY),
AST_FLAG(TYPE_OBJECT),
AST_FLAG(TYPE_CALLABLE),
AST_FLAG(TYPE_VOID),
AST_FLAG(TYPE_ITERABLE),
AST_FLAG(TYPE_STATIC),
AST_FLAG(TYPE_MIXED),
AST_FLAG(TYPE_NEVER),
NULL
};
static const char *unary_op_flags[] = {
AST_FLAG(UNARY_BOOL_NOT),
AST_FLAG(UNARY_BITWISE_NOT),
AST_FLAG(UNARY_MINUS),
AST_FLAG(UNARY_PLUS),
AST_FLAG(UNARY_SILENCE),
NULL
};
#define AST_SHARED_BINARY_OP_FLAGS \
AST_FLAG(BINARY_BITWISE_OR), \
AST_FLAG(BINARY_BITWISE_AND), \
AST_FLAG(BINARY_BITWISE_XOR), \
AST_FLAG(BINARY_CONCAT), \
AST_FLAG(BINARY_ADD), \
AST_FLAG(BINARY_SUB), \
AST_FLAG(BINARY_MUL), \
AST_FLAG(BINARY_DIV), \
AST_FLAG(BINARY_MOD), \
AST_FLAG(BINARY_POW), \
AST_FLAG(BINARY_SHIFT_LEFT), \
AST_FLAG(BINARY_SHIFT_RIGHT), \
AST_FLAG(BINARY_COALESCE) \
static const char *binary_op_flags[] = {
AST_SHARED_BINARY_OP_FLAGS,
AST_FLAG(BINARY_BOOL_AND),
AST_FLAG(BINARY_BOOL_OR),
AST_FLAG(BINARY_BOOL_XOR),
AST_FLAG(BINARY_IS_IDENTICAL),
AST_FLAG(BINARY_IS_NOT_IDENTICAL),
AST_FLAG(BINARY_IS_EQUAL),
AST_FLAG(BINARY_IS_NOT_EQUAL),
AST_FLAG(BINARY_IS_SMALLER),
AST_FLAG(BINARY_IS_SMALLER_OR_EQUAL),
AST_FLAG(BINARY_IS_GREATER),
AST_FLAG(BINARY_IS_GREATER_OR_EQUAL),
AST_FLAG(BINARY_SPACESHIP),
NULL
};
static const char *assign_op_flags[] = {
AST_SHARED_BINARY_OP_FLAGS,
NULL
};
static const char *magic_const_flags[] = {
AST_FLAG(MAGIC_LINE),
AST_FLAG(MAGIC_FILE),
AST_FLAG(MAGIC_DIR),
AST_FLAG(MAGIC_NAMESPACE),
AST_FLAG(MAGIC_FUNCTION),
AST_FLAG(MAGIC_METHOD),
AST_FLAG(MAGIC_CLASS),
AST_FLAG(MAGIC_TRAIT),
NULL
};
static const char *use_flags[] = {
AST_FLAG(USE_NORMAL),
AST_FLAG(USE_FUNCTION),
AST_FLAG(USE_CONST),
NULL
};
static const char *include_flags[] = {
AST_FLAG(EXEC_EVAL),
AST_FLAG(EXEC_INCLUDE),
AST_FLAG(EXEC_INCLUDE_ONCE),
AST_FLAG(EXEC_REQUIRE),
AST_FLAG(EXEC_REQUIRE_ONCE),
NULL
};
static const char *array_flags[] = {
AST_FLAG(ARRAY_SYNTAX_LIST),
AST_FLAG(ARRAY_SYNTAX_LONG),
AST_FLAG(ARRAY_SYNTAX_SHORT),
NULL
};
static const char *array_elem_flags[] = {
AST_FLAG(ARRAY_ELEM_REF),
NULL
};
static const char *closure_use_flags[] = {
AST_FLAG(CLOSURE_USE_REF),
NULL
};
#define AST_MODIFIER_FLAGS \
AST_FLAG(MODIFIER_PUBLIC), \
AST_FLAG(MODIFIER_PROTECTED), \
AST_FLAG(MODIFIER_PRIVATE), \
AST_FLAG(MODIFIER_STATIC), \
AST_FLAG(MODIFIER_ABSTRACT), \
AST_FLAG(MODIFIER_FINAL), \
AST_FLAG(MODIFIER_READONLY)
static const char *modifier_flags[] = {
AST_MODIFIER_FLAGS,
NULL
};
static const char *func_flags[] = {
AST_MODIFIER_FLAGS,
AST_FLAG(FUNC_RETURNS_REF),
AST_FLAG(FUNC_GENERATOR),
NULL
};
static const char *dim_flags[] = {
AST_FLAG(DIM_ALTERNATIVE_SYNTAX),
AST_FLAG(ENCAPS_VAR_DOLLAR_CURLY),
NULL
};
// Flags of AST_CONDITIONAL are marked as combinable in case any other flags get added in the future.
static const char *conditional_flags[] = {
AST_FLAG(PARENTHESIZED_CONDITIONAL),
NULL
};
static const char *var_flags[] = {
AST_FLAG(ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR),
AST_FLAG(ENCAPS_VAR_DOLLAR_CURLY),
NULL
};
static const ast_flag_info flag_info[] = {
{ AST_NAME, 0, name_flags },
{ ZEND_AST_CLASS, 1, class_flags },
{ ZEND_AST_PARAM, 1, param_flags },
{ ZEND_AST_TYPE, 0, type_flags },
{ ZEND_AST_CAST, 0, type_flags },
{ ZEND_AST_UNARY_OP, 0, unary_op_flags },
{ ZEND_AST_BINARY_OP, 0, binary_op_flags },
{ ZEND_AST_ASSIGN_OP, 0, assign_op_flags },
{ ZEND_AST_MAGIC_CONST, 0, magic_const_flags },
{ ZEND_AST_USE, 0, use_flags },
{ ZEND_AST_GROUP_USE, 0, use_flags },
{ ZEND_AST_USE_ELEM, 0, use_flags },
{ ZEND_AST_INCLUDE_OR_EVAL, 0, include_flags },
{ ZEND_AST_ARRAY, 0, array_flags },
{ ZEND_AST_ARRAY_ELEM, 0, array_elem_flags },
{ AST_CLOSURE_VAR, 0, closure_use_flags },
{ ZEND_AST_METHOD, 1, func_flags },
{ ZEND_AST_FUNC_DECL, 1, func_flags },
{ ZEND_AST_CLOSURE, 1, func_flags },
{ ZEND_AST_ARROW_FUNC, 1, func_flags },
{ ZEND_AST_PROPERTY_HOOK, 1, func_flags },
{ ZEND_AST_PROP_DECL, 1, modifier_flags },
{ ZEND_AST_PROP_GROUP, 1, modifier_flags },
{ ZEND_AST_CLASS_CONST_DECL, 1, modifier_flags },
{ ZEND_AST_CLASS_CONST_GROUP, 1, modifier_flags },
{ ZEND_AST_TRAIT_ALIAS, 1, modifier_flags },
{ ZEND_AST_DIM, 1, dim_flags },
{ ZEND_AST_CONDITIONAL, 1, conditional_flags },
{ ZEND_AST_VAR, 1, var_flags },
};
static inline void ast_update_property(zval *object, zend_string *name, zval *value) {
#if PHP_VERSION_ID < 80000
zval name_zv;
ZVAL_STR(&name_zv, name);
Z_OBJ_HT_P(object)->write_property(object, &name_zv, value, NULL);
#else
Z_OBJ_HT_P(object)->write_property(Z_OBJ_P(object), name, value, NULL);
#endif
}
static inline void ast_update_property_long(zval *object, zend_string *name, zend_long value_raw) {
zval value_zv;
ZVAL_LONG(&value_zv, value_raw);
ast_update_property(object, name, &value_zv);
}
static zend_ast *get_ast(zend_string *code, zend_arena **ast_arena, zend_string *filename) {
#if PHP_VERSION_ID >= 80100
if (filename) {
return zend_compile_string_to_ast(code, ast_arena, filename);
} else {
zend_ast *ast;
filename = zend_string_init("string code", sizeof("string code") - 1, 0);
ast = zend_compile_string_to_ast(code, ast_arena, filename);
zend_string_release_ex(filename, 0);
return ast;
}
#else
zval code_zv;
zend_bool original_in_compilation;
zend_lex_state original_lex_state;
zend_ast *ast;
ZVAL_STR_COPY(&code_zv, code);
original_in_compilation = CG(in_compilation);
CG(in_compilation) = 1;
zend_save_lexical_state(&original_lex_state);
zend_prepare_string_for_scanning(&code_zv, filename ? filename->val : "string code");
CG(ast) = NULL;
CG(ast_arena) = zend_arena_create(1024 * 32);
LANG_SCNG(yy_state) = yycINITIAL;
if (zendparse() != 0) {
zend_ast_destroy(CG(ast));
zend_arena_destroy(CG(ast_arena));
CG(ast) = NULL;
}
/* restore_lexical_state changes CG(ast) and CG(ast_arena) */
ast = CG(ast);
*ast_arena = CG(ast_arena);
zend_restore_lexical_state(&original_lex_state);
CG(in_compilation) = original_in_compilation;
zval_dtor(&code_zv);
return ast;
#endif
}
/* Returns whether node->attr (i.e. flags) is used by this node kind. Not to be confused with php 8.0's attributes. */
static inline zend_bool ast_kind_uses_attr(zend_ast_kind kind) {
return kind == ZEND_AST_PARAM || kind == ZEND_AST_TYPE || kind == ZEND_AST_TRAIT_ALIAS
|| kind == ZEND_AST_UNARY_OP || kind == ZEND_AST_BINARY_OP || kind == ZEND_AST_ASSIGN_OP
|| kind == ZEND_AST_CAST || kind == ZEND_AST_MAGIC_CONST || kind == ZEND_AST_ARRAY_ELEM
|| kind == ZEND_AST_INCLUDE_OR_EVAL || kind == ZEND_AST_USE || kind == ZEND_AST_PROP_DECL
|| kind == ZEND_AST_PROP_GROUP
|| kind == ZEND_AST_GROUP_USE || kind == ZEND_AST_USE_ELEM
|| kind == AST_NAME || kind == AST_CLOSURE_VAR || kind == ZEND_AST_CLASS_CONST_DECL
|| kind == ZEND_AST_CLASS_CONST_GROUP
|| kind == ZEND_AST_ARRAY || kind == ZEND_AST_DIM || kind == ZEND_AST_CONDITIONAL
|| kind == ZEND_AST_VAR;
}
/* Returns true if nodes of this kind are represented with the C struct zend_ast_decl. */
static inline zend_bool ast_kind_is_decl(zend_ast_kind kind) {
return kind == ZEND_AST_FUNC_DECL || kind == ZEND_AST_CLOSURE
|| kind == ZEND_AST_ARROW_FUNC
|| kind == ZEND_AST_PROPERTY_HOOK
|| kind == ZEND_AST_METHOD || kind == ZEND_AST_CLASS;
}
static inline zend_bool ast_is_name(zend_ast *ast, zend_ast *parent, uint32_t i) {
if (!ast) {
return 0;
}
if (ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(ast)) != IS_STRING) {
return 0;
}
if (parent->kind == ZEND_AST_NAME_LIST) {
return 1;
}
#if PHP_VERSION_ID >= 80100
if (parent->kind == ZEND_AST_TYPE_INTERSECTION) {
return 1;
}
#endif
#if PHP_VERSION_ID >= 80000
if (parent->kind == ZEND_AST_TYPE_UNION) {
return 1;
}
#endif
if (i == 0) {
return parent->kind == ZEND_AST_CATCH || parent->kind == ZEND_AST_CLASS
|| parent->kind == ZEND_AST_PARAM || parent->kind == ZEND_AST_METHOD_REFERENCE
|| parent->kind == ZEND_AST_CALL || parent->kind == ZEND_AST_CONST
|| parent->kind == ZEND_AST_NEW || parent->kind == ZEND_AST_STATIC_CALL
|| parent->kind == ZEND_AST_CLASS_CONST || parent->kind == ZEND_AST_STATIC_PROP
|| parent->kind == ZEND_AST_PROP_GROUP || parent->kind == ZEND_AST_CLASS_NAME
#if PHP_VERSION_ID >= 80000
|| parent->kind == ZEND_AST_ATTRIBUTE
#endif
;
}
#if PHP_VERSION_ID >= 80300
if (i == 2) {
return parent->kind == ZEND_AST_CLASS_CONST_GROUP;
}
#endif
if (i == 1) {
return parent->kind == ZEND_AST_INSTANCEOF;
}
if (i == 3) {
return parent->kind == ZEND_AST_FUNC_DECL || parent->kind == ZEND_AST_CLOSURE
#if PHP_VERSION_ID >= 70400
|| parent->kind == ZEND_AST_ARROW_FUNC
#endif
|| parent->kind == ZEND_AST_METHOD;
}
#if PHP_VERSION_ID >= 80100
if (i == 4) {
return parent->kind == ZEND_AST_CLASS;
}
#endif
return 0;
}
/* Assumes that ast_is_name is already true */
static inline zend_bool ast_is_type(zend_ast *ast, zend_ast *parent, uint32_t i) {
#if PHP_VERSION_ID >= 80100
if (parent->kind == ZEND_AST_TYPE_INTERSECTION) {
return 1;
}
#endif
#if PHP_VERSION_ID >= 80000
if (parent->kind == ZEND_AST_TYPE_UNION) {
return 1;
}
#endif
if (i == 0) {
return parent->kind == ZEND_AST_PARAM
#if PHP_VERSION_ID >= 70400
|| parent->kind == ZEND_AST_PROP_GROUP
#endif
;
}
#if PHP_VERSION_ID >= 80300
if (i == 2) {
return parent->kind == ZEND_AST_CLASS_CONST_GROUP;
}
#endif
if (i == 3) {
return parent->kind == ZEND_AST_CLOSURE || parent->kind == ZEND_AST_FUNC_DECL
#if PHP_VERSION_ID >= 70400
|| parent->kind == ZEND_AST_ARROW_FUNC
#endif
|| parent->kind == ZEND_AST_METHOD;
}
#if PHP_VERSION_ID >= 80100
if (i == 4) {
return parent->kind == ZEND_AST_CLASS;
}
#endif
return 0;
}
static inline zend_bool ast_is_var_name(zend_ast *ast, zend_ast *parent, uint32_t i) {
return (parent->kind == ZEND_AST_STATIC && i == 0)
|| (parent->kind == ZEND_AST_CATCH && i == 1 && ast != NULL);
}
/* Whether this node may need statement list normalization */
static inline zend_bool ast_should_normalize_list(zend_ast *ast, zend_ast *parent, uint32_t i) {
if (ast && ast->kind == ZEND_AST_STMT_LIST) {
return 0;
}
if (i == 0) {
return parent->kind == ZEND_AST_DO_WHILE;
}
if (i == 1) {
if (parent->kind == ZEND_AST_DECLARE) {
/* declare(); and declare() {} are not the same */
return ast != NULL;
}
return parent->kind == ZEND_AST_IF_ELEM || parent->kind == ZEND_AST_WHILE;
}
if (i == 2) {
return parent->kind == ZEND_AST_CATCH;
}
if (i == 3) {
return parent->kind == ZEND_AST_FOR || parent->kind == ZEND_AST_FOREACH;
}
return 0;
}
/* Adopted from zend_compile.c */
typedef struct _builtin_type_info {
const char* name;
const size_t name_len;
const zend_uchar type;
} builtin_type_info;
static const builtin_type_info builtin_types[] = {
{ZEND_STRL("int"), IS_LONG},
{ZEND_STRL("float"), IS_DOUBLE},
{ZEND_STRL("string"), IS_STRING},
{ZEND_STRL("bool"), _IS_BOOL},
{ZEND_STRL("void"), IS_VOID},
{ZEND_STRL("iterable"), IS_ITERABLE},
{ZEND_STRL("object"), IS_OBJECT},
{ZEND_STRL("null"), IS_NULL}, /* Null and false for php 8.0 union types */
{ZEND_STRL("false"), IS_FALSE},
// {ZEND_STRL("static"), IS_STATIC}, /* Impossible to be parsed before php 8 */
{ZEND_STRL("mixed"), IS_MIXED},
{ZEND_STRL("never"), IS_NEVER},
{ZEND_STRL("true"), IS_TRUE}, /* PHP 8.2 added the true type */
{NULL, 0, IS_UNDEF}
};
static inline zend_uchar lookup_builtin_type(const zend_string *name) {
const builtin_type_info *info = &builtin_types[0];
for (; info->name; ++info) {
if (ZSTR_LEN(name) == info->name_len
&& !zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len)
) {
return info->type;
}
}
return 0;
}
#if PHP_VERSION_ID < 70400
static inline zend_ast_attr ast_assign_op_to_binary_op(zend_ast_attr attr) {
switch (attr) {
case ZEND_ASSIGN_BW_OR: return ZEND_BW_OR;
case ZEND_ASSIGN_BW_AND: return ZEND_BW_AND;
case ZEND_ASSIGN_BW_XOR: return ZEND_BW_XOR;
case ZEND_ASSIGN_CONCAT: return ZEND_CONCAT;
case ZEND_ASSIGN_ADD: return ZEND_ADD;
case ZEND_ASSIGN_SUB: return ZEND_SUB;
case ZEND_ASSIGN_MUL: return ZEND_MUL;
case ZEND_ASSIGN_DIV: return ZEND_DIV;
case ZEND_ASSIGN_MOD: return ZEND_MOD;
case ZEND_ASSIGN_POW: return ZEND_POW;
case ZEND_ASSIGN_SL: return ZEND_SL;
case ZEND_ASSIGN_SR: return ZEND_SR;
EMPTY_SWITCH_DEFAULT_CASE()
}
}
#endif
static inline zend_ast **ast_get_children(zend_ast *ast, ast_state_info_t *state, uint32_t *count) {
if (ast_kind_is_decl(ast->kind)) {
zend_ast_decl *decl = (zend_ast_decl *) ast;
#if PHP_VERSION_ID >= 80100
*count = decl->kind == ZEND_AST_CLASS ? (state->version >= 85 ? 5 : 4) : 5;
#elif PHP_VERSION_ID >= 80000
*count = decl->kind == ZEND_AST_CLASS ? 4 : 5;
#else
*count = decl->kind == ZEND_AST_CLASS ? 3 : 4;
#endif
return decl->child;
} else if (zend_ast_is_list(ast)) {
zend_ast_list *list = zend_ast_get_list(ast);
*count = list->children;
return list->child;
} else {
*count = zend_ast_get_num_children(ast);
return ast->child;
}
}
static void ast_to_zval(zval *zv, zend_ast *ast, ast_state_info_t *state);
static void ast_create_virtual_node_ex(
zval *zv, zend_ast_kind kind, zend_ast_attr attr, uint32_t lineno,
ast_state_info_t *state, uint32_t num_children, ...) {
va_list va;
uint32_t i;
object_init_ex(zv, ast_node_ce);
zend_object *obj = Z_OBJ_P(zv);
AST_NODE_SET_PROP_KIND(obj, kind);
AST_NODE_SET_PROP_FLAGS(obj, attr);
AST_NODE_SET_PROP_LINENO(obj, lineno);
array_init_size(AST_NODE_PROP_CHILDREN(obj), num_children);
HashTable *children = Z_ARRVAL_P(AST_NODE_PROP_CHILDREN(obj));
va_start(va, num_children);
for (i = 0; i < num_children; i++) {
zval *child_zv = va_arg(va, zval *);
zend_string *child_name = ast_kind_child_name(kind, i);
if (child_name) {
zend_hash_add_new(children, child_name, child_zv);
} else {
zend_hash_next_index_insert(children, child_zv);
}
}
va_end(va);
}
static void ast_create_virtual_node(
zval *zv, zend_ast_kind kind, zend_ast_attr attr, zend_ast *child, ast_state_info_t *state) {
zval child_zv;
ast_to_zval(&child_zv, child, state);
ast_create_virtual_node_ex(
zv, kind, attr, zend_ast_get_lineno(child), state, 1, &child_zv);
}
static inline void ast_name_to_zval(zend_ast *child, zend_ast *ast, zval *child_zv, int i, ast_state_info_t *state) {
zend_uchar type;
zend_bool is_nullable = 0;
if (child->attr & ZEND_TYPE_NULLABLE) {
is_nullable = 1;
child->attr &= ~ZEND_TYPE_NULLABLE;
}
if (child->attr == ZEND_NAME_FQ) {
/* Ensure there is no leading \ for fully-qualified names. This can happen if
* something like ('\bar')() is used. */
zval *name = zend_ast_get_zval(child);
if (Z_STRVAL_P(name)[0] == '\\') {
zend_string *new_name = zend_string_init(
Z_STRVAL_P(name) + 1, Z_STRLEN_P(name) - 1, 0);
zend_string_release(Z_STR_P(name));
Z_STR_P(name) = new_name;
}
}
if (child->attr == ZEND_NAME_NOT_FQ
&& ast_is_type(child, ast, i)
&& (type = lookup_builtin_type(zend_ast_get_str(child)))
&& (type != IS_MIXED || state->version >= 80)
) {
/* Convert "int" etc typehints to TYPE nodes */
ast_create_virtual_node_ex(
child_zv, ZEND_AST_TYPE, type, zend_ast_get_lineno(child), state, 0);
} else {
ast_create_virtual_node(child_zv, AST_NAME, child->attr, child, state);
}
if (is_nullable) {
/* Create explicit AST_NULLABLE_TYPE node */
zval tmp;
ZVAL_COPY_VALUE(&tmp, child_zv);
ast_create_virtual_node_ex(
child_zv, AST_NULLABLE_TYPE, 0, zend_ast_get_lineno(child), state, 1, &tmp);
}
}
static void ast_fill_children_ht(HashTable *ht, zend_ast *ast, ast_state_info_t *state) {
uint32_t i, count;
zend_bool is_list = zend_ast_is_list(ast);
zend_ast **children = ast_get_children(ast, state, &count);
const zend_ast_kind ast_kind = ast->kind;
for (i = 0; i < count; ++i) {
zend_ast *child = children[i];
zend_string *child_name = !is_list ? ast_kind_child_name(ast_kind, i) : NULL;
zval child_zv;
if (ast_kind == ZEND_AST_STMT_LIST) {
if (child != NULL && child->kind == ZEND_AST_STMT_LIST) {
ast_fill_children_ht(ht, child, state);
continue;
}
if (child == NULL) {
continue;
}
}
#if PHP_VERSION_ID >= 80000
if (state->version < 80) {
switch (ast_kind) {
case ZEND_AST_PARAM:
if (i >= 3) {
/* Skip attributes and doc comment and hooks. */
continue;
}
break;
case ZEND_AST_METHOD:
case ZEND_AST_FUNC_DECL:
case ZEND_AST_CLOSURE:
case ZEND_AST_ARROW_FUNC:
if (i == 4) {
continue;
}
break;
case ZEND_AST_CLASS:
if (i >= 3) {
continue;
}
break;
case ZEND_AST_PROP_GROUP:
if (i == 2) {
continue;
}
break;
}
}
#if PHP_VERSION_ID >= 80100
if (ast_kind == ZEND_AST_CLASS && i == 4) {
if (state->version < 85) {
continue;
}
}
#endif
#if PHP_VERSION_ID >= 80300
if (ast_kind == ZEND_AST_CLASS_CONST_GROUP && i == 2) {
if (state->version < 100) {
continue;
}
}
#endif
#if PHP_VERSION_ID >= 80400
if (ast_kind == ZEND_AST_PROP_ELEM && i == 3) {
if (state->version < 110) {
continue;
}
}
if (ast_kind == ZEND_AST_PROPERTY_HOOK && (i == 1 || i == 3)) {
/* Property hooks don't have uses/returnType but they do have params/stmts/attributes. */
continue;
}
/* Constructor property promotion shorthand can have property hooks. */
if (ast_kind == ZEND_AST_PARAM && i == 5) {
if (state->version < 110) {
continue;
}
}
#endif
#endif
if (ast_is_name(child, ast, i)) {
ast_name_to_zval(child, ast, &child_zv, i, state);
} else if (child && child->kind == ZEND_AST_TYPE && (child->attr & ZEND_TYPE_NULLABLE)) {
child->attr &= ~ZEND_TYPE_NULLABLE;
ast_create_virtual_node(&child_zv, AST_NULLABLE_TYPE, 0, child, state);
} else if (ast_kind == ZEND_AST_CLOSURE_USES) {
ast_create_virtual_node(&child_zv, AST_CLOSURE_VAR, child->attr, child, state);
} else if (ast_is_var_name(child, ast, i)) {
ast_create_virtual_node(&child_zv, ZEND_AST_VAR, 0, child, state);
} else if (ast_should_normalize_list(child, ast, i)) {
if (child) {
zval tmp;
ast_to_zval(&tmp, child, state);
ast_create_virtual_node_ex(
&child_zv, ZEND_AST_STMT_LIST, 0, zend_ast_get_lineno(child), state, 1, &tmp);
} else {
ast_create_virtual_node_ex(
&child_zv, ZEND_AST_STMT_LIST, 0, zend_ast_get_lineno(ast), state, 0);
}
} else if (state->version >= 60 && i == 1
&& (ast_kind == ZEND_AST_FUNC_DECL || ast_kind == ZEND_AST_METHOD)) {
/* Skip "uses" child, it is only relevant for closures */
continue;
#if PHP_VERSION_ID >= 70400
} else if (i == 1 && ast_kind == ZEND_AST_ARROW_FUNC) {
/* Skip "uses" child since it is always empty */
continue;
#endif
} else if (ast_kind == ZEND_AST_LIST && child != NULL) {
/* Emulate simple variable list */
ast_to_zval(&child_zv, child->child[0], state);
} else {
ast_to_zval(&child_zv, child, state);
}
if (child_name) {
#if PHP_VERSION_ID >= 80200
if (ast_kind == ZEND_AST_ARROW_FUNC && i == 2) {
/* Imitate the native arrow function ast structure used in php 7.4-8.1 for stmts */
/* (This is still different from regular functions, which have AST_STMT_LIST) */
/* TODO: In a new node type, remove the ZEND_AST_RETURN node instead. */
zval tmp;
ZVAL_COPY_VALUE(&tmp, &child_zv);
ast_create_virtual_node_ex(
&child_zv, ZEND_AST_RETURN, 0, zend_ast_get_lineno(child), state, 1, &tmp);
}
#endif
zend_hash_add_new(ht, child_name, &child_zv);
} else {
zend_hash_next_index_insert(ht, &child_zv);
}
}
#if PHP_VERSION_ID < 80000
if (state->version >= 80) {
if (ast_kind == ZEND_AST_PARAM) {
zval tmp;
ZVAL_NULL(&tmp);
zend_hash_add_new(ht, AST_STR(str_attributes), &tmp);
zend_hash_add_new(ht, AST_STR(str_docComment), &tmp);
return;
} else if (ast_kind == ZEND_AST_PROP_GROUP) {
zval tmp;
ZVAL_NULL(&tmp);
zend_hash_add_new(ht, AST_STR(str_attributes), &tmp);
return;
}
}
#endif
#if PHP_VERSION_ID < 80400
if (state->version >= 110) {
if (ast_kind == ZEND_AST_PARAM || ast_kind == ZEND_AST_PROP_ELEM) {
zval tmp;
ZVAL_NULL(&tmp);
zend_hash_add_new(ht, AST_STR(str_hooks), &tmp);
return;
}
}
#endif
if (ast_kind_is_decl(ast_kind)) {
zval id_zval;
#if PHP_VERSION_ID < 80000
if (state->version >= 80) {
zval tmp;
ZVAL_NULL(&tmp);
zend_hash_add_new(ht, AST_STR(str_attributes), &tmp);
}
#endif
#if PHP_VERSION_ID < 80100
if (ast_kind == ZEND_AST_CLASS) {
if (state->version >= 85) {
zval tmp;
ZVAL_NULL(&tmp);
zend_hash_add_new(ht, AST_STR(str_type), &tmp);
}
}
#endif
#if PHP_VERSION_ID < 80300
if (ast_kind == ZEND_AST_CLASS_CONST_GROUP) {
if (state->version >= 100) {
zval tmp;
ZVAL_NULL(&tmp);
zend_hash_add_new(ht, AST_STR(str_type), &tmp);
}
}
#endif
ZVAL_LONG(&id_zval, state->declIdCounter);
state->declIdCounter++;
zend_hash_add_new(ht, AST_STR(str___declId), &id_zval);
}
}
static void ast_to_zval(zval *zv, zend_ast *ast, ast_state_info_t *state) {
zval tmp_zv;
if (ast == NULL) {
ZVAL_NULL(zv);
return;
}
if (ast->kind == ZEND_AST_ZVAL) {
ZVAL_COPY(zv, zend_ast_get_zval(ast));
return;
}
switch (ast->kind) {
#if PHP_VERSION_ID < 70400
case ZEND_AST_ASSIGN_OP:
ast->attr = ast_assign_op_to_binary_op(ast->attr);
break;
#endif
case ZEND_AST_GREATER:
ast->kind = ZEND_AST_BINARY_OP;
ast->attr = AST_BINARY_IS_GREATER;
break;
case ZEND_AST_GREATER_EQUAL:
ast->kind = ZEND_AST_BINARY_OP;
ast->attr = AST_BINARY_IS_GREATER_OR_EQUAL;
break;
case ZEND_AST_OR:
ast->kind = ZEND_AST_BINARY_OP;
ast->attr = AST_BINARY_BOOL_OR;
break;
case ZEND_AST_AND:
ast->kind = ZEND_AST_BINARY_OP;
ast->attr = AST_BINARY_BOOL_AND;
break;
case ZEND_AST_COALESCE:
ast->kind = ZEND_AST_BINARY_OP;
ast->attr = AST_BINARY_COALESCE;
break;
case ZEND_AST_SILENCE:
ast->kind = ZEND_AST_UNARY_OP;
ast->attr = AST_SILENCE;
break;
case ZEND_AST_UNARY_PLUS:
ast->kind = ZEND_AST_UNARY_OP;
ast->attr = AST_PLUS;
break;
case ZEND_AST_UNARY_MINUS:
ast->kind = ZEND_AST_UNARY_OP;
ast->attr = AST_MINUS;
break;
#if PHP_VERSION_ID >= 80000
case ZEND_AST_CLASS_CONST_GROUP:
// ast->child is [AST_CLASS_CONST_DECL, optional attributes_list]
if (state->version < 80) {
// Keep class constants as a list of numerically indexed values in php 8
ast->child[0]->attr = ast->attr;
ast_to_zval(zv, ast->child[0], state);
return;
}
break;
#endif
#if PHP_VERSION_ID >= 70400
case ZEND_AST_PROP_GROUP:
if (state->version < 70) {
// In versions less than 70, just omit property type information entirely.
// ast->child is [type_ast, prop_ast]
ast_to_zval(zv, ast->child[1], state);
// The property visibility is on the AST_PROP_GROUP node.
// Add it to the AST_PROP_DECL node for old
AST_NODE_SET_PROP_FLAGS(Z_OBJ_P(zv), ast->attr);
return;
}
break;
case ZEND_AST_ASSIGN_COALESCE:
ast->kind = ZEND_AST_ASSIGN_OP;
ast->attr = AST_BINARY_COALESCE;
break;