Repository navigation
Expand file tree
/
Copy pathTemplateArgumentObserver.cpp
More file actions
1642 lines (1518 loc) · 74.5 KB
/
Copy pathTemplateArgumentObserver.cpp
File metadata and controls
1642 lines (1518 loc) · 74.5 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\TemplateArgumentObserver — native implementation of
* PHPStan\Analyser\Generics\TemplateArgumentObserver.
*
* The stateless DI service matching declared and actual types for template
* arguments a call left unresolved. collectSites() runs on the type of every
* call the observation pass walks (~90K per self-analysis) and its
* TypeTraverser callback on every node of those types, so it is a native
* callback holder entered by the native traverser without a frame; the
* matching of sends and lower bounds (a few thousand calls) asks the Type
* kernel through its ops, ClassReflection through its entries and the rest
* of the template machinery by name. The twin's closures over $this and
* `&$constraints` are native closures over the same captures. Native callers
* use the pt_template_argument_observer_* entries (support.h).
*/
#include "support.h"
#include "generated/TemplateArgumentObserver.h"
#include "generated/UnresolvedTemplateArgumentType.h"
namespace sigs = ptdecl::TemplateArgumentObserver::sig;
#include "zv.h"
#include "TypeTraits.h"
#include "TypeOps.h"
#include "Engine.h"
#include "AcceptorValues.h"
#include "ParameterValues.h"
zend_class_entry *pt_ce_template_argument_observer = nullptr;
namespace {
/* {{{ type tests and reads */
inline bool isMarker(zval *type)
{
return Z_TYPE_P(type) == IS_OBJECT && Z_OBJCE_P(type) == pt_ce_unresolved_template_argument_type;
}
inline bool isInstance(zval *type, zend_class_entry *ce)
{
return Z_TYPE_P(type) == IS_OBJECT && instanceof_function(Z_OBJCE_P(type), ce);
}
/* $type instanceof TemplateType; false = pending exception */
[[nodiscard]] bool isTemplateType(zval *type, bool &out)
{
zend_class_entry *ce = pt_class_loaded(PT_CLASS_TEMPLATE_TYPE);
if (UNEXPECTED(EG(exception))) return false;
out = ce != NULL && isInstance(type, ce);
return true;
}
/* $type instanceof UnionType && !$type instanceof TemplateType; false =
* pending exception */
[[nodiscard]] bool isPlainUnion(zval *type, bool &out)
{
if (!isInstance(type, pt_ce_union_type)) {
out = false;
return true;
}
bool isTemplate;
if (UNEXPECTED(!isTemplateType(type, isTemplate))) return false;
out = !isTemplate;
return true;
}
/* $type instanceof MixedType && !$type instanceof TemplateType; false =
* pending exception */
[[nodiscard]] bool isPlainMixed(zval *type, bool &out)
{
if (!isInstance(type, pt_ce_mixed_type)) {
out = false;
return true;
}
bool isTemplate;
if (UNEXPECTED(!isTemplateType(type, isTemplate))) return false;
out = !isTemplate;
return true;
}
/* $marker->getInitialType() of an UnresolvedTemplateArgumentType */
zv::Val markerInitialType(zval *marker)
{
zval *initial = OBJ_PROP_NUM(Z_OBJ_P(marker), ptdecl::UnresolvedTemplateArgumentType::slot::initialType);
if (EXPECTED(Z_TYPE_P(initial) != IS_UNDEF)) return zv::Val::copyOf(zv::Ref(initial));
return pt_type_call(Z_OBJ_P(marker), PT_LC("getinitialtype"), 0, NULL);
}
/* $object->method() by name, the engine's Error for a non-object */
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);
}
zv::Val call1(zval *object, const char *lcname, size_t len, const char *method, zval *argument)
{
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, 1, argument);
}
/* $type->isX()->yes() of a TrinaryLogic query by name; false = pending exception */
[[nodiscard]] bool trinaryYes(zval *type, const char *lcname, size_t len, bool &out)
{
zend_long value = pt_type_call_trinary(Z_OBJ_P(type), lcname, len, 0, NULL);
if (UNEXPECTED(value < 0)) return false;
out = value == PT_TRI_YES;
return true;
}
/* $variance->invariant() / ->covariant() / ->contravariant() / ->bivariant();
* false = pending exception */
[[nodiscard]] bool varianceIs(zval *variance, zend_long expected, bool &out)
{
if (UNEXPECTED(Z_TYPE_P(variance) != IS_OBJECT)) {
const char *name = expected == PT_TEMPLATE_TYPE_VARIANCE_INVARIANT ? "invariant" : (expected == PT_TEMPLATE_TYPE_VARIANCE_CONTRAVARIANT ? "contravariant" : "bivariant");
zend_throw_error(NULL, "Call to a member function %s() on %s", name, zend_zval_value_name(variance));
return false;
}
zend_long value;
if (UNEXPECTED(!pt_template_type_variance_value_of(variance, value))) return false;
out = value == expected;
return true;
}
/* $list[$i] of a list read by an integer key, NULL when absent */
inline zval *listItem(zval *list, zend_ulong index)
{
if (UNEXPECTED(Z_TYPE_P(list) != IS_ARRAY)) return NULL;
zval *item = zend_hash_index_find(Z_ARRVAL_P(list), index);
if (item != NULL) {
ZVAL_DEREF(item);
}
return item;
}
/* $type->getObjectClassReflections() */
zv::Val objectClassReflections(zval *type)
{
return pt_type_op(Z_OBJ_P(type), PT_OP_GET_OBJECT_CLASS_REFLECTIONS, 0, NULL);
}
inline uint32_t countOf(zval *array)
{
return Z_TYPE_P(array) == IS_ARRAY ? zend_hash_num_elements(Z_ARRVAL_P(array)) : 0;
}
/* }}} */
void collectSitesBody(zval *constraints, zval *state1, uint32_t argc, zval *argv, zval *return_value);
void containsMarkerBody(zval *contains, zval *state1, uint32_t argc, zval *argv, zval *return_value);
void containsTemplateArgumentMarkerBody(zval *contains, zval *state1, uint32_t argc, zval *argv, zval *return_value);
zv::Val escapeByRefUses(zv::Val constraints, zval *closureType);
void containsClosureSignatureMarkerBody(zval *contains, zval *state1, uint32_t argc, zval *argv, zval *return_value);
void replaceInferableTemplatesBody(zval *captures, uint32_t argc, zval *argv, zval *return_value);
} // namespace
namespace phpstanturbo {
/* Mirrors PHPStan\Analyser\Generics\TemplateArgumentObserver (stateless);
* the constraints flow through by value as the twin passes them; UNDEF =
* pending exception. */
class TemplateArgumentObserver
{
public:
/* Mirrors collectSites(). */
static zv::Val collectSites(zval *type)
{
zv::Val constraints = pt_template_argument_constraints_create_empty();
zv::Val callback = pt_type_native_callback(collectSitesBody, constraints.raw(), NULL);
if (UNEXPECTED(callback.isUndef())) return zv::Val();
constraints.release();
zv::Val mapped = pt_type_traverser_map_of(type, callback.raw());
if (UNEXPECTED(mapped.isUndef())) return zv::Val();
return zv::Val::copyOf(zv::Ref(pt_type_native_callback_state(callback.raw(), 0)));
}
/* Mirrors containsMarker(); false = pending exception */
[[nodiscard]] static bool containsMarker(zval *type, bool &out, bool templateArgumentsOnly = false)
{
zval contains;
ZVAL_FALSE(&contains);
zv::Val callback = pt_type_native_callback(templateArgumentsOnly ? containsTemplateArgumentMarkerBody : containsMarkerBody, &contains, NULL);
if (UNEXPECTED(callback.isUndef())) return false;
zv::Val mapped = pt_type_traverser_map_of(type, callback.raw());
if (UNEXPECTED(mapped.isUndef())) return false;
out = Z_TYPE_P(pt_type_native_callback_state(callback.raw(), 0)) == IS_TRUE;
return true;
}
/* Mirrors collectSend(). */
static zv::Val collectSend(zval *declared, zval *actual)
{
zv::Val constraints = observeSend(pt_template_argument_constraints_create_empty(), declared, actual, false);
return observeClosureSend(std::move(constraints), declared, actual);
}
/* Mirrors collectClosureArgument(). */
static zv::Val collectClosureArgument(zval *parameterType, zval *argumentType)
{
return observeClosureSend(pt_template_argument_constraints_create_empty(), parameterType, argumentType);
}
/* Mirrors collectClosureArguments(). */
static zv::Val collectClosureArguments(zval *acceptor, zval *argumentTypes, bool isPure)
{
zv::Val constraints = pt_template_argument_constraints_create_empty();
zv::Val parameters;
zv::Arr parametersByName = zv::Arr::create(0);
uint32_t parameterCount = 0;
for (auto entry : zv::ArrRef(argumentTypes)) {
zval *argumentType = entry.value().deref().raw();
bool contains;
if (UNEXPECTED(!containsClosureSignatureMarker(argumentType, contains))) return zv::Val();
if (!contains) continue;
if (parameters.isUndef()) {
parameters = pt_parameters_acceptor_call(acceptor, PT_PA_GET_PARAMETERS);
if (UNEXPECTED(parameters.isUndef())) return zv::Val();
for (auto parameterEntry : zv::ArrRef(parameters.raw())) {
zval *parameter = parameterEntry.value().deref().raw();
zv::Val name = pt_parameter_reflection_call(parameter, PT_PR_GET_NAME);
if (UNEXPECTED(name.isUndef())) return zv::Val();
zval *slot = Z_TYPE_P(name.raw()) == IS_STRING
? zend_symtable_update(parametersByName.table(), Z_STR_P(name.raw()), parameter)
: zend_hash_index_update(parametersByName.table(), (zend_ulong) zval_get_long(name.raw()), parameter);
Z_TRY_ADDREF_P(slot);
}
parameterCount = countOf(parameters.raw());
}
zval *parameter;
zend_string *key = entry.stringKeyOrNull();
if (key != NULL) {
parameter = zend_hash_find(parametersByName.table(), key);
} else {
parameter = listItem(parameters.raw(), entry.indexKey());
}
if (parameter != NULL) {
ZVAL_DEREF(parameter);
}
if (parameter == NULL || Z_TYPE_P(parameter) == IS_NULL) {
bool variadic;
if (UNEXPECTED(!pt_parameters_acceptor_bool(acceptor, PT_PA_IS_VARIADIC, variadic))) return zv::Val();
parameter = variadic && parameterCount > 0 ? listItem(parameters.raw(), parameterCount - 1) : NULL;
}
if (parameter == NULL || Z_TYPE_P(parameter) == IS_NULL) {
constraints = escapeClosures(std::move(constraints), argumentType);
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
continue;
}
zv::Val parameterHold = zv::Val::copyOf(zv::Ref(parameter));
zv::Val parameterType = pt_parameter_reflection_call(parameterHold.raw(), PT_PR_GET_TYPE);
if (UNEXPECTED(parameterType.isUndef())) return zv::Val();
if (isPure) {
bool plainMixed;
if (UNEXPECTED(!isPlainMixed(parameterType.raw(), plainMixed))) return zv::Val();
if (plainMixed) continue;
}
constraints = observeClosureSend(std::move(constraints), parameterType.raw(), argumentType);
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
}
return constraints;
}
/* Mirrors collectEscape(). */
static zv::Val collectEscape(zval *type)
{
bool contains;
if (UNEXPECTED(!containsClosureSignatureMarker(type, contains))) return zv::Val();
if (!contains) return pt_template_argument_constraints_create_empty();
return escapeClosures(pt_template_argument_constraints_create_empty(), type);
}
/* Mirrors carriesClosureSignatureMarkers(); false = pending exception */
[[nodiscard]] static bool carriesClosureSignatureMarkers(zval *types, bool &out)
{
out = false;
for (auto entry : zv::ArrRef(types)) {
if (UNEXPECTED(!containsClosureSignatureMarker(entry.value().deref().raw(), out))) return false;
if (out) return true;
}
return true;
}
/* Mirrors containsClosureSignatureMarker(); false = pending exception */
[[nodiscard]] static bool containsClosureSignatureMarker(zval *type, bool &out)
{
zval contains;
ZVAL_FALSE(&contains);
zv::Val callback = pt_type_native_callback(containsClosureSignatureMarkerBody, &contains, NULL);
if (UNEXPECTED(callback.isUndef())) return false;
zv::Val mapped = pt_type_traverser_map_of(type, callback.raw());
if (UNEXPECTED(mapped.isUndef())) return false;
out = Z_TYPE_P(pt_type_native_callback_state(callback.raw(), 0)) == IS_TRUE;
return true;
}
/* Mirrors observeClosureSend(). */
static zv::Val observeClosureSend(zv::Val constraints, zval *declared, zval *actual)
{
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
bool contains;
if (UNEXPECTED(!containsClosureSignatureMarker(actual, contains))) return zv::Val();
if (!contains) return constraints;
if (isInstance(actual, pt_ce_union_type)) {
zv::Val types = pt_type_op(Z_OBJ_P(actual), PT_OP_GET_TYPES, 0, NULL);
if (UNEXPECTED(types.isUndef())) return zv::Val();
for (auto entry : zv::ArrRef(types.raw())) {
constraints = observeClosureSend(std::move(constraints), declared, entry.value().deref().raw());
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
}
return constraints;
}
if (isInstance(actual, pt_ce_closure_type)) {
return observeClosureSendToCallable(std::move(constraints), declared, actual);
}
bool actualObject, actualIterable;
if (UNEXPECTED(!trinaryYes(actual, PT_LC("isobject"), actualObject))) return zv::Val();
if (!actualObject) {
if (UNEXPECTED(!trinaryYes(actual, PT_LC("isiterable"), actualIterable))) return zv::Val();
if (actualIterable) {
bool declaredObject;
if (UNEXPECTED(!trinaryYes(declared, PT_LC("isobject"), declaredObject))) return zv::Val();
if (!declaredObject) {
bool declaredIterable;
if (UNEXPECTED(!trinaryYes(declared, PT_LC("isiterable"), declaredIterable))) return zv::Val();
if (declaredIterable) {
zv::Val declaredKey = pt_type_op(Z_OBJ_P(declared), PT_OP_GET_ITERABLE_KEY_TYPE, 0, NULL);
if (UNEXPECTED(declaredKey.isUndef())) return zv::Val();
zv::Val actualKey = pt_type_op(Z_OBJ_P(actual), PT_OP_GET_ITERABLE_KEY_TYPE, 0, NULL);
if (UNEXPECTED(actualKey.isUndef())) return zv::Val();
constraints = observeClosureSend(std::move(constraints), declaredKey.raw(), actualKey.raw());
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
zv::Val declaredValue = pt_type_op(Z_OBJ_P(declared), PT_OP_GET_ITERABLE_VALUE_TYPE, 0, NULL);
if (UNEXPECTED(declaredValue.isUndef())) return zv::Val();
bool plainMixed;
if (UNEXPECTED(!isPlainMixed(declaredValue.raw(), plainMixed))) return zv::Val();
if (plainMixed) {
return escapeClosures(std::move(constraints), actual);
}
zv::Val actualValue = pt_type_op(Z_OBJ_P(actual), PT_OP_GET_ITERABLE_VALUE_TYPE, 0, NULL);
if (UNEXPECTED(actualValue.isUndef())) return zv::Val();
return observeClosureSend(std::move(constraints), declaredValue.raw(), actualValue.raw());
}
}
}
}
return escapeClosures(std::move(constraints), actual);
}
/* Mirrors observeClosureSendToCallable(). */
static zv::Val observeClosureSendToCallable(zv::Val constraints, zval *declaredArg, zval *actual)
{
zv::Val declared = zv::Val::copyOf(zv::Ref(declaredArg));
bool declaredTemplate;
if (UNEXPECTED(!isTemplateType(declared.raw(), declaredTemplate))) return zv::Val();
if (isInstance(declared.raw(), pt_ce_union_type) && !declaredTemplate) {
zv::Val types = pt_type_op(Z_OBJ_P(declared.raw()), PT_OP_GET_TYPES, 0, NULL);
if (UNEXPECTED(types.isUndef())) return zv::Val();
zv::Arr kept = zv::Arr::create(0);
for (auto entry : zv::ArrRef(types.raw())) {
zval *member = entry.value().deref().raw();
zend_long callable = pt_type_call_trinary(Z_OBJ_P(member), PT_LC("iscallable"), 0, NULL);
if (UNEXPECTED(callable < 0)) return zv::Val();
if (callable == PT_TRI_NO) continue;
kept.push(zv::Val::copyOf(zv::Ref(member)));
}
HashTable *keptTable = kept.table();
uint32_t keptCount = zend_hash_num_elements(keptTable);
zval *keptArgv = keptCount > 0 ? (zval *) safe_emalloc(keptCount, sizeof(zval), 0) : NULL;
uint32_t k = 0;
for (auto entry : zv::ArrRef(kept.raw())) {
ZVAL_COPY_VALUE(&keptArgv[k++], entry.value().raw());
}
declared = pt_type_combinator_union(keptCount, keptArgv);
if (keptArgv != NULL) efree(keptArgv);
if (UNEXPECTED(declared.isUndef())) return zv::Val();
}
bool declaredMixed = isInstance(declared.raw(), pt_ce_mixed_type);
bool declaredCallable = false;
if (!declaredMixed && UNEXPECTED(!trinaryYes(declared.raw(), PT_LC("iscallable"), declaredCallable))) return zv::Val();
if (declaredMixed || !declaredCallable) {
return escapeClosures(std::move(constraints), actual);
}
// whoever is given the closure invokes it when it chooses
constraints = escapeByRefUses(std::move(constraints), actual);
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
zv::Val closureParameters = pt_type_call(Z_OBJ_P(actual), PT_LC("getparameters"), 0, NULL);
if (UNEXPECTED(closureParameters.isUndef())) return zv::Val();
zv::Val returnMarker = pt_type_call(Z_OBJ_P(actual), PT_LC("getreturntype"), 0, NULL);
if (UNEXPECTED(returnMarker.isUndef())) return zv::Val();
zv::Val outOfClassScope = pt_type_new(PT_CLASS_OUT_OF_CLASS_SCOPE, 0, NULL);
if (UNEXPECTED(outOfClassScope.isUndef())) return zv::Val();
zv::Val acceptors = pt_type_call(Z_OBJ_P(declared.raw()), PT_LC("getcallableparametersacceptors"), 1, outOfClassScope.raw());
if (UNEXPECTED(acceptors.isUndef())) return zv::Val();
for (auto acceptorEntry : zv::ArrRef(acceptors.raw())) {
zv::Val acceptor = zv::Val::copyOf(acceptorEntry.value().deref());
zv::Val targetParameters = pt_parameters_acceptor_call(acceptor.raw(), PT_PA_GET_PARAMETERS);
if (UNEXPECTED(targetParameters.isUndef())) return zv::Val();
uint32_t targetCount = countOf(targetParameters.raw());
bool acceptorVariadic;
if (UNEXPECTED(!pt_parameters_acceptor_bool(acceptor.raw(), PT_PA_IS_VARIADIC, acceptorVariadic))) return zv::Val();
if (targetCount == 0 && acceptorVariadic) {
/* callable, Closure: the parameters are not described */
constraints = escapeClosures(std::move(constraints), actual);
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
continue;
}
for (auto parameterEntry : zv::ArrRef(closureParameters.raw())) {
zval *closureParameter = parameterEntry.value().deref().raw();
zv::Val marker = pt_parameter_reflection_call(closureParameter, PT_PR_GET_TYPE);
if (UNEXPECTED(marker.isUndef())) return zv::Val();
if (!isMarker(marker.raw())) continue;
bool closureMarker;
if (UNEXPECTED(!pt_unresolved_template_argument_type_is_closure_signature(marker.raw(), closureMarker))) return zv::Val();
if (!closureMarker) continue;
zend_ulong i = parameterEntry.indexKey();
bool variadic;
if (UNEXPECTED(!pt_parameter_reflection_bool(closureParameter, PT_PR_IS_VARIADIC, variadic))) return zv::Val();
if (variadic) {
for (zend_ulong j = i; j < targetCount; j++) {
zval *target = listItem(targetParameters.raw(), j);
if (target == NULL) continue;
zv::Val targetType = pt_parameter_reflection_call(target, PT_PR_GET_TYPE);
if (UNEXPECTED(targetType.isUndef())) return zv::Val();
constraints = pt_template_argument_constraints_with_lower_bound(constraints.raw(), marker.raw(), targetType.raw());
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
}
continue;
}
zval *target = listItem(targetParameters.raw(), i);
if (target != NULL && Z_TYPE_P(target) != IS_NULL) {
zv::Val targetType = pt_parameter_reflection_call(target, PT_PR_GET_TYPE);
if (UNEXPECTED(targetType.isUndef())) return zv::Val();
constraints = pt_template_argument_constraints_with_lower_bound(constraints.raw(), marker.raw(), targetType.raw());
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
continue;
}
if (!acceptorVariadic || targetCount == 0) {
/* never passed: the closure parameter keeps its default */
continue;
}
zval *last = listItem(targetParameters.raw(), targetCount - 1);
if (last == NULL) continue;
zv::Val lastType = pt_parameter_reflection_call(last, PT_PR_GET_TYPE);
if (UNEXPECTED(lastType.isUndef())) return zv::Val();
constraints = pt_template_argument_constraints_with_lower_bound(constraints.raw(), marker.raw(), lastType.raw());
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
}
zv::Val targetReturnType = pt_parameters_acceptor_call(acceptor.raw(), PT_PA_GET_RETURN_TYPE);
if (UNEXPECTED(targetReturnType.isUndef())) return zv::Val();
zv::Val returnedType;
bool closureReturn = false;
if (isMarker(returnMarker.raw()) && UNEXPECTED(!pt_unresolved_template_argument_type_is_closure_return(returnMarker.raw(), closureReturn))) return zv::Val();
if (closureReturn) {
bool isVoid;
if (UNEXPECTED(!trinaryYes(targetReturnType.raw(), PT_LC("isvoid"), isVoid))) return zv::Val();
if (!isVoid && !isInstance(targetReturnType.raw(), pt_ce_mixed_type)) {
zv::Val covariant = pt_type_template_type_variance(PT_TEMPLATE_TYPE_VARIANCE_COVARIANT);
if (UNEXPECTED(covariant.isUndef())) return zv::Val();
constraints = pt_template_argument_constraints_with_send(constraints.raw(), returnMarker.raw(), targetReturnType.raw(), covariant.raw());
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
}
returnedType = markerInitialType(returnMarker.raw());
if (UNEXPECTED(returnedType.isUndef())) return zv::Val();
} else {
returnedType = zv::Val::copyOf(zv::Ref(returnMarker.raw()));
}
if (Z_TYPE_P(returnedType.raw()) != IS_OBJECT) continue;
/* a closure returning closures: the returned ones are sent on */
constraints = observeClosureSend(std::move(constraints), targetReturnType.raw(), returnedType.raw());
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
}
return constraints;
}
/* Mirrors escapeClosures(). */
static zv::Val escapeClosures(zv::Val constraints, zval *type)
{
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
zv::Val escapes = pt_closure_signature_inference_collect_escapes(type);
if (UNEXPECTED(escapes.isUndef())) return zv::Val();
return pt_template_argument_constraints_merge(constraints.raw(), escapes.raw());
}
/* Mirrors collectArgument(). */
static zv::Val collectArgument(zval *parameterType, zval *argumentType, bool isPure)
{
if (isPure) {
bool plainMixed;
if (UNEXPECTED(!isPlainMixed(parameterType, plainMixed))) return zv::Val();
if (plainMixed) return pt_template_argument_constraints_create_empty();
}
return observeArgument(pt_template_argument_constraints_create_empty(), parameterType, argumentType);
}
/* Mirrors collectCall() ($classTemplates NULL / IS_NULL for null). */
static zv::Val collectCall(zval *self, zval *site, zval *acceptorZv, zval *argumentTypes, zval *classTemplates)
{
zv::Val constraints = pt_template_argument_constraints_create_empty();
zv::Val acceptor = zv::Val::copyOf(zv::Ref(acceptorZv));
zend_class_entry *resolvedCe = pt_class_loaded(PT_CLASS_RESOLVED_FUNCTION_VARIANT);
if (UNEXPECTED(EG(exception))) return zv::Val();
if (resolvedCe != NULL && isInstance(acceptor.raw(), resolvedCe)) {
acceptor = pt_parameters_acceptor_call(acceptor.raw(), PT_PA_GET_ORIGINAL_PARAMETERS_ACCEPTOR);
if (UNEXPECTED(acceptor.isUndef())) return zv::Val();
}
/* new TemplateTypeMap(array_merge($classTemplates?->getTypes() ?? [], $acceptor->getTemplateTypeMap()->getTypes())) */
zv::Arr merged = zv::Arr::create(0);
if (classTemplates != NULL && Z_TYPE_P(classTemplates) != IS_NULL) {
zv::Val classTypes = pt_template_type_map_get_types(classTemplates);
if (UNEXPECTED(classTypes.isUndef())) return zv::Val();
if (UNEXPECTED(!pt_callable_array_merge_into(merged, classTypes.raw()))) return zv::Val();
}
zv::Val templateTypeMap = pt_parameters_acceptor_call(acceptor.raw(), PT_PA_GET_TEMPLATE_TYPE_MAP);
if (UNEXPECTED(templateTypeMap.isUndef())) return zv::Val();
if (UNEXPECTED(Z_TYPE_P(templateTypeMap.raw()) != IS_OBJECT)) {
zend_throw_error(NULL, "Call to a member function getTypes() on %s", zend_zval_value_name(templateTypeMap.raw()));
return zv::Val();
}
zv::Val acceptorTypes = pt_template_type_map_get_types(templateTypeMap.raw());
if (UNEXPECTED(acceptorTypes.isUndef())) return zv::Val();
if (UNEXPECTED(!pt_callable_array_merge_into(merged, acceptorTypes.raw()))) return zv::Val();
zval templatesZv;
if (UNEXPECTED(!pt_template_type_map_new(&templatesZv, merged.raw()))) return zv::Val();
zv::Val templates = zv::Val::adopt(templatesZv);
zv::Val isEmpty = pt_type_call(Z_OBJ_P(templates.raw()), PT_LC("isempty"), 0, NULL);
if (UNEXPECTED(isEmpty.isUndef())) return zv::Val();
if (zend_is_true(isEmpty.raw())) return constraints;
bool hasMarkers = false;
for (auto entry : zv::ArrRef(argumentTypes)) {
bool contains;
/* a closure whose signature is being inferred is sent through
* collectClosureArguments(), it holds no template argument */
if (UNEXPECTED(!containsMarker(entry.value().deref().raw(), contains, true))) return zv::Val();
if (!contains) continue;
hasMarkers = true;
break;
}
if (!hasMarkers) return constraints;
zv::Val parameters = pt_parameters_acceptor_call(acceptor.raw(), PT_PA_GET_PARAMETERS);
if (UNEXPECTED(parameters.isUndef())) return zv::Val();
zv::Arr parametersByName = zv::Arr::create(0);
for (auto entry : zv::ArrRef(parameters.raw())) {
zval *parameter = entry.value().deref().raw();
zv::Val name = pt_parameter_reflection_call(parameter, PT_PR_GET_NAME);
if (UNEXPECTED(name.isUndef())) return zv::Val();
zval *slot = Z_TYPE_P(name.raw()) == IS_STRING
? zend_symtable_update(parametersByName.table(), Z_STR_P(name.raw()), parameter)
: zend_hash_index_update(parametersByName.table(), (zend_ulong) zval_get_long(name.raw()), parameter);
Z_TRY_ADDREF_P(slot);
}
uint32_t parameterCount = countOf(parameters.raw());
for (auto entry : zv::ArrRef(argumentTypes)) {
zval *argumentType = entry.value().deref().raw();
zval *parameter;
zend_string *key = entry.stringKeyOrNull();
if (key != NULL) {
parameter = zend_hash_find(parametersByName.table(), key);
} else {
parameter = listItem(parameters.raw(), entry.indexKey());
}
if (parameter != NULL) {
ZVAL_DEREF(parameter);
}
zv::Val parameterHold;
if (parameter == NULL || Z_TYPE_P(parameter) == IS_NULL) {
bool variadic;
if (UNEXPECTED(!pt_parameters_acceptor_bool(acceptor.raw(), PT_PA_IS_VARIADIC, variadic))) return zv::Val();
parameter = variadic && parameterCount > 0 ? listItem(parameters.raw(), parameterCount - 1) : NULL;
if (parameter != NULL) {
parameterHold = zv::Val::copyOf(zv::Ref(parameter));
parameter = parameterHold.raw();
}
} else {
parameterHold = zv::Val::copyOf(zv::Ref(parameter));
parameter = parameterHold.raw();
}
if (parameter == NULL || Z_TYPE_P(parameter) == IS_NULL) continue;
zv::Val parameterType = pt_parameter_reflection_call(parameter, PT_PR_GET_TYPE);
if (UNEXPECTED(parameterType.isUndef())) return zv::Val();
zval reference;
ZVAL_NEW_REF(&reference, constraints.raw());
ZVAL_UNDEF(constraints.raw());
zv::Val referenceHold = zv::Val::adopt(reference);
zv::Val replaced = replaceInferableTemplates(self, parameterType.raw(), site, templates.raw(), referenceHold.raw());
constraints = zv::Val::copyOf(zv::Ref(Z_REFVAL_P(referenceHold.raw())));
if (UNEXPECTED(replaced.isUndef())) return zv::Val();
constraints = observeArgument(std::move(constraints), replaced.raw(), argumentType);
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
}
return constraints;
}
/* Mirrors replaceInferableTemplates(); $constraints is the reference the
* twin's by-reference parameter binds (an IS_REFERENCE zval). */
static zv::Val replaceInferableTemplates(zval *self, zval *type, zval *site, zval *templates, zval *constraintsReference)
{
bool plainUnion;
if (UNEXPECTED(!isPlainUnion(type, plainUnion))) return zv::Val();
if (plainUnion) {
zv::Val types = pt_type_op(Z_OBJ_P(type), PT_OP_GET_TYPES, 0, NULL);
if (UNEXPECTED(types.isUndef())) return zv::Val();
uint32_t count = countOf(types.raw());
zv::Arr members = zv::Arr::create(count);
zv::Arr naked = zv::Arr::create(0);
zv::Arr structural = zv::Arr::create(0);
for (auto entry : zv::ArrRef(types.raw())) {
zv::Val member = replaceInferableTemplates(self, entry.value().deref().raw(), site, templates, constraintsReference);
if (UNEXPECTED(member.isUndef())) return zv::Val();
members.push(zv::Ref(member.raw()));
if (isMarker(member.raw())) {
naked.push(zv::Ref(member.raw()));
continue;
}
bool contains;
if (UNEXPECTED(!containsMarker(member.raw(), contains))) return zv::Val();
if (contains) structural.push(zv::Ref(member.raw()));
}
if (zend_hash_num_elements(naked.table()) > 0 && zend_hash_num_elements(structural.table()) == 0) {
// a union keeps markers apart from everything else (see
// TypeCombinator::union()) - the absorption is this method's own
zv::Arr kept = zv::Arr::create(zend_hash_num_elements(members.table()));
for (auto entry : zv::ArrRef(members.raw())) {
zval *member = entry.value().raw();
bool keep = true;
if (!(Z_TYPE_P(member) == IS_OBJECT && instanceof_function(Z_OBJCE_P(member), pt_ce_unresolved_template_argument_type))) {
for (auto nakedEntry : zv::ArrRef(naked.raw())) {
zv::Val delegate = pt_type_call(Z_OBJ_P(nakedEntry.value().raw()), PT_LC("getdelegate"), 0, NULL);
if (UNEXPECTED(delegate.isUndef())) return zv::Val();
zv::Val result = pt_type_op(Z_OBJ_P(delegate.raw()), PT_OP_IS_SUPER_TYPE_OF, 1, member);
if (UNEXPECTED(result.isUndef())) return zv::Val();
zend_long trinary = pt_type_result_trinary(result.raw());
if (UNEXPECTED(trinary < 0)) return zv::Val();
if (trinary == PT_TRI_YES) {
keep = false;
break;
}
}
}
if (keep) kept.push(zv::Ref(member));
}
members = std::move(kept);
}
if (zend_hash_num_elements(naked.table()) == 0 || zend_hash_num_elements(structural.table()) == 0) {
uint32_t argc = zend_hash_num_elements(members.table());
zval *argv = (zval *) emalloc(sizeof(zval) * (argc > 0 ? argc : 1));
uint32_t i = 0;
for (auto entry : zv::ArrRef(members.raw())) {
ZVAL_COPY_VALUE(&argv[i++], entry.value().raw());
}
zv::Val union_ = pt_type_combinator_union(argc, argv);
efree(argv);
return union_;
}
for (auto entry : zv::ArrRef(structural.raw())) {
naked.push(entry.value());
}
zval result;
if (UNEXPECTED(!pt_union_type_new(&result, naked.raw()))) return zv::Val();
return zv::Val::adopt(result);
}
zval captures[4];
ZVAL_COPY_VALUE(&captures[0], self);
ZVAL_COPY_VALUE(&captures[1], site);
ZVAL_COPY_VALUE(&captures[2], templates);
ZVAL_COPY_VALUE(&captures[3], constraintsReference);
zv::Val callback = pt_native_closure_new(&replaceInferableTemplatesBody, 4, captures, 1u << 3);
return pt_type_traverser_map_of(type, callback.raw());
}
/* Mirrors observeSend(). */
static zv::Val observeSend(zv::Val constraints, zval *declared, zval *actual, bool isCallArgument)
{
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
bool declaredTemplate;
if (UNEXPECTED(!isTemplateType(declared, declaredTemplate))) return zv::Val();
if (declaredTemplate) return constraints;
bool contains;
if (UNEXPECTED(!containsMarker(actual, contains))) return zv::Val();
if (!contains) return constraints;
if (isCallArgument && isInstance(declared, pt_ce_mixed_type)) {
zv::Val sites = collectSites(actual);
if (UNEXPECTED(sites.isUndef())) return zv::Val();
struct Data
{
zv::Val *constraints;
} data{&constraints};
bool ok = pt_template_argument_constraints_facts(sites.raw(), [](void *raw, zval *fact) -> bool {
Data *d = static_cast<Data *>(raw);
/* foreach (... as [$marker]) */
zval *marker = Z_TYPE_P(fact) == IS_ARRAY ? zend_hash_index_find(Z_ARRVAL_P(fact), 0) : NULL;
zval null;
ZVAL_NULL(&null);
if (marker == NULL) {
if (Z_TYPE_P(fact) == IS_ARRAY) zend_error(E_WARNING, "Undefined array key 0");
marker = &null;
}
ZVAL_DEREF(marker);
zv::Val next = pt_template_argument_constraints_with_unconstraining_send(d->constraints->raw(), marker);
if (UNEXPECTED(next.isUndef())) return false;
*d->constraints = std::move(next);
return true;
}, &data);
if (UNEXPECTED(!ok)) return zv::Val();
return constraints;
}
bool actualUnion = isInstance(actual, pt_ce_union_type);
if (actualUnion) {
zv::Val types = pt_type_op(Z_OBJ_P(actual), PT_OP_GET_TYPES, 0, NULL);
if (UNEXPECTED(types.isUndef())) return zv::Val();
for (auto entry : zv::ArrRef(types.raw())) {
constraints = observeSend(std::move(constraints), declared, entry.value().deref().raw(), isCallArgument);
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
}
return constraints;
}
if (isInstance(declared, pt_ce_union_type)) {
zv::Val types = pt_type_op(Z_OBJ_P(declared), PT_OP_GET_TYPES, 0, NULL);
if (UNEXPECTED(types.isUndef())) return zv::Val();
for (auto entry : zv::ArrRef(types.raw())) {
constraints = observeSend(std::move(constraints), entry.value().deref().raw(), actual, isCallArgument);
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
}
return constraints;
}
if (isMarker(actual)) return constraints;
if (isInstance(actual, pt_ce_never_type)) return constraints;
if (isInstance(declared, pt_ce_object_shape_type)) {
return observeObjectShapeSend(std::move(constraints), declared, actual, isCallArgument);
}
if (isInstance(declared, pt_ce_intersection_type)) {
zv::Val types = pt_type_op(Z_OBJ_P(declared), PT_OP_GET_TYPES, 0, NULL);
if (UNEXPECTED(types.isUndef())) return zv::Val();
for (auto entry : zv::ArrRef(types.raw())) {
zval *member = entry.value().deref().raw();
if (!isInstance(member, pt_ce_object_shape_type)) continue;
constraints = observeObjectShapeSend(std::move(constraints), member, actual, isCallArgument);
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
}
}
zv::Val actualReflections = objectClassReflections(actual);
if (UNEXPECTED(actualReflections.isUndef())) return zv::Val();
uint32_t actualReflectionCount = countOf(actualReflections.raw());
if (actualReflectionCount == 1) {
zv::Val declaredReflections = objectClassReflections(declared);
if (UNEXPECTED(declaredReflections.isUndef())) return zv::Val();
if (countOf(declaredReflections.raw()) != 1) return constraints;
zval *declaredReflection = listItem(declaredReflections.raw(), 0);
zval *actualReflection = listItem(actualReflections.raw(), 0);
if (UNEXPECTED(declaredReflection == NULL || actualReflection == NULL)) return undefinedKeyZero();
zv::Val declaredName = reflectionName(declaredReflection);
if (UNEXPECTED(declaredName.isUndef())) return zv::Val();
zv::Val ancestor = call1(actualReflection, PT_LC("getancestorwithclassname"), "getAncestorWithClassName", declaredName.raw());
if (UNEXPECTED(ancestor.isUndef())) return zv::Val();
if (Z_TYPE_P(ancestor.raw()) == IS_NULL) return constraints;
bool generic;
if (UNEXPECTED(!reflectionIsGeneric(ancestor.raw(), generic))) return zv::Val();
if (!generic) return constraints;
zv::Val templates = reflectionTypeMapList(ancestor.raw(), PT_LC("gettemplatetypemap"), "getTemplateTypeMap");
if (UNEXPECTED(templates.isUndef())) return zv::Val();
zv::Val declaredArguments = reflectionTypeMapList(declaredReflection, PT_LC("getpossiblyincompleteactivetemplatetypemap"), "getPossiblyIncompleteActiveTemplateTypeMap");
if (UNEXPECTED(declaredArguments.isUndef())) return zv::Val();
zv::Val declaredVariances = call0(declaredReflection, PT_LC("getcallsitevariancemap"), "getCallSiteVarianceMap");
if (UNEXPECTED(declaredVariances.isUndef())) return zv::Val();
zv::Val arguments = reflectionTypeMapList(ancestor.raw(), PT_LC("getactivetemplatetypemap"), "getActiveTemplateTypeMap");
if (UNEXPECTED(arguments.isUndef())) return zv::Val();
for (auto entry : zv::ArrRef(arguments.raw())) {
zval *argument = entry.value().deref().raw();
zval *template_ = entry.hasStringKey() ? zend_hash_find(Z_ARRVAL_P(templates.raw()), entry.stringKey()) : listItem(templates.raw(), entry.indexKey());
if (template_ != NULL) {
ZVAL_DEREF(template_);
}
bool templateIsTemplate = false;
if (template_ != NULL && UNEXPECTED(!isTemplateType(template_, templateIsTemplate))) return zv::Val();
zval *declaredArgumentSlot = entry.hasStringKey() ? zend_hash_find(Z_ARRVAL_P(declaredArguments.raw()), entry.stringKey()) : listItem(declaredArguments.raw(), entry.indexKey());
if (declaredArgumentSlot != NULL) {
ZVAL_DEREF(declaredArgumentSlot);
}
if (!templateIsTemplate || declaredArgumentSlot == NULL || Z_TYPE_P(declaredArgumentSlot) == IS_NULL) continue;
/* $declaredVariances->getVariance($template->getName()) ?? TemplateTypeVariance::createInvariant() */
zv::Val templateName = call0(template_, PT_LC("getname"), "getName");
if (UNEXPECTED(templateName.isUndef())) return zv::Val();
zv::Val callSiteVariance = call1(declaredVariances.raw(), PT_LC("getvariance"), "getVariance", templateName.raw());
if (UNEXPECTED(callSiteVariance.isUndef())) return zv::Val();
if (Z_TYPE_P(callSiteVariance.raw()) == IS_NULL) {
zval *invariant = pt_template_type_variance_singleton(PT_TEMPLATE_TYPE_VARIANCE_INVARIANT);
if (UNEXPECTED(invariant == NULL)) return zv::Val();
callSiteVariance = zv::Val::copyOf(zv::Ref(invariant));
}
bool invariant;
if (UNEXPECTED(!varianceIs(callSiteVariance.raw(), PT_TEMPLATE_TYPE_VARIANCE_INVARIANT, invariant))) return zv::Val();
zv::Val effectiveVariance = invariant ? call0(template_, PT_LC("getvariance"), "getVariance") : std::move(callSiteVariance);
if (UNEXPECTED(effectiveVariance.isUndef())) return zv::Val();
constraints = observeArgumentSend(std::move(constraints), declaredArgumentSlot, argument, effectiveVariance.raw(), isCallArgument);
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
}
return constraints;
}
if (actualReflectionCount > 0) return constraints;
bool yes;
if (UNEXPECTED(!trinaryYes(actual, PT_LC("isobject"), yes))) return zv::Val();
if (yes) return constraints;
if (UNEXPECTED(!trinaryYes(actual, PT_LC("isiterable"), yes))) return zv::Val();
if (!yes) return constraints;
if (UNEXPECTED(!trinaryYes(declared, PT_LC("isiterable"), yes))) return zv::Val();
if (!yes) return constraints;
zv::Val declaredKey = pt_type_op(Z_OBJ_P(declared), PT_OP_GET_ITERABLE_KEY_TYPE, 0, NULL);
if (UNEXPECTED(declaredKey.isUndef())) return zv::Val();
bool inert;
if (UNEXPECTED(!isInertSendTarget(declaredKey.raw(), isCallArgument, inert))) return zv::Val();
if (!inert) {
zv::Val actualKey = pt_type_op(Z_OBJ_P(actual), PT_OP_GET_ITERABLE_KEY_TYPE, 0, NULL);
if (UNEXPECTED(actualKey.isUndef())) return zv::Val();
constraints = observeSend(std::move(constraints), declaredKey.raw(), actualKey.raw(), isCallArgument);
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
}
zv::Val declaredValue = pt_type_op(Z_OBJ_P(declared), PT_OP_GET_ITERABLE_VALUE_TYPE, 0, NULL);
if (UNEXPECTED(declaredValue.isUndef())) return zv::Val();
if (UNEXPECTED(!isInertSendTarget(declaredValue.raw(), isCallArgument, inert))) return zv::Val();
if (inert) return constraints;
zv::Val actualValue = pt_type_op(Z_OBJ_P(actual), PT_OP_GET_ITERABLE_VALUE_TYPE, 0, NULL);
if (UNEXPECTED(actualValue.isUndef())) return zv::Val();
return observeSend(std::move(constraints), declaredValue.raw(), actualValue.raw(), isCallArgument);
}
/* Mirrors observeObjectShapeSend(). */
static zv::Val observeObjectShapeSend(zv::Val constraints, zval *declared, zval *actual, bool isCallArgument)
{
bool yes;
if (UNEXPECTED(!trinaryYes(actual, PT_LC("isobject"), yes))) return zv::Val();
if (!yes) return constraints;
zv::Val scope = pt_type_new(PT_CLASS_OUT_OF_CLASS_SCOPE, 0, NULL);
if (UNEXPECTED(scope.isUndef())) return zv::Val();
zv::Val properties = call0(declared, PT_LC("getproperties"), "getProperties");
if (UNEXPECTED(properties.isUndef())) return zv::Val();
if (UNEXPECTED(Z_TYPE_P(properties.raw()) != IS_ARRAY)) {
zend_type_error("phpstan_turbo: getProperties() must return array");
return zv::Val();
}
for (auto entry : zv::ArrRef(properties.raw())) {
/* (string) $name */
zv::Val name = entry.hasStringKey()
? zv::Val::string(entry.stringKey())
: zv::Val::adoptString(zend_long_to_str((zend_long) entry.indexKey()));
zend_long hasProperty = pt_type_call_trinary(Z_OBJ_P(actual), PT_LC("hasinstanceproperty"), 1, name.raw());
if (UNEXPECTED(hasProperty < 0)) return zv::Val();
if (hasProperty != PT_TRI_YES) continue;
zv::Args args{name.raw(), scope.raw()};
zv::Val actualProperty = pt_type_call(Z_OBJ_P(actual), PT_LC("getinstanceproperty"), 2, args);
if (UNEXPECTED(actualProperty.isUndef())) {
zend_class_entry *missingCe = pt_class(PT_CLASS_MISSING_PROPERTY_FROM_REFLECTION_EXCEPTION);
if (EG(exception) == NULL || missingCe == NULL || !instanceof_function(EG(exception)->ce, missingCe)) return zv::Val();
zend_clear_exception();
continue;
}
zv::Val flag = call0(actualProperty.raw(), PT_LC("ispublic"), "isPublic");
if (UNEXPECTED(flag.isUndef())) return zv::Val();
if (!zend_is_true(flag.raw())) continue;
flag = call0(actualProperty.raw(), PT_LC("isstatic"), "isStatic");
if (UNEXPECTED(flag.isUndef())) return zv::Val();
if (zend_is_true(flag.raw())) continue;
zv::Val actualPropertyType = call0(actualProperty.raw(), PT_LC("getreadabletype"), "getReadableType");
if (UNEXPECTED(actualPropertyType.isUndef())) return zv::Val();
constraints = observeSend(std::move(constraints), entry.value().deref().raw(), actualPropertyType.raw(), isCallArgument);
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
}
return constraints;
}
/* Mirrors observeArgumentSend(). */
static zv::Val observeArgumentSend(zv::Val constraints, zval *declaredArgument, zval *argument, zval *variance, bool isCallArgument)
{
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
if (isMarker(argument)) {
bool closureSignature;
if (UNEXPECTED(!pt_unresolved_template_argument_type_is_closure_signature(argument, closureSignature))) return zv::Val();
if (!closureSignature) return observeMarkerSend(std::move(constraints), declaredArgument, argument, variance, isCallArgument);
}
bool argumentTemplate = false;
bool argumentUnion = isInstance(argument, pt_ce_union_type);
if (argumentUnion && UNEXPECTED(!isTemplateType(argument, argumentTemplate))) return zv::Val();
if (!argumentUnion || argumentTemplate) return observeSend(std::move(constraints), declaredArgument, argument, isCallArgument);
bool invariant, covariant = false;
if (UNEXPECTED(!varianceIs(variance, PT_TEMPLATE_TYPE_VARIANCE_INVARIANT, invariant))) return zv::Val();
if (!invariant && UNEXPECTED(!varianceIs(variance, PT_TEMPLATE_TYPE_VARIANCE_COVARIANT, covariant))) return zv::Val();
zval *upperBound = NULL;
if (invariant || covariant) {
upperBound = pt_template_type_variance_singleton(PT_TEMPLATE_TYPE_VARIANCE_COVARIANT);
if (UNEXPECTED(upperBound == NULL)) return zv::Val();
}
zv::Val types = pt_type_op(Z_OBJ_P(argument), PT_OP_GET_TYPES, 0, NULL);
if (UNEXPECTED(types.isUndef())) return zv::Val();
for (auto entry : zv::ArrRef(types.raw())) {
zval *member = entry.value().deref().raw();
bool closureSignature = false;
bool marker = isMarker(member);
if (marker && UNEXPECTED(!pt_unresolved_template_argument_type_is_closure_signature(member, closureSignature))) return zv::Val();
if (!marker || closureSignature) {
constraints = observeSend(std::move(constraints), declaredArgument, member, isCallArgument);
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
continue;
}
if (upperBound == NULL) continue;
constraints = observeMarkerSend(std::move(constraints), declaredArgument, member, upperBound, isCallArgument);
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
}
return constraints;
}
/* Mirrors observeMarkerSend(). */
static zv::Val observeMarkerSend(zv::Val constraints, zval *declaredArgumentIn, zval *marker, zval *variance, bool isCallArgument)
{
zv::Val initial = markerInitialType(marker);
if (UNEXPECTED(initial.isUndef())) return zv::Val();
zv::Val declaredArgument = zv::Val::copyOf(zv::Ref(declaredArgumentIn));
if (isCallArgument && (Z_TYPE_P(initial.raw()) == IS_NULL || isInstance(initial.raw(), pt_ce_never_type))) {
bool onlyInferable;
if (UNEXPECTED(!hasOnlyInferableTemplates(declaredArgument.raw(), onlyInferable))) return zv::Val();
if (onlyInferable) {
declaredArgument = pt_type_call_static_ce(pt_ce_template_type_helper, PT_LC("resolvetodefaults"), 1, declaredArgument.raw());
if (UNEXPECTED(declaredArgument.isUndef())) return zv::Val();
}
}
bool uninformative;
if (UNEXPECTED(!isUninformativeSendTarget(declaredArgument.raw(), uninformative))) return zv::Val();
if (uninformative) {
// An unresolved call parameter, like mixed, uses the object without
// constraining it. Return/property templates are fixed by their
// declaration and must keep an empty argument compatible with them.
bool unconstraining = false;
if (isCallArgument) {
if (UNEXPECTED(!hasOnlyInferableTemplates(declaredArgument.raw(), unconstraining))) return zv::Val();
}
if (!unconstraining) {
if (UNEXPECTED(!isPlainMixed(declaredArgument.raw(), unconstraining))) return zv::Val();
}
if (unconstraining) {
return pt_template_argument_constraints_with_unconstraining_send(constraints.raw(), marker);
}
return constraints;
}
constraints = pt_template_argument_constraints_with_send(constraints.raw(), marker, declaredArgument.raw(), variance);
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
if (Z_TYPE_P(initial.raw()) == IS_NULL) return constraints;
// a site whose inferred argument itself carries markers: nested in objects
// (wrap(new Foo(1))), or bare as the sites it was inferred from
// (collect(of(1), of(2)) returning Coll<U>) - an upper bound of the site
// bounds those from above too
return observeArgumentSend(std::move(constraints), declaredArgument.raw(), initial.raw(), variance, isCallArgument);
}
/* Mirrors isInertSendTarget(); false = pending exception */
[[nodiscard]] static bool isInertSendTarget(zval *declared, bool isCallArgument, bool &out)
{
if (isCallArgument) {
out = false;
return true;
}
return isPlainMixed(declared, out);
}