Repository navigation
Expand file tree
/
Copy pathTemplateArgumentFrame.cpp
More file actions
960 lines (866 loc) · 42.8 KB
/
Copy pathTemplateArgumentFrame.cpp
File metadata and controls
960 lines (866 loc) · 42.8 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
/*
* PHPStanTurbo\TemplateArgumentFrame — native implementation of
* PHPStan\Analyser\Generics\TemplateArgumentFrame.
*
* The template inference context a scope carries: isObserving() is asked
* ~650K times per self-analysis and returnTypeOfCall() ~145K times (by the
* call handlers and the native MutatingScope). State lives in the twin's
* three readonly promoted property slots, in its order. returnTypeOfCall()
* asks the scope through MutatingScope's direct entries and the acceptor
* (a PHP ResolvedFunctionVariant) by name; resolveUnconstrained()'s
* traversal callback and resolveOrUnconstrained()'s resolver are native
* callback holders (TypeTraits.h).
*/
#include "support.h"
#include "generated/TemplateArgumentFrame.h"
namespace slots = ptdecl::TemplateArgumentFrame::slot;
namespace sigs = ptdecl::TemplateArgumentFrame::sig;
#include "zv.h"
#include "TypeTraits.h"
#include "TypeOps.h"
#include "AcceptorValues.h"
#include "zend_smart_str.h"
zend_class_entry *pt_ce_template_argument_frame = nullptr;
namespace {
/* the twin's SYNTHETIC_SITE_ATTRIBUTE / ORIGINAL_SITE_ATTRIBUTE values */
constexpr const char *pt_taf_synthetic_site = "templateArgumentSyntheticSite";
constexpr const char *pt_taf_original_site = "templateArgumentOriginalSite";
/* ORIGINAL_SITE_ATTRIBUTE as a permanent interned string */
zend_string *pt_taf_original_site_str = nullptr;
/* getByRefSiteMode()'s results, permanent interned strings */
zend_string *pt_taf_local = nullptr;
zend_string *pt_taf_escaped = nullptr;
/* $object->method() by name; UNDEF = pending exception */
zv::Val call0(zval *object, const char *lcname, size_t len, const char *method)
{
if (UNEXPECTED(Z_TYPE_P(object) != IS_OBJECT)) {
zend_throw_error(NULL, "Call to a member function %s() on %s", method, zend_zval_value_name(object));
return zv::Val();
}
return pt_type_call(Z_OBJ_P(object), lcname, len, 0, NULL);
}
void resolveUnconstrainedCallback(zval *site, zval *captured, uint32_t argc, zval *argv, zval *return_value);
void resolveCallback(zval *frame, zval *state1, uint32_t argc, zval *argv, zval *return_value);
void resolveMarkersCallback(zval *frame, zval *state1, uint32_t argc, zval *argv, zval *return_value);
} // namespace
namespace phpstanturbo {
/* Mirrors PHPStan\Analyser\Generics\TemplateArgumentFrame. */
class TemplateArgumentFrame
{
public:
explicit TemplateArgumentFrame(zend_object *self) : self(self) {}
/* Mirrors returnTypeOfCall(); $allowUnresolved -1 for null */
static zv::Val returnTypeOfCall(zval *acceptor, zval *scope, zval *site, int allowUnresolved)
{
zv::Val frame = pt_mutating_scope_get_current_template_argument_frame(Z_OBJ_P(scope));
if (UNEXPECTED(frame.isUndef())) return zv::Val();
zend_class_entry *resolvedFunctionVariantCe = Z_TYPE_P(frame.raw()) == IS_NULL ? NULL : pt_class_loaded(PT_CLASS_RESOLVED_FUNCTION_VARIANT);
if (UNEXPECTED(EG(exception))) return zv::Val();
if (resolvedFunctionVariantCe == NULL || !instanceof_function(Z_OBJCE_P(acceptor), resolvedFunctionVariantCe)) {
return pt_parameters_acceptor_call(acceptor, PT_PA_GET_RETURN_TYPE);
}
zval *originalSite = pt_node_attribute(Z_OBJ_P(site), pt_taf_original_site_str);
zend_class_entry *exprCe = pt_class(PT_CLASS_EXPR);
if (UNEXPECTED(exprCe == NULL)) return zv::Val();
zval *callSite = originalSite != NULL && Z_TYPE_P(originalSite) == IS_OBJECT && instanceof_function(Z_OBJCE_P(originalSite), exprCe) ? originalSite : site;
bool allow;
if (allowUnresolved >= 0) {
allow = allowUnresolved != 0;
} else {
bool nativeTypesPromoted;
if (UNEXPECTED(!pt_mutating_scope_native_types_promoted(Z_OBJ_P(scope), nativeTypesPromoted))) return zv::Val();
allow = !nativeTypesPromoted;
}
return pt_resolved_function_variant_get_return_type_with_unresolved_template_arguments(acceptor, callSite, frame.raw(), allow);
}
/* __construct(private readonly ?self $parent, private readonly ?array
* $resolutions = null, private readonly array $siteStatementIndexes =
* [], private readonly ?Node $closureSignatureBody = null, private
* readonly array $closureSignatureStmts = [], private readonly array
* $settledClosureSites = [], private readonly bool $observingClosures =
* false, private readonly array $byRefSites = [], private readonly array
* $staticVariableTypes = [], private readonly array
* $staticVariableConditionalExpressions = []); NULL for null / []. false =
* pending exception (a repeated construction modifies readonly properties) */
[[nodiscard]] bool construct(zval *parent, zval *resolutions, zval *siteStatementIndexes, zval *closureSignatureBody, zval *closureSignatureStmts, zval *settledClosureSites, bool observingClosures, zval *byRefSites, zval *staticVariableTypes, zval *staticVariableConditionalExpressions) const
{
if (UNEXPECTED(Z_TYPE_P(OBJ_PROP_NUM(self, slots::parent)) != IS_UNDEF)) {
zend_throw_error(NULL, "Cannot modify readonly property %s::$parent", ZSTR_VAL(self->ce->name));
return false;
}
zval value = {};
if (parent != NULL) {
pt_write_slot(self, slots::parent, parent);
} else {
ZVAL_NULL(&value);
pt_write_slot(self, slots::parent, &value);
}
if (resolutions != NULL) {
pt_write_slot(self, slots::resolutions, resolutions);
} else {
ZVAL_NULL(&value);
pt_write_slot(self, slots::resolutions, &value);
}
if (siteStatementIndexes != NULL) {
pt_write_slot(self, slots::siteStatementIndexes, siteStatementIndexes);
} else {
ZVAL_EMPTY_ARRAY(&value);
pt_write_slot(self, slots::siteStatementIndexes, &value);
}
if (closureSignatureBody != NULL) {
pt_write_slot(self, slots::closureSignatureBody, closureSignatureBody);
} else {
ZVAL_NULL(&value);
pt_write_slot(self, slots::closureSignatureBody, &value);
}
if (closureSignatureStmts != NULL) {
pt_write_slot(self, slots::closureSignatureStmts, closureSignatureStmts);
} else {
ZVAL_EMPTY_ARRAY(&value);
pt_write_slot(self, slots::closureSignatureStmts, &value);
}
if (settledClosureSites != NULL) {
pt_write_slot(self, slots::settledClosureSites, settledClosureSites);
} else {
ZVAL_EMPTY_ARRAY(&value);
pt_write_slot(self, slots::settledClosureSites, &value);
}
ZVAL_BOOL(&value, observingClosures);
pt_write_slot(self, slots::observingClosures, &value);
if (byRefSites != NULL) {
pt_write_slot(self, slots::byRefSites, byRefSites);
} else {
ZVAL_EMPTY_ARRAY(&value);
pt_write_slot(self, slots::byRefSites, &value);
}
if (staticVariableTypes != NULL) {
pt_write_slot(self, slots::staticVariableTypes, staticVariableTypes);
} else {
ZVAL_EMPTY_ARRAY(&value);
pt_write_slot(self, slots::staticVariableTypes, &value);
}
if (staticVariableConditionalExpressions != NULL) {
pt_write_slot(self, slots::staticVariableConditionalExpressions, staticVariableConditionalExpressions);
} else {
ZVAL_EMPTY_ARRAY(&value);
pt_write_slot(self, slots::staticVariableConditionalExpressions, &value);
}
return true;
}
/* Mirrors getStaticVariableTypes(): [phpdoc, native] or PHP null */
zv::Val getStaticVariableTypes(zval *var) const
{
zval *types = pt_typed_slot(self, slots::staticVariableTypes, self->ce, "staticVariableTypes");
if (UNEXPECTED(types == NULL)) return zv::Val();
zval *found = zend_hash_index_find(Z_ARRVAL_P(types), Z_OBJ_HANDLE_P(var));
if (found == NULL || Z_TYPE_P(found) != IS_ARRAY) return zv::Val::null();
zval *node = zend_hash_index_find(Z_ARRVAL_P(found), 0);
if (node == NULL || Z_TYPE_P(node) != IS_OBJECT || Z_OBJ_P(node) != Z_OBJ_P(var)) return zv::Val::null();
zval *type = zend_hash_index_find(Z_ARRVAL_P(found), 1);
zval *nativeType = zend_hash_index_find(Z_ARRVAL_P(found), 2);
if (UNEXPECTED(type == NULL || nativeType == NULL)) return zv::Val::null();
zv::Arr pair = zv::Arr::create(2);
pair.push(zv::Ref(type));
pair.push(zv::Ref(nativeType));
return zv::Val(std::move(pair));
}
/* Mirrors getStaticVariableConditionalExpressions(): the holders by
* expression string, or [] */
zv::Val getStaticVariableConditionalExpressions(zval *stmt) const
{
zval *all = pt_typed_slot(self, slots::staticVariableConditionalExpressions, self->ce, "staticVariableConditionalExpressions");
if (UNEXPECTED(all == NULL)) return zv::Val();
zval *found = zend_hash_index_find(Z_ARRVAL_P(all), Z_OBJ_HANDLE_P(stmt));
if (found == NULL || Z_TYPE_P(found) != IS_ARRAY) return zv::Val(zv::Arr::empty());
zval *node = zend_hash_index_find(Z_ARRVAL_P(found), 0);
if (node == NULL || Z_TYPE_P(node) != IS_OBJECT || Z_OBJ_P(node) != Z_OBJ_P(stmt)) return zv::Val(zv::Arr::empty());
zval *conditionalExpressions = zend_hash_index_find(Z_ARRVAL_P(found), 1);
if (UNEXPECTED(conditionalExpressions == NULL)) return zv::Val(zv::Arr::empty());
return zv::Val::copyOf(zv::Ref(conditionalExpressions));
}
/* Mirrors withStaticVariableTypes(); UNDEF = pending exception */
zv::Val withStaticVariableTypes(zval *staticVariableTypes, zval *statementIndexes, zval *conditionalExpressions) const
{
zval *parent = pt_typed_slot(self, slots::parent, self->ce, "parent");
if (UNEXPECTED(parent == NULL)) return zv::Val();
zval *resolutions = pt_typed_slot(self, slots::resolutions, self->ce, "resolutions");
if (UNEXPECTED(resolutions == NULL)) return zv::Val();
zval *indexes = pt_typed_slot(self, slots::siteStatementIndexes, self->ce, "siteStatementIndexes");
if (UNEXPECTED(indexes == NULL)) return zv::Val();
zv::Val resolvedStaticVariableTypes;
if (Z_TYPE_P(resolutions) != IS_NULL) {
resolvedStaticVariableTypes = resolveStaticVariableTypeMarkers(staticVariableTypes);
if (UNEXPECTED(resolvedStaticVariableTypes.isUndef())) return zv::Val();
staticVariableTypes = resolvedStaticVariableTypes.raw();
}
zv::Val mergedIndexes = arrayUnion(Z_ARRVAL_P(indexes), Z_ARRVAL_P(statementIndexes));
zval *body = OBJ_PROP_NUM(self, slots::closureSignatureBody);
return create(
Z_TYPE_P(parent) == IS_NULL ? NULL : parent,
Z_TYPE_P(resolutions) == IS_NULL ? NULL : resolutions,
mergedIndexes.raw(),
Z_TYPE_P(body) == IS_NULL ? NULL : body,
OBJ_PROP_NUM(self, slots::closureSignatureStmts),
OBJ_PROP_NUM(self, slots::settledClosureSites),
Z_TYPE_P(OBJ_PROP_NUM(self, slots::observingClosures)) == IS_TRUE,
OBJ_PROP_NUM(self, slots::byRefSites),
staticVariableTypes,
conditionalExpressions
);
}
/* withStaticVariableTypes()'s loop on a resolved frame: every entry
* [$var, $type, $nativeType] with both types through resolveMarkers(),
* under its key; UNDEF = pending exception */
zv::Val resolveStaticVariableTypeMarkers(zval *staticVariableTypes) const
{
zv::Arr result = zv::Arr::create(zend_hash_num_elements(Z_ARRVAL_P(staticVariableTypes)));
for (auto entry : zv::ArrRef(staticVariableTypes)) {
zval *types = entry.value().deref().raw();
zval *var = Z_TYPE_P(types) == IS_ARRAY ? zend_hash_index_find(Z_ARRVAL_P(types), 0) : NULL;
zval *type = Z_TYPE_P(types) == IS_ARRAY ? zend_hash_index_find(Z_ARRVAL_P(types), 1) : NULL;
zval *nativeType = Z_TYPE_P(types) == IS_ARRAY ? zend_hash_index_find(Z_ARRVAL_P(types), 2) : NULL;
if (UNEXPECTED(var == NULL || type == NULL || nativeType == NULL)) {
zend_throw_error(NULL, "TemplateArgumentFrame::withStaticVariableTypes(): an entry must be [$var, $type, $nativeType]");
return zv::Val();
}
zv::Val resolvedType = resolveMarkers(type);
if (UNEXPECTED(resolvedType.isUndef())) return zv::Val();
zv::Val resolvedNativeType = resolveMarkers(nativeType);
if (UNEXPECTED(resolvedNativeType.isUndef())) return zv::Val();
zv::Arr triple = zv::Arr::create(3);
triple.push(zv::Ref(var));
triple.push(std::move(resolvedType));
triple.push(std::move(resolvedNativeType));
zend_string *key = entry.stringKeyOrNull();
if (key != NULL) {
result.set(key, zv::Val(std::move(triple)));
} else {
zval tripleZv = zv::Val(std::move(triple)).take();
zend_hash_index_update(result.table(), entry.indexKey(), &tripleZv);
}
}
return zv::Val(std::move(result));
}
/* Mirrors resolveMarkers(); UNDEF = pending exception */
zv::Val resolveMarkers(zval *type) const
{
zval selfZv;
ZVAL_OBJ(&selfZv, self);
zv::Val callback = pt_type_native_callback(resolveMarkersCallback, &selfZv, NULL);
if (UNEXPECTED(callback.isUndef())) return zv::Val();
return pt_type_traverser_map_of(type, callback.raw());
}
/* Mirrors getByRefSiteMode(): the entry of this frame's byRefSites for
* spl_object_id($site) holding that very node, then the parent's; the
* interned 'local' / 'escaped', or PHP null. UNDEF = pending exception */
zv::Val getByRefSiteMode(zval *site) const
{
zend_ulong id = Z_OBJ_HANDLE_P(site);
zend_object *frame = self;
for (;;) {
zval *sites = OBJ_PROP_NUM(frame, slots::byRefSites);
zval *found = Z_TYPE_P(sites) == IS_ARRAY ? zend_hash_index_find(Z_ARRVAL_P(sites), id) : NULL;
if (found != NULL && Z_TYPE_P(found) == IS_ARRAY) {
zval *node = zend_hash_index_find(Z_ARRVAL_P(found), 0);
if (node != NULL && Z_TYPE_P(node) == IS_OBJECT && Z_OBJ_P(node) == Z_OBJ_P(site)) {
zval *local = zend_hash_index_find(Z_ARRVAL_P(found), 2);
return zv::Val::string(local != NULL && zend_is_true(local) ? pt_taf_local : pt_taf_escaped);
}
}
zval *parent = pt_typed_slot(frame, slots::parent, frame->ce, "parent");
if (UNEXPECTED(parent == NULL)) return zv::Val();
if (Z_TYPE_P(parent) == IS_NULL) return zv::Val::null();
if (UNEXPECTED(Z_OBJCE_P(parent) != frame->ce)) return pt_type_call(Z_OBJ_P(parent), PT_LC("getbyrefsitemode"), 1, site);
frame = Z_OBJ_P(parent);
}
}
/* Mirrors getLocalByRefSites(): [$site, $statementIndex] of every local
* entry, in order */
zv::Val getLocalByRefSites() const
{
zval *sites = pt_typed_slot(self, slots::byRefSites, self->ce, "byRefSites");
if (UNEXPECTED(sites == NULL)) return zv::Val();
zv::Arr result = zv::Arr::empty();
for (auto entry : zv::ArrRef(sites)) {
zval *byRefSite = entry.value().deref().raw();
if (UNEXPECTED(Z_TYPE_P(byRefSite) != IS_ARRAY)) continue;
zval *site = zend_hash_index_find(Z_ARRVAL_P(byRefSite), 0);
zval *statementIndex = zend_hash_index_find(Z_ARRVAL_P(byRefSite), 1);
zval *local = zend_hash_index_find(Z_ARRVAL_P(byRefSite), 2);
if (local == NULL || !zend_is_true(local) || site == NULL || statementIndex == NULL) continue;
zv::Arr pair = zv::Arr::create(2);
pair.push(zv::Ref(site));
pair.push(zv::Ref(statementIndex));
result.push(std::move(pair));
}
return zv::Val(std::move(result));
}
/* Mirrors withObservedClosures(): new self($this->parent,
* ($this->resolutions ?? []) + $closureResolutions,
* $this->siteStatementIndexes + $closureSiteStatementIndexes, the body,
* its statements, $settledClosureSites, byRefSites: $byRefSites); UNDEF =
* pending exception */
zv::Val withObservedClosures(zval *closureResolutions, zval *closureSiteStatementIndexes, zval *settledClosureSites, zval *byRefSites) const
{
zval *parent = pt_typed_slot(self, slots::parent, self->ce, "parent");
if (UNEXPECTED(parent == NULL)) return zv::Val();
zval *resolutions = pt_typed_slot(self, slots::resolutions, self->ce, "resolutions");
if (UNEXPECTED(resolutions == NULL)) return zv::Val();
zval *indexes = pt_typed_slot(self, slots::siteStatementIndexes, self->ce, "siteStatementIndexes");
if (UNEXPECTED(indexes == NULL)) return zv::Val();
zv::Val mergedResolutions = arrayUnion(Z_TYPE_P(resolutions) == IS_ARRAY ? Z_ARRVAL_P(resolutions) : NULL, Z_ARRVAL_P(closureResolutions));
zv::Val mergedIndexes = arrayUnion(Z_ARRVAL_P(indexes), Z_ARRVAL_P(closureSiteStatementIndexes));
zval *body = OBJ_PROP_NUM(self, slots::closureSignatureBody);
return create(
Z_TYPE_P(parent) == IS_NULL ? NULL : parent,
mergedResolutions.raw(),
mergedIndexes.raw(),
Z_TYPE_P(body) == IS_NULL ? NULL : body,
OBJ_PROP_NUM(self, slots::closureSignatureStmts),
settledClosureSites,
false,
byRefSites,
OBJ_PROP_NUM(self, slots::staticVariableTypes),
OBJ_PROP_NUM(self, slots::staticVariableConditionalExpressions)
);
}
/* Mirrors isSettledClosureSite(): isset($this->settledClosureSites[
* spl_object_id($site)]), then the parent; false = pending exception */
[[nodiscard]] bool isSettledClosureSite(zval *site, bool &out) const
{
zend_ulong id = Z_OBJ_HANDLE_P(site);
zend_object *frame = self;
for (;;) {
zval *settled = OBJ_PROP_NUM(frame, slots::settledClosureSites);
if (Z_TYPE_P(settled) == IS_ARRAY) {
zval *found = zend_hash_index_find(Z_ARRVAL_P(settled), id);
if (found != NULL && Z_TYPE_P(found) != IS_NULL) {
out = true;
return true;
}
}
zval *parent = pt_typed_slot(frame, slots::parent, frame->ce, "parent");
if (UNEXPECTED(parent == NULL)) return false;
if (Z_TYPE_P(parent) == IS_NULL) {
out = false;
return true;
}
if (UNEXPECTED(Z_OBJCE_P(parent) != frame->ce)) {
zv::Val result = pt_type_call(Z_OBJ_P(parent), PT_LC("issettledclosuresite"), 1, site);
if (UNEXPECTED(result.isUndef())) return false;
out = zend_is_true(result.raw());
return true;
}
frame = Z_OBJ_P(parent);
}
}
/* Mirrors getParent(): the slot (borrowed) */
zval *parent() const { return OBJ_PROP_NUM(self, slots::parent); }
/* Mirrors getClosureSignatureBody() / getClosureSignatureStmts(): the
* slots (borrowed) */
zval *closureSignatureBody() const { return OBJ_PROP_NUM(self, slots::closureSignatureBody); }
zval *closureSignatureStmts() const { return OBJ_PROP_NUM(self, slots::closureSignatureStmts); }
/* new self(...); UNDEF = pending exception */
static zv::Val create(zval *parent, zval *resolutions, zval *siteStatementIndexes, zval *closureSignatureBody, zval *closureSignatureStmts, zval *settledClosureSites, bool observingClosures, zval *byRefSites, zval *staticVariableTypes = NULL, zval *staticVariableConditionalExpressions = NULL)
{
zval object;
if (UNEXPECTED(object_init_ex(&object, pt_ce_template_argument_frame) != SUCCESS)) return zv::Val();
zv::Val frame = zv::Val::adopt(object);
if (UNEXPECTED(!TemplateArgumentFrame(Z_OBJ_P(frame.raw())).construct(parent, resolutions, siteStatementIndexes, closureSignatureBody, closureSignatureStmts, settledClosureSites, observingClosures, byRefSites, staticVariableTypes, staticVariableConditionalExpressions))) return zv::Val();
return frame;
}
/* Mirrors isObserving(); false = pending exception */
[[nodiscard]] bool isObserving(bool &out) const
{
zval *resolutions = pt_typed_slot(self, slots::resolutions, self->ce, "resolutions");
if (UNEXPECTED(resolutions == NULL)) return false;
out = Z_TYPE_P(resolutions) == IS_NULL;
return true;
}
/* Mirrors isObservingClosures(); false = pending exception */
[[nodiscard]] bool isObservingClosures(bool &out) const
{
zval *resolutions = pt_typed_slot(self, slots::resolutions, self->ce, "resolutions");
if (UNEXPECTED(resolutions == NULL)) return false;
zval *observingClosures = pt_typed_slot(self, slots::observingClosures, self->ce, "observingClosures");
if (UNEXPECTED(observingClosures == NULL)) return false;
out = Z_TYPE_P(resolutions) == IS_NULL || Z_TYPE_P(observingClosures) == IS_TRUE;
return true;
}
/* Mirrors firstSiteStatementIndex(). */
zv::Val firstSiteStatementIndex() const
{
zval *indexes = pt_typed_slot(self, slots::siteStatementIndexes, self->ce, "siteStatementIndexes");
if (UNEXPECTED(indexes == NULL)) return zv::Val();
zval first;
ZVAL_UNDEF(&first);
for (auto entry : zv::TableRef(Z_ARRVAL_P(indexes))) {
zval index;
keyOf(entry, index);
if (Z_TYPE(first) != IS_UNDEF && zend_compare(&index, &first) >= 0) continue;
ZVAL_COPY_VALUE(&first, &index);
}
if (Z_TYPE(first) == IS_UNDEF) return zv::Val::null();
return zv::Val::copyOf(zv::Ref(&first));
}
/* Mirrors ownsSiteInStatement(): isset($this->siteStatementIndexes[$statementIndex]) */
bool ownsSiteInStatement(zend_long statementIndex, bool &out) const
{
zval *indexes = OBJ_PROP_NUM(self, slots::siteStatementIndexes);
zval *found = Z_TYPE_P(indexes) == IS_ARRAY ? zend_hash_index_find(Z_ARRVAL_P(indexes), (zend_ulong) statementIndex) : NULL;
out = found != NULL && Z_TYPE_P(found) != IS_NULL;
return true;
}
/* Mirrors hasSiteAtOrAfter(). */
bool hasSiteAtOrAfter(zend_long statementIndex, bool &out) const
{
zval *indexes = pt_typed_slot(self, slots::siteStatementIndexes, self->ce, "siteStatementIndexes");
if (UNEXPECTED(indexes == NULL)) return false;
zval threshold;
ZVAL_LONG(&threshold, statementIndex);
for (auto entry : zv::TableRef(Z_ARRVAL_P(indexes))) {
zval index;
keyOf(entry, index);
if (zend_compare(&index, &threshold) >= 0) {
out = true;
return true;
}
}
out = false;
return true;
}
/* Mirrors resolveOrUnconstrained(). */
zv::Val resolveOrUnconstrained(zval *site, zval *templateType) const
{
zv::Val name = call0(templateType, PT_LC("getname"), "getName");
if (UNEXPECTED(name.isUndef())) return zv::Val();
if (UNEXPECTED(Z_TYPE_P(name.raw()) != IS_STRING)) {
zend_type_error("%s::resolve(): Argument #2 ($templateName) must be of type string, %s given", ZSTR_VAL(self->ce->name), zend_zval_value_name(name.raw()));
return zv::Val();
}
zv::Val resolved = resolve(site, Z_STR_P(name.raw()));
if (UNEXPECTED(resolved.isUndef())) return zv::Val();
if (Z_TYPE_P(resolved.raw()) != IS_NULL) return resolved;
zval selfZv;
ZVAL_OBJ(&selfZv, self);
zv::Val resolver = pt_type_native_callback(resolveCallback, &selfZv, NULL);
if (UNEXPECTED(resolver.isUndef())) return zv::Val();
return resolveUnconstrained(site, templateType, resolver.raw());
}
/* Mirrors resolveUnconstrained(). */
static zv::Val resolveUnconstrained(zval *site, zval *templateType, zval *resolver)
{
zv::Val defaultType = call0(templateType, PT_LC("getdefault"), "getDefault");
if (UNEXPECTED(defaultType.isUndef())) return zv::Val();
if (Z_TYPE_P(defaultType.raw()) != IS_NULL) return defaultType;
zv::Val bound = call0(templateType, PT_LC("getbound"), "getBound");
if (UNEXPECTED(bound.isUndef())) return zv::Val();
if (UNEXPECTED(Z_TYPE_P(bound.raw()) != IS_OBJECT)) {
zend_throw_error(NULL, "Call to a member function hasTemplateOrLateResolvableType() on %s", zend_zval_value_name(bound.raw()));
return zv::Val();
}
zv::Val hasTemplate = pt_type_op(Z_OBJ_P(bound.raw()), PT_OP_HAS_TEMPLATE_OR_LATE_RESOLVABLE_TYPE, 0, NULL);
if (UNEXPECTED(hasTemplate.isUndef())) return zv::Val();
if (!zend_is_true(hasTemplate.raw())) return bound;
zv::Val scope = call0(templateType, PT_LC("getscope"), "getScope");
if (UNEXPECTED(scope.isUndef())) return zv::Val();
/* use ($site, $scope, $resolve): the site in the first state slot,
* [$scope, $resolve] in the second */
zv::Arr captured = zv::Arr::create(2);
captured.push(zv::Ref(scope.raw()));
captured.push(zv::Ref(resolver));
zv::Val callback = pt_type_native_callback(resolveUnconstrainedCallback, site, captured.raw());
if (UNEXPECTED(callback.isUndef())) return zv::Val();
return pt_type_traverser_map_of(bound.raw(), callback.raw());
}
/* Mirrors resolve(): the resolution of the key on this frame, then on the
* parents; PHP null for none. UNDEF = pending exception */
zv::Val resolve(zval *site, zend_string *templateName) const
{
smart_str key = {};
smart_str_append_long(&key, (zend_long) Z_OBJ_HANDLE_P(site));
smart_str_appendc(&key, '#');
smart_str_append(&key, templateName);
smart_str_0(&key);
zv::Str keyStr = zv::Str::adopt(key.s);
zend_object *frame = self;
for (;;) {
zval *resolutions = OBJ_PROP_NUM(frame, slots::resolutions);
if (Z_TYPE_P(resolutions) == IS_ARRAY) {
zval *found = zend_symtable_find(Z_ARRVAL_P(resolutions), keyStr.get());
if (found != NULL && Z_TYPE_P(found) != IS_NULL) return zv::Val::copyOf(zv::Ref(found));
}
zval *parent = pt_typed_slot(frame, slots::parent, frame->ce, "parent");
if (UNEXPECTED(parent == NULL)) return zv::Val();
if (Z_TYPE_P(parent) == IS_NULL) return zv::Val::null();
if (UNEXPECTED(Z_OBJCE_P(parent) != frame->ce)) {
zval siteZv, nameZv;
ZVAL_COPY_VALUE(&siteZv, site);
ZVAL_STR(&nameZv, templateName);
zv::Args argv{&siteZv, &nameZv};
return pt_type_call(Z_OBJ_P(parent), PT_LC("resolve"), 2, argv);
}
frame = Z_OBJ_P(parent);
}
}
/* Mirrors getResolutionCacheKeySuffix(). */
zv::Val getResolutionCacheKeySuffix() const
{
zend_object *frame = self;
for (;;) {
zval *resolutions = pt_typed_slot(frame, slots::resolutions, frame->ce, "resolutions");
if (UNEXPECTED(resolutions == NULL)) return zv::Val();
if (Z_TYPE_P(resolutions) != IS_NULL) {
return zv::Val::adoptString(zend_strpprintf(0, "|templateArguments:%u", frame->handle));
}
zval *parent = pt_typed_slot(frame, slots::parent, frame->ce, "parent");
if (UNEXPECTED(parent == NULL)) return zv::Val();
if (Z_TYPE_P(parent) == IS_NULL) return zv::Val::adoptString(ZSTR_EMPTY_ALLOC());
frame = Z_OBJ_P(parent);
}
}
private:
zend_object *self;
/* $left + $right (the left entry wins); $left NULL for [] */
static zv::Val arrayUnion(HashTable *left, HashTable *right)
{
zval result;
if (left == NULL) {
ZVAL_ARR(&result, zend_array_dup(right));
return zv::Val::adopt(result);
}
ZVAL_ARR(&result, zend_array_dup(left));
zend_hash_merge(Z_ARRVAL(result), right, zval_add_ref, 0);
return zv::Val::adopt(result);
}
/* an array entry's key as a zval (array_keys()) */
static void keyOf(const zv::ArrayEntry &entry, zval &out)
{
zend_string *key = entry.stringKeyOrNull();
if (key != NULL) {
ZVAL_STR(&out, key);
} else {
ZVAL_LONG(&out, (zend_long) entry.indexKey());
}
}
};
} // namespace phpstanturbo
using phpstanturbo::TemplateArgumentFrame;
namespace {
/* fn (Expr $site, string $templateName): ?Type => $this->resolve($site, $templateName) */
void resolveCallback(zval *frame, zval *state1, uint32_t argc, zval *argv, zval *return_value)
{
(void) state1;
if (UNEXPECTED(argc < 2 || Z_TYPE(argv[0]) != IS_OBJECT || Z_TYPE(argv[1]) != IS_STRING)) {
zend_type_error("TemplateArgumentFrame resolver: expected (Expr $site, string $templateName)");
return;
}
zv::Val resolved = TemplateArgumentFrame(Z_OBJ_P(frame)).resolve(&argv[0], Z_STR(argv[1]));
if (UNEXPECTED(resolved.isUndef())) return;
resolved.intoReturnValue(return_value);
}
/* function (Type $type, callable $traverse): Type of resolveMarkers() — the
* frame in the first state slot */
void resolveMarkersCallback(zval *frame, zval *state1, uint32_t argc, zval *argv, zval *return_value)
{
(void) state1;
if (UNEXPECTED(argc < 2 || Z_TYPE(argv[0]) != IS_OBJECT)) {
zend_type_error("TemplateArgumentFrame traversal: expected (Type $type, callable $traverse)");
return;
}
zval *type = &argv[0];
zval *traverse = &argv[1];
if (instanceof_function(Z_OBJCE_P(type), pt_ce_unresolved_template_argument_type)) {
bool closureSignature = false;
if (UNEXPECTED(!pt_unresolved_template_argument_type_is_closure_signature(type, closureSignature))) return;
if (!closureSignature) {
/* $traverse($this->resolve($site, $type->getTemplateName()) ?? $type->getInitialType() ?? $this->resolveOrUnconstrained($site, $type->getTemplate())) */
zval *site = pt_unresolved_template_argument_type_site(type);
if (UNEXPECTED(site == NULL)) return;
zv::Val siteHold = zv::Val::copyOf(zv::Ref(site));
zv::Val name = pt_unresolved_template_argument_type_get_template_name(type);
if (UNEXPECTED(name.isUndef())) return;
if (UNEXPECTED(Z_TYPE_P(name.raw()) != IS_STRING)) {
zend_type_error("%s::resolve(): Argument #2 ($templateName) must be of type string, %s given", ZSTR_VAL(Z_OBJCE_P(frame)->name), zend_zval_value_name(name.raw()));
return;
}
zv::Val replacement = TemplateArgumentFrame(Z_OBJ_P(frame)).resolve(siteHold.raw(), Z_STR_P(name.raw()));
if (UNEXPECTED(replacement.isUndef())) return;
if (Z_TYPE_P(replacement.raw()) == IS_NULL) {
replacement = pt_type_call(Z_OBJ_P(type), PT_LC("getinitialtype"), 0, NULL);
if (UNEXPECTED(replacement.isUndef())) return;
}
if (Z_TYPE_P(replacement.raw()) == IS_NULL) {
zv::Val templateType = pt_type_call(Z_OBJ_P(type), PT_LC("gettemplate"), 0, NULL);
if (UNEXPECTED(templateType.isUndef())) return;
replacement = TemplateArgumentFrame(Z_OBJ_P(frame)).resolveOrUnconstrained(siteHold.raw(), templateType.raw());
if (UNEXPECTED(replacement.isUndef())) return;
}
zv::Val traversed = pt_type_call_callable(traverse, 1, replacement.raw());
if (UNEXPECTED(traversed.isUndef())) return;
traversed.intoReturnValue(return_value);
return;
}
}
zv::Val traversed = pt_type_call_callable(traverse, 1, type);
if (UNEXPECTED(traversed.isUndef())) return;
traversed.intoReturnValue(return_value);
}
/* static function (Type $type, callable $traverse) use ($site, $scope, $resolve): Type
* — $site in the first state slot, [$scope, $resolve] in the second */
void resolveUnconstrainedCallback(zval *site, zval *captured, uint32_t argc, zval *argv, zval *return_value)
{
if (UNEXPECTED(argc < 2 || Z_TYPE(argv[0]) != IS_OBJECT)) {
zend_type_error("TemplateArgumentFrame traversal: expected (Type $type, callable $traverse)");
return;
}
zval *type = &argv[0];
zval *traverse = &argv[1];
zval *scope = zend_hash_index_find(Z_ARRVAL_P(captured), 0);
zval *resolver = zend_hash_index_find(Z_ARRVAL_P(captured), 1);
zend_class_entry *templateTypeCe = pt_class_loaded(PT_CLASS_TEMPLATE_TYPE);
if (UNEXPECTED(EG(exception))) return;
if (templateTypeCe != NULL && instanceof_function(Z_OBJCE_P(type), templateTypeCe)) {
zv::Val typeScope = pt_type_call(Z_OBJ_P(type), PT_LC("getscope"), 0, NULL);
if (UNEXPECTED(typeScope.isUndef())) return;
if (UNEXPECTED(Z_TYPE_P(typeScope.raw()) != IS_OBJECT)) {
zend_throw_error(NULL, "Call to a member function equals() on %s", zend_zval_value_name(typeScope.raw()));
return;
}
zv::Val sameScope = pt_type_call(Z_OBJ_P(typeScope.raw()), PT_LC("equals"), 1, scope);
if (UNEXPECTED(sameScope.isUndef())) return;
if (zend_is_true(sameScope.raw())) {
/* $resolve($site, $type->getName()) ?? $type->getDefault() ?? $traverse($type->getBound()) */
zv::Val name = pt_type_call(Z_OBJ_P(type), PT_LC("getname"), 0, NULL);
if (UNEXPECTED(name.isUndef())) return;
zv::Args resolveArgv{site, name.raw()};
zv::Val resolved = pt_type_call_callable(resolver, 2, resolveArgv);
if (UNEXPECTED(resolved.isUndef())) return;
if (Z_TYPE_P(resolved.raw()) != IS_NULL) {
resolved.intoReturnValue(return_value);
return;
}
zv::Val defaultType = pt_type_call(Z_OBJ_P(type), PT_LC("getdefault"), 0, NULL);
if (UNEXPECTED(defaultType.isUndef())) return;
if (Z_TYPE_P(defaultType.raw()) != IS_NULL) {
defaultType.intoReturnValue(return_value);
return;
}
zv::Val bound = pt_type_call(Z_OBJ_P(type), PT_LC("getbound"), 0, NULL);
if (UNEXPECTED(bound.isUndef())) return;
zv::Val traversed = pt_type_call_callable(traverse, 1, bound.raw());
if (UNEXPECTED(traversed.isUndef())) return;
traversed.intoReturnValue(return_value);
return;
}
}
zv::Val traversed = pt_type_call_callable(traverse, 1, type);
if (UNEXPECTED(traversed.isUndef())) return;
traversed.intoReturnValue(return_value);
}
} // namespace
/* {{{ exported helpers: the shadowing class for native callers */
zv::Val pt_template_argument_frame_return_type_of_call(zval *acceptor, zval *scope, zval *site, int allowUnresolved)
{
return TemplateArgumentFrame::returnTypeOfCall(acceptor, scope, site, allowUnresolved);
}
zv::Val pt_template_argument_frame_get_parent(zval *frame)
{
if (EXPECTED(Z_OBJCE_P(frame) == pt_ce_template_argument_frame)) return zv::Val::copyOf(zv::Ref(TemplateArgumentFrame(Z_OBJ_P(frame)).parent()));
return pt_type_call(Z_OBJ_P(frame), PT_LC("getparent"), 0, NULL);
}
zv::Val pt_template_argument_frame_get_closure_signature_body(zval *frame)
{
if (EXPECTED(Z_OBJCE_P(frame) == pt_ce_template_argument_frame)) return zv::Val::copyOf(zv::Ref(TemplateArgumentFrame(Z_OBJ_P(frame)).closureSignatureBody()));
return pt_type_call(Z_OBJ_P(frame), PT_LC("getclosuresignaturebody"), 0, NULL);
}
zv::Val pt_template_argument_frame_get_closure_signature_stmts(zval *frame)
{
if (EXPECTED(Z_OBJCE_P(frame) == pt_ce_template_argument_frame)) return zv::Val::copyOf(zv::Ref(TemplateArgumentFrame(Z_OBJ_P(frame)).closureSignatureStmts()));
return pt_type_call(Z_OBJ_P(frame), PT_LC("getclosuresignaturestmts"), 0, NULL);
}
bool pt_template_argument_frame_is_settled_closure_site(zval *frame, zval *site, bool &out)
{
if (EXPECTED(Z_OBJCE_P(frame) == pt_ce_template_argument_frame)) return TemplateArgumentFrame(Z_OBJ_P(frame)).isSettledClosureSite(site, out);
zv::Val result = pt_type_call(Z_OBJ_P(frame), PT_LC("issettledclosuresite"), 1, site);
if (UNEXPECTED(result.isUndef())) return false;
out = zend_is_true(result.raw());
return true;
}
zv::Val pt_template_argument_frame_new(zval *parent, zval *resolutions, zval *siteStatementIndexes, zval *closureSignatureBody, zval *closureSignatureStmts, zval *settledClosureSites, bool observingClosures, zval *byRefSites)
{
return TemplateArgumentFrame::create(parent != NULL && Z_TYPE_P(parent) == IS_NULL ? NULL : parent, resolutions != NULL && Z_TYPE_P(resolutions) == IS_NULL ? NULL : resolutions, siteStatementIndexes, closureSignatureBody != NULL && Z_TYPE_P(closureSignatureBody) == IS_NULL ? NULL : closureSignatureBody, closureSignatureStmts, settledClosureSites, observingClosures, byRefSites);
}
zv::Val pt_template_argument_frame_with_observed_closures(zval *frame, zval *closureResolutions, zval *closureSiteStatementIndexes, zval *settledClosureSites, zval *byRefSites)
{
if (EXPECTED(Z_OBJCE_P(frame) == pt_ce_template_argument_frame)) return TemplateArgumentFrame(Z_OBJ_P(frame)).withObservedClosures(closureResolutions, closureSiteStatementIndexes, settledClosureSites, byRefSites);
zv::Args argv{closureResolutions, closureSiteStatementIndexes, settledClosureSites, byRefSites};
return pt_type_call(Z_OBJ_P(frame), PT_LC("withobservedclosures"), 4, argv);
}
zv::Val pt_template_argument_frame_get_by_ref_site_mode(zval *frame, zval *site)
{
if (EXPECTED(Z_OBJCE_P(frame) == pt_ce_template_argument_frame)) return TemplateArgumentFrame(Z_OBJ_P(frame)).getByRefSiteMode(site);
return pt_type_call(Z_OBJ_P(frame), PT_LC("getbyrefsitemode"), 1, site);
}
zv::Val pt_template_argument_frame_get_static_variable_types(zval *frame, zval *var)
{
if (EXPECTED(Z_OBJCE_P(frame) == pt_ce_template_argument_frame)) return TemplateArgumentFrame(Z_OBJ_P(frame)).getStaticVariableTypes(var);
return pt_type_call(Z_OBJ_P(frame), PT_LC("getstaticvariabletypes"), 1, var);
}
zv::Val pt_template_argument_frame_with_static_variable_types(zval *frame, zval *staticVariableTypes, zval *statementIndexes, zval *conditionalExpressions)
{
zval empty;
ZVAL_EMPTY_ARRAY(&empty);
if (conditionalExpressions == NULL) conditionalExpressions = ∅
if (EXPECTED(Z_OBJCE_P(frame) == pt_ce_template_argument_frame)) return TemplateArgumentFrame(Z_OBJ_P(frame)).withStaticVariableTypes(staticVariableTypes, statementIndexes, conditionalExpressions);
zv::Args argv{staticVariableTypes, statementIndexes, conditionalExpressions};
return pt_type_call(Z_OBJ_P(frame), PT_LC("withstaticvariabletypes"), 3, argv);
}
zv::Val pt_template_argument_frame_get_static_variable_conditional_expressions(zval *frame, zval *stmt)
{
if (EXPECTED(Z_OBJCE_P(frame) == pt_ce_template_argument_frame)) return TemplateArgumentFrame(Z_OBJ_P(frame)).getStaticVariableConditionalExpressions(stmt);
return pt_type_call(Z_OBJ_P(frame), PT_LC("getstaticvariableconditionalexpressions"), 1, stmt);
}
zv::Val pt_template_argument_frame_get_local_by_ref_sites(zval *frame)
{
if (EXPECTED(Z_OBJCE_P(frame) == pt_ce_template_argument_frame)) return TemplateArgumentFrame(Z_OBJ_P(frame)).getLocalByRefSites();
return pt_type_call(Z_OBJ_P(frame), PT_LC("getlocalbyrefsites"), 0, NULL);
}
/* the twin is final: the native class entry answers natively, anything
* else (the PHP twin declared next to the native class in the differential
* tests) through the method */
zv::Val pt_template_argument_frame_resolve(zval *frame, zval *site, zend_string *templateName)
{
if (EXPECTED(Z_OBJCE_P(frame) == pt_ce_template_argument_frame)) return TemplateArgumentFrame(Z_OBJ_P(frame)).resolve(site, templateName);
zval nameZv;
ZVAL_STR(&nameZv, templateName);
zv::Args argv{site, &nameZv};
return pt_type_call(Z_OBJ_P(frame), PT_LC("resolve"), 2, argv);
}
zv::Val pt_template_argument_frame_resolve_or_unconstrained(zval *frame, zval *site, zval *templateType)
{
if (EXPECTED(Z_OBJCE_P(frame) == pt_ce_template_argument_frame)) return TemplateArgumentFrame(Z_OBJ_P(frame)).resolveOrUnconstrained(site, templateType);
zv::Args argv{site, templateType};
return pt_type_call(Z_OBJ_P(frame), PT_LC("resolveorunconstrained"), 2, argv);
}
zv::Val pt_template_argument_frame_resolution_cache_key_suffix(zval *frame)
{
if (EXPECTED(Z_OBJCE_P(frame) == pt_ce_template_argument_frame)) return TemplateArgumentFrame(Z_OBJ_P(frame)).getResolutionCacheKeySuffix();
return pt_type_call(Z_OBJ_P(frame), PT_LC("getresolutioncachekeysuffix"), 0, NULL);
}
/* }}} */
/* {{{ engine ABI glue: parameter parsing + registration */
#include "reg.h"
PT_MINIT_REGISTRATION(pt_register_template_argument_frame)
{
pt_taf_original_site_str = zend_string_init_interned(pt_taf_original_site, strlen(pt_taf_original_site), 1);
pt_taf_local = zend_string_init_interned(PT_LC("local"), 1);
pt_taf_escaped = zend_string_init_interned(PT_LC("escaped"), 1);
reg::Class cls("PHPStan\\Analyser\\Generics\\TemplateArgumentFrame");
ptdecl::TemplateArgumentFrame::declareClass(cls);
cls.publicClassConstantString("SYNTHETIC_SITE_ATTRIBUTE", pt_taf_synthetic_site);
cls.publicClassConstantString("ORIGINAL_SITE_ATTRIBUTE", pt_taf_original_site);
ptdecl::TemplateArgumentFrame::declareProperties(cls);
cls.method(sigs::returnTypeOfCall, [](INTERNAL_FUNCTION_PARAMETERS) {
zval *acceptor, *scope, *site;
bool allowUnresolved = false, allowUnresolvedIsNull = true;
ZEND_PARSE_PARAMETERS_START(3, 4)
Z_PARAM_OBJECT(acceptor)
Z_PARAM_OBJECT(scope)
Z_PARAM_OBJECT(site)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL_OR_NULL(allowUnresolved, allowUnresolvedIsNull)
ZEND_PARSE_PARAMETERS_END();
PT_RETURN_VAL(TemplateArgumentFrame::returnTypeOfCall(acceptor, scope, site, allowUnresolvedIsNull ? -1 : (allowUnresolved ? 1 : 0)));
});
cls.method(sigs::__construct, [](INTERNAL_FUNCTION_PARAMETERS) {
zval *parent, *resolutions = NULL, *siteStatementIndexes = NULL, *closureSignatureBody = NULL, *closureSignatureStmts = NULL, *settledClosureSites = NULL, *byRefSites = NULL, *staticVariableTypes = NULL, *staticVariableConditionalExpressions = NULL;
bool observingClosures = false;
ZEND_PARSE_PARAMETERS_START(1, 10)
Z_PARAM_OBJECT_OR_NULL(parent)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_OR_NULL(resolutions)
Z_PARAM_ARRAY(siteStatementIndexes)
Z_PARAM_OBJECT_OR_NULL(closureSignatureBody)
Z_PARAM_ARRAY(closureSignatureStmts)
Z_PARAM_ARRAY(settledClosureSites)
Z_PARAM_BOOL(observingClosures)
Z_PARAM_ARRAY(byRefSites)
Z_PARAM_ARRAY(staticVariableTypes)
Z_PARAM_ARRAY(staticVariableConditionalExpressions)
ZEND_PARSE_PARAMETERS_END();
if (UNEXPECTED(!TemplateArgumentFrame(Z_OBJ_P(ZEND_THIS)).construct(parent, resolutions, siteStatementIndexes, closureSignatureBody, closureSignatureStmts, settledClosureSites, observingClosures, byRefSites, staticVariableTypes, staticVariableConditionalExpressions))) RETURN_THROWS();
});
cls.method(sigs::withObservedClosures, [](INTERNAL_FUNCTION_PARAMETERS) {
zval *closureResolutions, *closureSiteStatementIndexes, *settledClosureSites, *byRefSites = NULL;
ZEND_PARSE_PARAMETERS_START(3, 4)
Z_PARAM_ARRAY(closureResolutions)
Z_PARAM_ARRAY(closureSiteStatementIndexes)
Z_PARAM_ARRAY(settledClosureSites)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY(byRefSites)
ZEND_PARSE_PARAMETERS_END();
PT_RETURN_VAL(TemplateArgumentFrame(Z_OBJ_P(ZEND_THIS)).withObservedClosures(closureResolutions, closureSiteStatementIndexes, settledClosureSites, byRefSites));
});
cls.method<&TemplateArgumentFrame::getByRefSiteMode, zp::Obj>(sigs::getByRefSiteMode);
cls.method<&TemplateArgumentFrame::getLocalByRefSites>(sigs::getLocalByRefSites);
cls.method<&TemplateArgumentFrame::getStaticVariableTypes, zp::Obj>(sigs::getStaticVariableTypes);
cls.method<&TemplateArgumentFrame::getStaticVariableConditionalExpressions, zp::Obj>(sigs::getStaticVariableConditionalExpressions);
cls.method(sigs::withStaticVariableTypes, [](INTERNAL_FUNCTION_PARAMETERS) {
zval *staticVariableTypes, *statementIndexes, *conditionalExpressions = NULL;
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_ARRAY(staticVariableTypes)
Z_PARAM_ARRAY(statementIndexes)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY(conditionalExpressions)
ZEND_PARSE_PARAMETERS_END();
zval empty;
ZVAL_EMPTY_ARRAY(&empty);
PT_RETURN_VAL(TemplateArgumentFrame(Z_OBJ_P(ZEND_THIS)).withStaticVariableTypes(staticVariableTypes, statementIndexes, conditionalExpressions != NULL ? conditionalExpressions : &empty));
});
cls.method(sigs::isSettledClosureSite, [](INTERNAL_FUNCTION_PARAMETERS) {
zval *site;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJECT(site)
ZEND_PARSE_PARAMETERS_END();
bool out;
if (UNEXPECTED(!TemplateArgumentFrame(Z_OBJ_P(ZEND_THIS)).isSettledClosureSite(site, out))) RETURN_THROWS();
RETURN_BOOL(out);
});
cls.method(sigs::getParent, [](INTERNAL_FUNCTION_PARAMETERS) {
ZEND_PARSE_PARAMETERS_NONE();
RETURN_COPY(TemplateArgumentFrame(Z_OBJ_P(ZEND_THIS)).parent());
});
cls.method(sigs::getClosureSignatureBody, [](INTERNAL_FUNCTION_PARAMETERS) {
ZEND_PARSE_PARAMETERS_NONE();
RETURN_COPY(TemplateArgumentFrame(Z_OBJ_P(ZEND_THIS)).closureSignatureBody());
});
cls.method(sigs::getClosureSignatureStmts, [](INTERNAL_FUNCTION_PARAMETERS) {
ZEND_PARSE_PARAMETERS_NONE();
RETURN_COPY(TemplateArgumentFrame(Z_OBJ_P(ZEND_THIS)).closureSignatureStmts());
});
cls.method<&TemplateArgumentFrame::isObserving>(sigs::isObserving);
cls.method<&TemplateArgumentFrame::isObservingClosures>(sigs::isObservingClosures);
cls.method<&TemplateArgumentFrame::firstSiteStatementIndex>(sigs::firstSiteStatementIndex);
cls.method<&TemplateArgumentFrame::ownsSiteInStatement, zp::Long>(sigs::ownsSiteInStatement);
cls.method<&TemplateArgumentFrame::hasSiteAtOrAfter, zp::Long>(sigs::hasSiteAtOrAfter);
cls.method<&TemplateArgumentFrame::resolveOrUnconstrained, zp::Obj, zp::Obj>(sigs::resolveOrUnconstrained);
cls.method(sigs::resolveUnconstrained, [](INTERNAL_FUNCTION_PARAMETERS) {
zval *site, *templateType, *resolver;
if (!zp::parse<zp::Obj, zp::Obj, zp::Zval>(execute_data, site, templateType, resolver)) RETURN_THROWS();
if (UNEXPECTED(!zend_is_callable(resolver, 0, NULL))) {
zend_argument_type_error(3, "must be of type callable, %s given", zend_zval_value_name(resolver));
RETURN_THROWS();
}
PT_RETURN_VAL(TemplateArgumentFrame::resolveUnconstrained(site, templateType, resolver));
});
cls.method(sigs::resolve, [](INTERNAL_FUNCTION_PARAMETERS) {
zval *site;
zend_string *templateName;
if (!zp::parse<zp::Obj, zp::Str>(execute_data, site, templateName)) RETURN_THROWS();
PT_RETURN_VAL(TemplateArgumentFrame(Z_OBJ_P(ZEND_THIS)).resolve(site, templateName));
});
cls.method<&TemplateArgumentFrame::getResolutionCacheKeySuffix>(sigs::getResolutionCacheKeySuffix);
cls.shadow(&pt_ce_template_argument_frame);
}
/* }}} */