Repository navigation
Expand file tree
/
Copy pathNodeScopeResolver.cpp
More file actions
2892 lines (2620 loc) · 121 KB
/
Copy pathNodeScopeResolver.cpp
File metadata and controls
2892 lines (2620 loc) · 121 KB
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
/*
* PHPStanTurbo\NodeScopeResolver — native implementation of
* PHPStan\Analyser\NodeScopeResolver.
*
* The hub of the analysis walk. processExprNode() / processStmtNode() /
* processStmtNodesInternal() reach the handlers through
* pt_expr_handler_process() / pt_stmt_handler_process() and the native
* registries, store results, apply NonNullabilityHelper's pending ensures and
* emit the node callbacks without a PHP frame of their own; the on-demand
* walks (processExprOnDemand(), processSyntheticOnDemand(),
* readTypeOfMaybeStored(), findScopeStateType(), ...) likewise.
*
* The class is not final and a DI service (#[AutowiredService] with an
* #[AutowiredExtensions] parameter): the constructor keeps the twin's
* arginfo, the twin's public static properties ($guardNewWorld,
* $guardRealExprIds, $guardProcessedExprIds) stay real static properties
* (MutatingScope and PHP code read them), and every $this-call of a public
* method takes the native body only for an instance of exactly this class —
* a PHP subclass may override it — and the method through the object's
* class entry otherwise. Other native classes call the direct entries
* pt_node_scope_resolver_*() (support.h), which follow the same rule.
*
* The twin's try/finally blocks (storage push/pop, gatherer suspend/restore,
* the walk-mode flags) are pt_finally() blocks: they run with a pending
* exception set aside, as PHP runs them.
*
* Deep recursion: processExprNode(), processStmtNode() and
* processStmtNodesInternal() continue on a fresh C stack segment when the
* current one runs low (Engine.h) — the twin recursed on the VM stack, the
* native walk re-enters execute_ex() for every PHP handler on its way down.
*
* InternalStatementResult, InternalThrowPoint, TemplateArgumentFrame and
* RecordingNodeCallback are native (their entries and the AnalyserValues.h
* readers); NodeFinder, the template-argument observer/constraints/stats and
* the virtual call-expression nodes stay PHP and are reached through the
* cached method sites in the block below, one helper each.
*/
#include "support.h"
#include "generated/NodeScopeResolver.h"
namespace slots = ptdecl::NodeScopeResolver::slot;
namespace sigs = ptdecl::NodeScopeResolver::sig;
#include "zv.h"
#include "TypeTraits.h"
#include "TypeOps.h"
#include "Engine.h"
#include "AnalyserValues.h"
#include "generated/RecordingNodeCallback.h"
#include <cstdlib>
#include <cstring>
zend_class_entry *pt_ce_node_scope_resolver = nullptr;
namespace {
/* {{{ the twin's static properties */
struct NsrStatics
{
zend_class_entry *ce;
uint32_t generation;
zval *guardNewWorld;
zval *guardRealExprIds;
zval *guardProcessedExprIds;
};
NsrStatics pt_nsr_statics;
zval *staticSlotOf(zend_class_entry *ce, const char *name, size_t len)
{
zend_property_info *info = (zend_property_info *) zend_hash_str_find_ptr(&PT_CE(ce, properties_info), name, len);
ZEND_ASSERT(info != NULL && (info->flags & ZEND_ACC_STATIC) != 0);
return CE_STATIC_MEMBERS(ce) + info->offset;
}
/* the slots of NodeScopeResolver::$guard* (the declaring class's, as the
* twin's self:: addresses them) */
const NsrStatics &statics()
{
zend_class_entry *ce = pt_ce_node_scope_resolver;
if (UNEXPECTED(pt_nsr_statics.ce != ce || pt_nsr_statics.generation != pt_engine_generation)) {
if (CE_STATIC_MEMBERS(ce) == NULL) {
zend_class_init_statics(ce);
}
pt_nsr_statics.ce = ce;
pt_nsr_statics.generation = pt_engine_generation;
pt_nsr_statics.guardNewWorld = staticSlotOf(ce, PT_LC("guardNewWorld"));
pt_nsr_statics.guardRealExprIds = staticSlotOf(ce, PT_LC("guardRealExprIds"));
pt_nsr_statics.guardProcessedExprIds = staticSlotOf(ce, PT_LC("guardProcessedExprIds"));
}
return pt_nsr_statics;
}
inline zval *staticValue(zval *slot)
{
ZVAL_DEINDIRECT(slot);
ZVAL_DEREF(slot);
return slot;
}
/* self::$guardNewWorld */
inline bool guardNewWorld()
{
return Z_TYPE_P(staticValue(statics().guardNewWorld)) == IS_TRUE;
}
/* self::$<array> = [] */
void resetStaticArray(zval *slot)
{
zval *value = staticValue(slot);
zval old;
ZVAL_COPY_VALUE(&old, value);
ZVAL_EMPTY_ARRAY(value);
zval_ptr_dtor(&old);
}
/* self::$<array>[$id] = true */
void setStaticArrayFlag(zval *slot, zend_ulong id)
{
zval *value = staticValue(slot);
if (UNEXPECTED(Z_TYPE_P(value) != IS_ARRAY)) {
zval_ptr_dtor(value);
ZVAL_EMPTY_ARRAY(value);
}
SEPARATE_ARRAY(value);
zval flag;
ZVAL_TRUE(&flag);
zend_hash_index_update(Z_ARRVAL_P(value), id, &flag);
}
/* isset(self::$<array>[$id]) */
bool staticArrayHas(zval *slot, zend_ulong id)
{
zval *value = staticValue(slot);
if (Z_TYPE_P(value) != IS_ARRAY) return false;
zval *found = zend_hash_index_find(Z_ARRVAL_P(value), id);
if (found == NULL) return false;
ZVAL_DEREF(found);
return Z_TYPE_P(found) != IS_NULL;
}
/* }}} */
/* {{{ the PHP collaborators (one site each; switch to their direct entries
* once they are ported) */
pt_method_site pt_nsr_get_by_type_site;
pt_method_site pt_nsr_normalize_path_site;
pt_method_site pt_nsr_find_instance_of_site;
pt_method_site pt_nsr_enable_from_environment_site;
pt_method_site pt_nsr_function_get_return_type_site;
pt_method_site pt_nsr_get_sub_node_names_site;
pt_property_site pt_nsr_name_site;
pt_property_site pt_nsr_var_site;
pt_property_site pt_nsr_class_site;
pt_property_site pt_nsr_dim_site;
pt_property_site pt_nsr_items_site;
pt_property_site pt_nsr_item_value_site;
pt_property_site pt_nsr_stmt_name_site;
pt_property_site pt_nsr_hook_fetch_var_site;
pt_property_site pt_nsr_hook_fetch_name_site;
pt_property_site pt_nsr_hook_this_name_site;
/* $container->getByType($className) */
zv::Val containerGetByType(zval *container, const char *className, size_t len)
{
zval name;
ZVAL_STRINGL(&name, className, len);
zv::Val service = pt_call_method_cached(pt_nsr_get_by_type_site, Z_OBJ_P(container), PT_LC("getbytype"), 1, &name);
zval_ptr_dtor(&name);
return service;
}
/* $extensionsCollection->getAll() */
zv::Val extensionsCollectionGetAll(zval *collection)
{
return pt_extensions_collection_get_all(Z_OBJ_P(collection));
}
/* $fileHelper->normalizePath($path) */
zv::Val fileHelperNormalizePath(zval *fileHelper, zval *path)
{
return pt_call_method_cached(pt_nsr_normalize_path_site, Z_OBJ_P(fileHelper), PT_LC("normalizepath"), 1, path);
}
/* (new NodeFinder())->findInstanceOf($nodes, Expr::class) */
zv::Val nodeFinderFindExprs(zval *nodes)
{
zv::Val finder = pt_type_new(PT_CLASS_NODE_FINDER, 0, NULL);
if (UNEXPECTED(finder.isUndef())) return zv::Val();
zval exprClass;
ZVAL_STRINGL(&exprClass, "PhpParser\\Node\\Expr", sizeof("PhpParser\\Node\\Expr") - 1);
zv::Args argv{nodes, &exprClass};
zv::Val found = pt_call_method_cached(pt_nsr_find_instance_of_site, Z_OBJ_P(finder.raw()), PT_LC("findinstanceof"), 2, argv);
zval_ptr_dtor(&exprClass);
return found;
}
/* an owned copy of a borrowed read of AnalyserValues.h (NULL = pending
* exception) */
inline zv::Val owned(zval *value)
{
return value != NULL ? zv::Val::copyOf(zv::Ref(value)) : zv::Val();
}
/* $expressionResult->getScope() / ->getBeforeScope() / ->getThrowPoints() /
* ->getImpurePoints(), owned */
inline zv::Val expressionResultScope(zval *result)
{
zv::Val hold;
return owned(pt_expression_result_scope(result, hold));
}
inline zv::Val expressionResultBeforeScope(zval *result)
{
zv::Val hold;
return owned(pt_expression_result_before_scope(result, hold));
}
inline zv::Val expressionResultThrowPoints(zval *result)
{
zv::Val hold;
return owned(pt_expression_result_throw_points(result, hold));
}
inline zv::Val expressionResultImpurePoints(zval *result)
{
zv::Val hold;
return owned(pt_expression_result_impure_points(result, hold));
}
/* new InternalStatementResult($scope, false, false, [], $throwPoints, []) */
zv::Val emptyInternalStatementResult(zval *scope, zval *throwPoints, zval *dependencies = NULL)
{
zval empty;
ZVAL_EMPTY_ARRAY(&empty);
return pt_internal_statement_result_new(scope, false, false, &empty, throwPoints != NULL ? throwPoints : &empty, &empty, NULL, NULL, -1, dependencies);
}
/* $frame->isObserving(); false = pending exception */
[[nodiscard]] bool templateArgumentFrameIsObserving(zval *frame, bool &out)
{
if (UNEXPECTED(Z_TYPE_P(frame) != IS_OBJECT)) {
zend_throw_error(NULL, "Call to a member function isObserving() on %s", zend_zval_value_name(frame));
return false;
}
return pt_template_argument_frame_is_observing(frame, out);
}
/* $templateArgumentObserver->collectSites($type) */
zv::Val templateArgumentObserverCollectSites(zval *observer, zval *type)
{
return pt_template_argument_observer_collect_sites(observer, type);
}
/* $templateArgumentObserver->collectSend($declared, $actual) */
zv::Val templateArgumentObserverCollectSend(zval *observer, zval *declared, zval *actual)
{
return pt_template_argument_observer_collect_send(observer, declared, actual);
}
/* TemplateArgumentConstraints::createEmpty() */
zv::Val templateArgumentConstraintsCreateEmpty()
{
return pt_template_argument_constraints_create_empty();
}
/* TemplateArgumentStats::enableFromEnvironment() */
zv::Val templateArgumentStatsEnableFromEnvironment()
{
return pt_call_static_cached(pt_nsr_enable_from_environment_site, PT_CLASS_TEMPLATE_ARGUMENT_STATS, PT_LC("enablefromenvironment"), 0, NULL);
}
/* $function->getReturnType() */
zv::Val functionGetReturnType(zval *function)
{
if (UNEXPECTED(Z_TYPE_P(function) != IS_OBJECT)) {
zend_throw_error(NULL, "Call to a member function getReturnType() on %s", zend_zval_value_name(function));
return zv::Val();
}
return pt_call_method_cached(pt_nsr_function_get_return_type_site, Z_OBJ_P(function), PT_LC("getreturntype"), 0, NULL);
}
/* {{{ RecordingNodeCallback (RecordingNodeCallback.cpp) */
/* $nodeCallback instanceof RecordingNodeCallback (a final class): the
* native class, or — under the prefixed differential activation, where the
* twin keeps the real name — the twin */
inline bool isRecordingNodeCallback(zval *nodeCallback)
{
if (Z_TYPE_P(nodeCallback) != IS_OBJECT) return false;
zend_class_entry *ce = Z_OBJCE_P(nodeCallback);
return EXPECTED(ce == pt_ce_recording_node_callback) || UNEXPECTED(zend_string_equals_literal(ce->name, "PHPStan\\Analyser\\RecordingNodeCallback"));
}
/* $recording($node, $scope); false = pending exception */
[[nodiscard]] bool recordingNodeCallbackInvoke(zval *recording, zval *node, zval *scope)
{
if (EXPECTED(Z_OBJCE_P(recording) == pt_ce_recording_node_callback)) return pt_recording_node_callback_record(Z_OBJ_P(recording), node, scope);
zv::Args argv{node, scope};
return !pt_type_call(Z_OBJ_P(recording), PT_LC("__invoke"), 2, argv).isUndef();
}
/* $recording->getPairs() */
zv::Val recordingNodeCallbackGetPairs(zval *recording)
{
if (EXPECTED(Z_OBJCE_P(recording) == pt_ce_recording_node_callback)) return zv::Val::copyOf(zv::Ref(OBJ_PROP_NUM(Z_OBJ_P(recording), ptdecl::RecordingNodeCallback::slot::pairs)));
return pt_type_call(Z_OBJ_P(recording), PT_LC("getpairs"), 0, NULL);
}
/* $recording->count(); false = pending exception */
[[nodiscard]] bool recordingNodeCallbackCount(zval *recording, zend_long &out)
{
if (EXPECTED(Z_OBJCE_P(recording) == pt_ce_recording_node_callback)) {
zval *pairs = OBJ_PROP_NUM(Z_OBJ_P(recording), ptdecl::RecordingNodeCallback::slot::pairs);
out = Z_TYPE_P(pairs) == IS_ARRAY ? (zend_long) zend_hash_num_elements(Z_ARRVAL_P(pairs)) : 0;
return true;
}
zv::Val count = pt_type_call(Z_OBJ_P(recording), PT_LC("count"), 0, NULL);
if (UNEXPECTED(count.isUndef())) return false;
out = zval_get_long(count.raw());
return true;
}
/* }}} */
/* $nodeCallback instanceof NoopNodeCallback (a final class; see above) */
inline bool isNoopNodeCallback(zval *nodeCallback)
{
return Z_TYPE_P(nodeCallback) == IS_OBJECT && Z_OBJCE_P(nodeCallback) == pt_class(PT_CLASS_NOOP_NODE_CALLBACK);
}
/* $node->getSubNodeNames() */
zv::Val nodeGetSubNodeNames(zend_object *node)
{
return pt_call_method_cached(pt_nsr_get_sub_node_names_site, node, PT_LC("getsubnodenames"), 0, NULL);
}
/* }}} */
/* {{{ node shapes */
/* what processStmtNode() asks about a statement's class, decided once per
* class: the declarations its PHPDocs are not applied to, Foreach_ (no @var
* before the walk), ClassMethod (the trait prelude) and the statements whose
* node callback their handler emits after walking their expressions */
enum : uint32_t
{
PT_NSR_STMT_DECLARATION = 1u << 0,
PT_NSR_STMT_FOREACH = 1u << 1,
PT_NSR_STMT_CLASS_METHOD = 1u << 2,
PT_NSR_STMT_DEFERRED_CALLBACK = 1u << 3,
};
struct StmtShapeEntry
{
zend_class_entry *ce;
uint32_t generation;
uint32_t shape;
};
#define PT_NSR_STMT_SHAPE_CACHE_BITS 8
StmtShapeEntry pt_nsr_stmt_shapes[1u << PT_NSR_STMT_SHAPE_CACHE_BITS];
/* the shape bits of a statement class; false = pending exception */
[[nodiscard]] bool computeStmtShape(zend_class_entry *ce, uint32_t &shape)
{
shape = 0;
static const int declarations[] = { PT_CLASS_STATIC_STMT, PT_CLASS_GLOBAL_STMT, PT_CLASS_PROPERTY_STMT, PT_CLASS_CLASS_CONST_STMT, PT_CLASS_CONST_STMT, PT_CLASS_CLASS_LIKE_STMT, PT_CLASS_FUNCTION_STMT, PT_CLASS_CLASS_METHOD_STMT };
static const int deferred[] = { PT_CLASS_RETURN_STMT, PT_CLASS_EXPRESSION_STMT, PT_CLASS_ECHO_STMT, PT_CLASS_IF_STMT, PT_CLASS_SWITCH_STMT, PT_CLASS_FOREACH_STMT, PT_CLASS_UNSET_STMT, PT_CLASS_CLASS_CONST_STMT, PT_CLASS_CONST_STMT, PT_CLASS_WHILE_STMT, PT_CLASS_DO_STMT };
for (int classIdx : declarations) {
zend_class_entry *target = pt_class(classIdx);
if (UNEXPECTED(target == NULL)) return false;
if (instanceof_function(ce, target)) {
shape |= PT_NSR_STMT_DECLARATION;
break;
}
}
zend_class_entry *foreachCe = pt_class(PT_CLASS_FOREACH_STMT);
zend_class_entry *classMethodCe = pt_class(PT_CLASS_CLASS_METHOD_STMT);
if (UNEXPECTED(foreachCe == NULL || classMethodCe == NULL)) return false;
if (instanceof_function(ce, foreachCe)) shape |= PT_NSR_STMT_FOREACH;
if (instanceof_function(ce, classMethodCe)) shape |= PT_NSR_STMT_CLASS_METHOD;
for (int classIdx : deferred) {
zend_class_entry *target = pt_class(classIdx);
if (UNEXPECTED(target == NULL)) return false;
if (instanceof_function(ce, target)) {
shape |= PT_NSR_STMT_DEFERRED_CALLBACK;
break;
}
}
return true;
}
[[nodiscard]] inline bool stmtShapeOf(zend_class_entry *ce, uint32_t &shape)
{
uintptr_t h = ((uintptr_t) ce >> 4) * (uintptr_t) 0x9E3779B97F4A7C15ull;
StmtShapeEntry &entry = pt_nsr_stmt_shapes[(size_t) (h >> (sizeof(uintptr_t) * 8 - PT_NSR_STMT_SHAPE_CACHE_BITS))];
if (EXPECTED(entry.ce == ce && entry.generation == pt_engine_generation)) {
shape = entry.shape;
return true;
}
if (UNEXPECTED(!computeStmtShape(ce, shape))) return false;
entry = { ce, pt_engine_generation, shape };
return true;
}
/* $object instanceof <class-map class>; false = pending exception */
[[nodiscard]] inline bool isInstance(zend_object *object, int classIdx, bool &out)
{
zend_class_entry *ce = pt_class(classIdx);
if (UNEXPECTED(ce == NULL)) return false;
out = instanceof_function(object->ce, ce);
return true;
}
/* $zv instanceof <class-map class> for any value; false = pending exception */
[[nodiscard]] inline bool isInstanceValue(zval *value, int classIdx, bool &out)
{
ZVAL_DEREF(value);
if (Z_TYPE_P(value) != IS_OBJECT) {
out = false;
return true;
}
return isInstance(Z_OBJ_P(value), classIdx, out);
}
/* $node->$name of a declared property through a per-site offset; the
* Error the engine raises for an undeclared or uninitialized one */
zval *nodeProperty(pt_property_site &site, zend_object *node, const char *name, size_t len)
{
zval *slot = pt_property_cached(site, node, name, len);
if (UNEXPECTED(slot == NULL)) {
zend_throw_error(NULL, "phpstan_turbo: %s has no declared property $%s", ZSTR_VAL(node->ce->name), name);
return NULL;
}
ZVAL_DEINDIRECT(slot);
ZVAL_DEREF(slot);
if (UNEXPECTED(Z_TYPE_P(slot) == IS_UNDEF)) {
zend_throw_error(NULL, "Typed property %s::$%s must not be accessed before initialization", ZSTR_VAL(node->ce->name), name);
return NULL;
}
return slot;
}
/* get_class($node) . ' on line ' . $node->getStartLine() into a
* ShouldNotHappenException message */
void throwWithNodeLine(zend_object *node, const char *format)
{
zv::Val line = pt_type_call(node, PT_LC("getstartline"), 0, NULL);
if (UNEXPECTED(line.isUndef())) return;
zend_class_entry *ce = pt_class(PT_CLASS_SHOULD_NOT_HAPPEN);
if (UNEXPECTED(ce == NULL)) return;
zend_throw_exception_ex(ce, 0, format, ZSTR_VAL(node->ce->name), (zend_long) zval_get_long(line.raw()));
}
/* the Error for reading an uninitialized typed property of the resolver */
zv::Val uninitialized(const char *name)
{
zend_throw_error(NULL, "Typed property PHPStan\\Analyser\\NodeScopeResolver::$%s must not be accessed before initialization", name);
return zv::Val();
}
/* }}} */
} // namespace
namespace phpstanturbo {
/* Mirrors PHPStan\Analyser\NodeScopeResolver; UNDEF / false = pending
* exception. */
class NodeScopeResolver
{
public:
explicit NodeScopeResolver(zend_object *self) : self(self) {}
/* the constructor body: the promoted properties, then the twin's body */
[[nodiscard]] bool construct(zval *container, zval *templateArgumentObserver, zval *fileHelper, zval *perFileAnalysisResettables, zval *expressionResultFactory, zval *statementsHandler)
{
zv::ObjRef object(self);
object.propAtWrite(slots::container, zv::Val::copyOf(zv::Ref(container)));
object.propAtWrite(slots::templateArgumentObserver, zv::Val::copyOf(zv::Ref(templateArgumentObserver)));
object.propAtWrite(slots::fileHelper, zv::Val::copyOf(zv::Ref(fileHelper)));
object.propAtWrite(slots::perFileAnalysisResettables, zv::Val::copyOf(zv::Ref(perFileAnalysisResettables)));
object.propAtWrite(slots::expressionResultFactory, zv::Val::copyOf(zv::Ref(expressionResultFactory)));
object.propAtWrite(slots::statementsHandler, zv::Val::copyOf(zv::Ref(statementsHandler)));
// self::$guardNewWorld = getenv('PHPSTAN_GUARD_NW') === '1';
const char *guard = getenv("PHPSTAN_GUARD_NW");
zval *guardSlot = staticValue(statics().guardNewWorld);
ZVAL_BOOL(guardSlot, guard != NULL && strcmp(guard, "1") == 0);
return !templateArgumentStatsEnableFromEnvironment().isUndef();
}
/* Mirrors setAnalysedFiles(). */
[[nodiscard]] bool setAnalysedFiles(HashTable *files)
{
zv::Ref fileHelper = slot(slots::fileHelper);
zv::Arr analysedFiles = zv::Arr::empty();
for (auto entry : zv::TableRef(files)) {
if (UNEXPECTED(!fileHelper.isObject())) {
(void) uninitialized("fileHelper");
return false;
}
zv::Val normalized = fileHelperNormalizePath(fileHelper.raw(), entry.value().deref().raw());
if (UNEXPECTED(normalized.isUndef())) return false;
zval flag;
ZVAL_TRUE(&flag);
analysedFiles.separate();
array_set_zval_key(analysedFiles.table(), normalized.raw(), &flag);
if (UNEXPECTED(EG(exception))) return false;
}
zv::ObjRef(self).propAtWrite(slots::analysedFiles, std::move(analysedFiles));
return true;
}
/* Mirrors resetPerFileAnalysisState(). */
[[nodiscard]] bool resetPerFileAnalysisState()
{
zv::Ref collection = slot(slots::perFileAnalysisResettables);
if (UNEXPECTED(!collection.isObject())) return !uninitialized("perFileAnalysisResettables").isUndef();
zv::Val all = extensionsCollectionGetAll(collection.raw());
if (UNEXPECTED(all.isUndef())) return false;
if (UNEXPECTED(!all.ref().isArray())) {
zend_error(E_WARNING, "foreach() argument must be of type array|object, %s given", zend_zval_value_name(all.raw()));
return EG(exception) == NULL;
}
for (auto entry : zv::ArrRef(all.raw())) {
zv::Ref service = entry.value().deref();
if (UNEXPECTED(!service.isObject())) {
zend_throw_error(NULL, "Call to a member function resetFileAnalysisState() on %s", zend_zval_value_name(service.raw()));
return false;
}
if (UNEXPECTED(!pt_non_nullability_helper_reset_file_analysis_state(service.raw()))) return false;
}
return true;
}
/* Mirrors processNodes(): $this->processFileNodes(...), through the
* object's class (a subclass may override it); false = pending exception */
[[nodiscard]] bool processNodes(zval *nodes, zval *scopeArg, zval *nodeCallback)
{
if (exact()) return !processFileNodes(nodes, scopeArg, nodeCallback).isUndef();
zv::Args argv{nodes, scopeArg, nodeCallback};
return !pt_type_call(self, PT_LC("processfilenodes"), 3, argv).isUndef();
}
/* Mirrors processFileNodes(): what the file depends on, UNDEF = pending
* exception */
zv::Val processFileNodes(zval *nodes, zval *scopeArg, zval *nodeCallback)
{
zv::Val scope = pt_mutating_scope_to_walk_scope(Z_OBJ_P(scopeArg));
if (UNEXPECTED(scope.isUndef())) return zv::Val();
if (UNEXPECTED(!requireObject(scope, "pushExpressionResultStorage"))) return zv::Val();
if (guardNewWorld()) {
const NsrStatics &s = statics();
resetStaticArray(s.guardRealExprIds);
resetStaticArray(s.guardProcessedExprIds);
zv::Val realExprs = nodeFinderFindExprs(nodes);
if (UNEXPECTED(realExprs.isUndef())) return zv::Val();
if (realExprs.ref().isArray()) {
for (auto entry : zv::ArrRef(realExprs.raw())) {
zv::Ref realExpr = entry.value().deref();
if (realExpr.isObject()) {
setStaticArrayFlag(statics().guardRealExprIds, realExpr.asObject()->handle);
}
}
}
}
zv::Val storage = pt_expression_result_storage_new();
if (UNEXPECTED(storage.isUndef())) return zv::Val();
if (UNEXPECTED(!pt_mutating_scope_push_expression_result_storage(Z_OBJ_P(scope.raw()), storage.raw()))) return zv::Val();
zv::Val gatherers = thisSuspendNodeGatherers();
if (UNEXPECTED(gatherers.isUndef())) return zv::Val();
zval thisZv;
ZVAL_OBJ(&thisZv, self);
zv::Val dependencies;
zval *statementsHandler = requireStatementsHandler();
if (EXPECTED(statementsHandler != NULL)) {
dependencies = pt_statements_handler_process_nodes_with_storage(statementsHandler, &thisZv, nodes, scope.raw(), storage.raw(), nodeCallback);
}
pt_finally([&]() {
if (thisRestoreNodeGatherers(gatherers.raw())) {
(void) pt_mutating_scope_pop_expression_result_storage(Z_OBJ_P(scope.raw()));
}
});
if (UNEXPECTED(EG(exception) != NULL)) return zv::Val();
return dependencies;
}
/* Mirrors storeExpressionResult(). */
void storeExpressionResult(zval *storage, zval *expr, zval *expressionResult)
{
if (UNEXPECTED(guardNewWorld())) {
setStaticArrayFlag(statics().guardProcessedExprIds, Z_OBJ_HANDLE_P(expr));
}
pt_expression_result_storage_store(storage, expr, expressionResult);
}
/* Mirrors narrowScopeWithCondition(). */
zv::Val narrowScopeWithCondition(zval *scope, zval *expr, zval *context)
{
zv::Val specifiedTypes = pt_mutating_scope_specify_types_of_new_world_handler_node(Z_OBJ_P(scope), Z_OBJ_P(expr), context);
if (UNEXPECTED(specifiedTypes.isUndef())) return zv::Val();
return pt_mutating_scope_apply_specified_types(Z_OBJ_P(scope), specifiedTypes.raw());
}
/* Mirrors processStmtNodes(). */
zv::Val processStmtNodes(zval *parentNode, zval *stmts, zval *scopeArg, zval *nodeCallback, zval *context)
{
zv::Val scope = pt_mutating_scope_to_walk_scope(Z_OBJ_P(scopeArg));
if (UNEXPECTED(scope.isUndef())) return zv::Val();
if (UNEXPECTED(!requireObject(scope, "pushExpressionResultStorage"))) return zv::Val();
zv::Val storage = pt_expression_result_storage_new();
if (UNEXPECTED(storage.isUndef())) return zv::Val();
if (UNEXPECTED(!pt_mutating_scope_push_expression_result_storage(Z_OBJ_P(scope.raw()), storage.raw()))) return zv::Val();
zv::Val gatherers = thisSuspendNodeGatherers();
if (UNEXPECTED(gatherers.isUndef())) return zv::Val();
zv::Val result = thisProcessStmtNodesInternal(parentNode, stmts, scope.raw(), storage.raw(), nodeCallback, context);
zv::Val publicResult;
if (EXPECTED(!result.isUndef())) {
if (UNEXPECTED(!result.ref().isObject())) {
zend_throw_error(NULL, "Call to a member function toPublic() on %s", zend_zval_value_name(result.raw()));
} else {
publicResult = pt_internal_statement_result_to_public(result.raw());
}
}
pt_finally([&]() {
if (thisRestoreNodeGatherers(gatherers.raw())) {
(void) pt_mutating_scope_pop_expression_result_storage(Z_OBJ_P(scope.raw()));
}
});
if (UNEXPECTED(EG(exception))) return zv::Val();
return publicResult;
}
/* Mirrors processStmtNodesInternal(). */
zv::Val processStmtNodesInternal(zval *parentNode, zval *stmts, zval *scope, zval *storage, zval *nodeCallback, zval *context)
{
zv::Val result;
pt_engine_with_stack([&]() { result = processStmtNodesInternalBody(parentNode, stmts, scope, storage, nodeCallback, context); });
return result;
}
/* Mirrors processLoopFinalStmtNodes(). */
zv::Val processLoopFinalStmtNodes(zval *parentNode, zval *stmts, zval *scope, zval *storage, zval *nodeCallback, zval *context, zval *lastPass)
{
zv::Val result;
pt_engine_with_stack([&]() {
zv::Val current = pt_mutating_scope_get_current_expression_result_storage(Z_OBJ_P(scope));
if (UNEXPECTED(current.isUndef())) return;
bool pushStorage = !(current.ref().isObject() && Z_OBJ_P(current.raw()) == Z_OBJ_P(storage));
if (pushStorage && UNEXPECTED(!pt_mutating_scope_push_expression_result_storage(Z_OBJ_P(scope), storage))) return;
zval thisZv;
ZVAL_OBJ(&thisZv, self);
zval *statementsHandler = requireStatementsHandler();
if (EXPECTED(statementsHandler != NULL)) {
result = pt_statements_handler_process_loop_final_stmt_nodes(statementsHandler, &thisZv, parentNode, stmts, scope, storage, nodeCallback, context, lastPass);
}
if (pushStorage) {
pt_finally([&]() { (void) pt_mutating_scope_pop_expression_result_storage(Z_OBJ_P(scope)); });
if (UNEXPECTED(EG(exception))) result = zv::Val();
}
});
return result;
}
/* Mirrors processLoopPassStmtNodes(); previous IS_NULL for the first
* pass, record receives this pass's [entries, recording] */
zv::Val processLoopPassStmtNodes(zval *parentNode, zval *stmts, zval *scope, zval *storage, zval *nodeCallback, zval *context, zval *previous, zv::Val &record)
{
zv::Val result;
pt_engine_with_stack([&]() {
zv::Val current = pt_mutating_scope_get_current_expression_result_storage(Z_OBJ_P(scope));
if (UNEXPECTED(current.isUndef())) return;
bool pushStorage = !(current.ref().isObject() && Z_OBJ_P(current.raw()) == Z_OBJ_P(storage));
if (pushStorage && UNEXPECTED(!pt_mutating_scope_push_expression_result_storage(Z_OBJ_P(scope), storage))) return;
zval thisZv;
ZVAL_OBJ(&thisZv, self);
zval *statementsHandler = requireStatementsHandler();
if (EXPECTED(statementsHandler != NULL)) {
result = pt_statements_handler_process_loop_pass_stmt_nodes(statementsHandler, &thisZv, parentNode, stmts, scope, storage, nodeCallback, context, previous, record);
}
if (pushStorage) {
pt_finally([&]() { (void) pt_mutating_scope_pop_expression_result_storage(Z_OBJ_P(scope)); });
if (UNEXPECTED(EG(exception))) result = zv::Val();
}
});
return result;
}
/* Mirrors processStmtNode(). */
zv::Val processStmtNode(zval *stmt, zval *scope, zval *storage, zval *nodeCallback, zval *context)
{
zv::Val result;
pt_engine_with_stack([&]() { result = processStmtNodeBody(stmt, scope, storage, nodeCallback, context); });
return result;
}
/* Mirrors isAnalysedFile(). */
bool isAnalysedFile(zend_string *fileName) const
{
zval *analysedFiles = OBJ_PROP_NUM(self, slots::analysedFiles);
ZVAL_DEREF(analysedFiles);
if (UNEXPECTED(Z_TYPE_P(analysedFiles) != IS_ARRAY)) return false;
zval *found = zend_symtable_find(Z_ARRVAL_P(analysedFiles), fileName);
if (found == NULL) return false;
ZVAL_DEREF(found);
return Z_TYPE_P(found) != IS_NULL;
}
bool isReturningStoredExpressionResults() const { return flag(slots::returnStoredExpressionResults); }
/* Mirrors lookForSetAllowedUndefinedExpressions() /
* lookForUnsetAllowedUndefinedExpressions() over the private
* lookForExpressionCallback() */
zv::Val lookForSetAllowedUndefinedExpressions(zval *scope, zval *expr)
{
return lookForExpressionCallback(scope, expr, true);
}
zv::Val lookForUnsetAllowedUndefinedExpressions(zval *scope, zval *expr)
{
return lookForExpressionCallback(scope, expr, false);
}
/* Mirrors processExprNodeConsumingStored(). */
zv::Val processExprNodeConsumingStored(zval *stmt, zval *expr, zval *consumedExpr, zval *scope, zval *storage, zval *nodeCallback, zval *context)
{
zv::Val previous = zv::Val::copyOf(slot(slots::consumeStoredExpression).deref());
slot(slots::consumeStoredExpression).deref().assign(zv::Val::copyOf(zv::Ref(consumedExpr)));
zv::Val result = thisProcessExprNode(stmt, expr, scope, storage, nodeCallback, context);
pt_finally([&]() { slot(slots::consumeStoredExpression).deref().assign(std::move(previous)); });
if (UNEXPECTED(EG(exception))) return zv::Val();
return result;
}
/* Mirrors processExprOnDemand(). */
zv::Val processExprOnDemand(zval *expr, zval *scope, zval *storage)
{
zv::Ref container = slot(slots::container);
if (UNEXPECTED(!container.isObject())) return uninitialized("container");
zv::Val handler = pt_expr_handler_registry_resolve(Z_OBJ_P(expr), container.raw());
if (UNEXPECTED(handler.isUndef())) return zv::Val();
if (handler.isNull()) {
bool firstClassCallable = false;
bool isCallLike;
if (UNEXPECTED(!isInstance(Z_OBJ_P(expr), PT_CLASS_CALL_LIKE, isCallLike))) return zv::Val();
if (isCallLike && UNEXPECTED(!pt_call_like_is_first_class_callable(Z_OBJ_P(expr), firstClassCallable))) return zv::Val();
if (!firstClassCallable) {
zv::Val mixed = pt_type_new_mixed_type();
if (UNEXPECTED(mixed.isUndef())) return zv::Val();
zv::Val specifyTypesCallback = pt_specified_types_empty_specify_callback();
if (UNEXPECTED(specifyTypesCallback.isUndef())) return zv::Val();
zval null;
ZVAL_NULL(&null);
zv::Ref factory = slot(slots::expressionResultFactory);
if (UNEXPECTED(!factory.isObject())) return uninitialized("expressionResultFactory");
pt_expression_result_args args(scope, scope, expr, false, false, NULL, NULL, &null, specifyTypesCallback.raw());
args.withType(mixed.raw()).withNativeType(mixed.raw());
return pt_expression_result_create(factory.raw(), args);
}
}
bool previous = flag(slots::returnStoredExpressionResults);
setFlag(slots::returnStoredExpressionResults, true);
if (UNEXPECTED(!pt_mutating_scope_push_expression_result_storage(Z_OBJ_P(scope), storage))) return zv::Val();
zv::Val result;
zv::Val stmt = pt_type_new(PT_CLASS_EXPRESSION_STMT, 1, expr);
if (EXPECTED(!stmt.isUndef())) {
zv::Val noop = pt_type_new(PT_CLASS_NOOP_NODE_CALLBACK, 0, NULL);
if (EXPECTED(!noop.isUndef())) {
zv::Val context = pt_expression_context_create_top_level(false);
if (EXPECTED(!context.isUndef())) {
result = thisProcessExprNode(stmt.raw(), expr, scope, storage, noop.raw(), context.raw());
}
}
}
pt_finally([&]() {
if (pt_mutating_scope_pop_expression_result_storage(Z_OBJ_P(scope))) {
setFlag(slots::returnStoredExpressionResults, previous);
}
});
if (UNEXPECTED(EG(exception))) return zv::Val();
return result;
}
/* Mirrors readStoredResult(). */
zv::Val readStoredResult(zval *expr, zval *storage)
{
zv::Val result = pt_expression_result_storage_find(storage, expr);
if (UNEXPECTED(result.isUndef())) return zv::Val();
if (result.isNull()) {
throwWithNodeLine(Z_OBJ_P(expr), "%s on line " ZEND_LONG_FMT " has no stored ExpressionResult - it was not processed by processExprNode().");
return zv::Val();
}
return result;
}
/* Mirrors readTypeOfMaybeStored(). */
zv::Val readTypeOfMaybeStored(zval *expr, zval *scope)
{
zv::Val storage = pt_mutating_scope_get_current_expression_result_storage(Z_OBJ_P(scope));
if (UNEXPECTED(storage.isUndef())) return zv::Val();
if (!storage.isNull()) {
zv::Val result = pt_expression_result_storage_find(storage.raw(), expr);
if (UNEXPECTED(result.isUndef())) return zv::Val();
if (!result.isNull()) {
bool promoted;
if (UNEXPECTED(!pt_mutating_scope_native_types_promoted(Z_OBJ_P(scope), promoted))) return zv::Val();
return pt_expression_result_get_type_on_scope(result.raw(), scope, promoted);
}
}
return thisReadScopeStateOrSyntheticType(expr, scope);
}
/* Mirrors findScopeStateType(). */
zv::Val findScopeStateType(zval *expr, zval *scope)
{
zend_object *exprObject = Z_OBJ_P(expr);
zend_object *scopeObject = Z_OBJ_P(scope);
bool isVariable;
if (UNEXPECTED(!isInstance(exprObject, PT_CLASS_VARIABLE, isVariable))) return zv::Val();
if (isVariable) {
zval *name = nodeProperty(pt_nsr_name_site, exprObject, PT_LC("name"));
if (UNEXPECTED(name == NULL)) return zv::Val();
if (Z_TYPE_P(name) == IS_STRING) {
zv::Val has = pt_mutating_scope_has_variable_type(scopeObject, Z_STR_P(name));
if (UNEXPECTED(has.isUndef())) return zv::Val();
zend_long hasValue = pt_type_trinary_value(has.raw());
if (UNEXPECTED(hasValue < 0)) return zv::Val();
if (hasValue == PT_TRI_NO) return pt_type_new_error_type();
return pt_mutating_scope_get_variable_type(scopeObject, Z_STR_P(name));
}
}
bool isLiteral;
if (UNEXPECTED(!isInstance(exprObject, PT_CLASS_SCALAR_STRING, isLiteral))) return zv::Val();
if (!isLiteral && UNEXPECTED(!isInstance(exprObject, PT_CLASS_SCALAR_INT, isLiteral))) return zv::Val();
if (!isLiteral && UNEXPECTED(!isInstance(exprObject, PT_CLASS_SCALAR_FLOAT, isLiteral))) return zv::Val();
if (isLiteral) return pt_mutating_scope_get_state_type(scopeObject, exprObject);
bool isClosure;
if (UNEXPECTED(!isInstance(exprObject, PT_CLASS_CLOSURE_EXPR, isClosure))) return zv::Val();
if (!isClosure && UNEXPECTED(!isInstance(exprObject, PT_CLASS_ARROW_FUNCTION, isClosure))) return zv::Val();
if (!isClosure) {
zend_long hasValue = pt_mutating_scope_has_expression_type(scopeObject, expr);
if (UNEXPECTED(hasValue < 0)) return zv::Val();
if (hasValue == PT_TRI_YES) {
zv::Val tracked = pt_mutating_scope_get_tracked_expression_type(scopeObject, exprObject);
if (UNEXPECTED(tracked.isUndef())) return zv::Val();
return pt_type_utils_resolve_late_resolvable_types(tracked.raw());
}
}
return zv::Val::null();
}
/* Mirrors readScopeStateOrSyntheticType(). */
zv::Val readScopeStateOrSyntheticType(zval *expr, zval *scope)
{
zv::Val type = thisFindScopeStateType(expr, scope);
if (UNEXPECTED(type.isUndef())) return zv::Val();
if (!type.isNull()) return type;
zv::Val result = thisProcessSyntheticOnDemand(expr, scope);
if (UNEXPECTED(result.isUndef())) return zv::Val();
if (UNEXPECTED(!result.ref().isObject())) {
zend_throw_error(NULL, "Call to a member function getTypeOnScope() on %s", zend_zval_value_name(result.raw()));
return zv::Val();
}
bool promoted;
if (UNEXPECTED(!pt_mutating_scope_native_types_promoted(Z_OBJ_P(scope), promoted))) return zv::Val();
return pt_expression_result_get_type_on_scope(result.raw(), scope, promoted);
}
/* Mirrors requireScopeStateType(). */
zv::Val requireScopeStateType(zval *expr, zval *scope)
{
zv::Val type = thisFindScopeStateType(expr, scope);
if (UNEXPECTED(type.isUndef())) return zv::Val();
if (type.isNull()) {
throwWithNodeLine(Z_OBJ_P(expr), "%s on line " ZEND_LONG_FMT " is not tracked on the scope it was pinned as tracked on.");
return zv::Val();
}
return type;
}
/* Mirrors processSyntheticOnDemand(). */
zv::Val processSyntheticOnDemand(zval *expr, zval *scope)
{
if (UNEXPECTED(!guardAgainstUnprocessedRealNode(Z_OBJ_P(expr), "processSyntheticOnDemand"))) return zv::Val();
zv::Val current = pt_mutating_scope_get_current_expression_result_storage(Z_OBJ_P(scope));
if (UNEXPECTED(current.isUndef())) return zv::Val();
if (current.isNull()) {
current = pt_expression_result_storage_new();
if (UNEXPECTED(current.isUndef())) return zv::Val();
}
zv::Val duplicate = pt_expression_result_storage_duplicate(current.raw());
if (UNEXPECTED(duplicate.isUndef())) return zv::Val();
return thisProcessExprOnDemand(expr, scope, duplicate.raw());
}
/* Mirrors processExprNode(). */
zv::Val processExprNode(zval *stmt, zval *expr, zval *scope, zval *storage, zval *nodeCallback, zval *context)
{
zv::Val result;
pt_engine_with_stack([&]() { result = processExprNodeBody(stmt, expr, scope, storage, nodeCallback, context); });
return result;
}
/* public (twin 822): a property read defaults to pure - only a hook we
* are certain about and that is certainly side-effecting makes the read
* impure */
zv::Val getImpurePointsFromPropertyHook(zval *scope, zval *propertyFetch, zval *propertyReflection, zend_string *hookName)
{
bool backingValueAccess;
if (UNEXPECTED(!isPropertyHookBackingValueAccess(Z_OBJ_P(scope), Z_OBJ_P(propertyFetch), backingValueAccess))) return zv::Val();
if (backingValueAccess) return zv::Val(zv::Arr::empty());
zend_object *property = Z_OBJ_P(propertyReflection);
zval hookNameZv;
ZVAL_STR(&hookNameZv, hookName);
zv::Val hasHook = pt_type_call(property, PT_LC("hashook"), 1, &hookNameZv);
if (UNEXPECTED(hasHook.isUndef())) return zv::Val();
if (!zend_is_true(hasHook.raw())) return zv::Val(zv::Arr::empty());
zv::Val hook = pt_type_call(property, PT_LC("gethook"), 1, &hookNameZv);
if (UNEXPECTED(hook.isUndef())) return zv::Val();
if (UNEXPECTED(!hook.ref().isObject())) {
zend_throw_error(NULL, "Call to a member function hasSideEffects() on %s", zend_zval_value_name(hook.raw()));
return zv::Val();
}
zv::Val sideEffects = pt_type_call(hook.ref().asObject(), PT_LC("hassideeffects"), 0, NULL);
if (UNEXPECTED(sideEffects.isUndef())) return zv::Val();
if (pt_type_trinary_value(sideEffects.raw()) != PT_TRI_YES) return zv::Val(zv::Arr::empty());
zv::Val declaringClass = pt_type_call(property, PT_LC("getdeclaringclass"), 0, NULL);
if (UNEXPECTED(declaringClass.isUndef())) return zv::Val();
if (UNEXPECTED(!declaringClass.ref().isObject())) {
zend_throw_error(NULL, "Call to a member function getDisplayName() on %s", zend_zval_value_name(declaringClass.raw()));
return zv::Val();
}
zv::Val displayName = pt_type_call(declaringClass.ref().asObject(), PT_LC("getdisplayname"), 0, NULL);
if (UNEXPECTED(displayName.isUndef())) return zv::Val();
zv::Val propertyName = pt_type_call(property, PT_LC("getname"), 0, NULL);
if (UNEXPECTED(propertyName.isUndef())) return zv::Val();
zend_string *displayNameString = zval_get_string(displayName.raw());
zend_string *propertyNameString = zval_get_string(propertyName.raw());
zend_string *description = zend_strpprintf(0, "call to %s hook of property %s::$%s", ZSTR_VAL(hookName), ZSTR_VAL(displayNameString), ZSTR_VAL(propertyNameString));
zend_string_release(displayNameString);
zend_string_release(propertyNameString);
zend_string *identifier = zend_string_init(PT_LC("propertyHookCall"), 0);
zv::Val point = pt_impure_point_new(scope, propertyFetch, identifier, description, true);
zend_string_release(identifier);
zend_string_release(description);
if (UNEXPECTED(point.isUndef())) return zv::Val();
zv::Arr points = zv::Arr::create(1);
points.push(std::move(point));
return zv::Val(std::move(points));
}
/* private (twin 861): inside a hook of the same property, $this->prop is
* the backing value, not a re-entrant hook call; false = pending
* exception */
[[nodiscard]] bool isPropertyHookBackingValueAccess(zend_object *scope, zend_object *propertyFetch, bool &out)
{
out = false;
zv::Val function = pt_mutating_scope_get_function(scope);
if (UNEXPECTED(function.isUndef())) return false;
bool isParserNodeMethod;
if (UNEXPECTED(!isInstanceValue(function.raw(), PT_CLASS_PHP_METHOD_FROM_PARSER_NODE_REFLECTION, isParserNodeMethod))) return false;
if (!isParserNodeMethod) return true;
zend_object *functionObject = function.ref().asObject();
zv::Val isPropertyHook = pt_type_call(functionObject, PT_LC("ispropertyhook"), 0, NULL);
if (UNEXPECTED(isPropertyHook.isUndef())) return false;
if (!zend_is_true(isPropertyHook.raw())) return true;
zval *var = nodeProperty(pt_nsr_hook_fetch_var_site, propertyFetch, PT_LC("var"));
if (UNEXPECTED(var == NULL)) return false;
bool isVariable;
if (UNEXPECTED(!isInstanceValue(var, PT_CLASS_VARIABLE, isVariable))) return false;
if (!isVariable) return true;
zval *varName = nodeProperty(pt_nsr_hook_this_name_site, Z_OBJ_P(var), PT_LC("name"));
if (UNEXPECTED(varName == NULL)) return false;
if (Z_TYPE_P(varName) != IS_STRING || !zend_string_equals_literal(Z_STR_P(varName), "this")) return true;
zval *name = nodeProperty(pt_nsr_hook_fetch_name_site, propertyFetch, PT_LC("name"));
if (UNEXPECTED(name == NULL)) return false;
bool isIdentifier;
if (UNEXPECTED(!isInstanceValue(name, PT_CLASS_IDENTIFIER, isIdentifier))) return false;
if (!isIdentifier) return true;
zv::Val nameString = pt_type_call(Z_OBJ_P(name), PT_LC("tostring"), 0, NULL);
if (UNEXPECTED(nameString.isUndef())) return false;
zv::Val hookedPropertyName = pt_type_call(functionObject, PT_LC("gethookedpropertyname"), 0, NULL);
if (UNEXPECTED(hookedPropertyName.isUndef())) return false;