Repository navigation
Expand file tree
/
Copy pathCalledOnTypeUnresolvedPropertyPrototypeReflection.cpp
More file actions
292 lines (259 loc) · 15 KB
/
Copy pathCalledOnTypeUnresolvedPropertyPrototypeReflection.cpp
File metadata and controls
292 lines (259 loc) · 15 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
/*
* PHPStanTurbo\CalledOnTypeUnresolvedPropertyPrototypeReflection — native
* implementation of PHPStan\Reflection\Type\CalledOnTypeUnresolvedPropertyPrototypeReflection.
*
* Declared under the twin's real name at activation (final, like the
* twin): the lazy property prototype ObjectType builds, whose
* getTransformedProperty() rewrites `static` in the property's types to
* the fetched-on type. The logic lives in the handle class below,
* mirroring src/Reflection/Type/CalledOnTypeUnresolvedPropertyPrototypeReflection.php
* method for method; transformPropertyWithStaticType() is the shared
* pt_prototype_resolved_property() (TypeTraits.cpp), and the
* transformStaticType() closure runs as a native TypeTraverser callback.
* State lives in the six declared property slots — the twin's, in its
* order; the standard object handlers do GC/free/clone.
*/
#include "support.h"
#include "generated/CalledOnTypeUnresolvedPropertyPrototypeReflection.h"
namespace slots = ptdecl::CalledOnTypeUnresolvedPropertyPrototypeReflection::slot;
namespace sigs = ptdecl::CalledOnTypeUnresolvedPropertyPrototypeReflection::sig;
#include "zv.h"
#include "TypeTraits.h"
zend_class_entry *pt_ce_called_on_type_unresolved_property_prototype_reflection = NULL;
namespace phpstanturbo {
/* Mirrors PHPStan\Reflection\Type\CalledOnTypeUnresolvedPropertyPrototypeReflection.
* State lives in the PHP object's $transformedProperty,
* $cachedDoNotResolveTemplateTypeMapToBounds and the promoted
* $propertyReflection, $resolvedDeclaringClass, $resolveTemplateTypeMapToBounds,
* $fetchedOnType. */
class CalledOnTypeUnresolvedPropertyPrototypeReflection
{
public:
explicit CalledOnTypeUnresolvedPropertyPrototypeReflection(zend_object *self) : self(self) {}
/* __construct(private ExtendedPropertyReflection $propertyReflection,
* private ClassReflection $resolvedDeclaringClass, private bool
* $resolveTemplateTypeMapToBounds, private Type $fetchedOnType) */
void construct(zval *propertyReflection, zval *resolvedDeclaringClass, bool resolveTemplateTypeMapToBounds, zval *fetchedOnType) const
{
write(slots::propertyReflection, zv::Val::copyOf(zv::Ref(propertyReflection)));
write(slots::resolvedDeclaringClass, zv::Val::copyOf(zv::Ref(resolvedDeclaringClass)));
write(slots::resolveTemplateTypeMapToBounds, zv::Val::boolean(resolveTemplateTypeMapToBounds));
write(slots::fetchedOnType, zv::Val::copyOf(zv::Ref(fetchedOnType)));
}
/* new self(...); UNDEF = pending exception */
static zv::Val create(zval *propertyReflection, zval *resolvedDeclaringClass, bool resolveTemplateTypeMapToBounds, zval *fetchedOnType)
{
zval object;
if (UNEXPECTED(object_init_ex(&object, pt_ce_called_on_type_unresolved_property_prototype_reflection) != SUCCESS)) return zv::Val();
CalledOnTypeUnresolvedPropertyPrototypeReflection(Z_OBJ(object)).construct(propertyReflection, resolvedDeclaringClass, resolveTemplateTypeMapToBounds, fetchedOnType);
return zv::Val::adopt(object);
}
/* doNotResolveTemplateTypeMapToBounds(): the memoized copy with
* $resolveTemplateTypeMapToBounds = false; UNDEF = pending exception */
zv::Val doNotResolveTemplateTypeMapToBounds() const
{
zval *cached = OBJ_PROP_NUM(self, slots::cachedDoNotResolveTemplateTypeMapToBounds);
if (Z_TYPE_P(cached) == IS_OBJECT) return zv::Val::copyOf(zv::Ref(cached));
zval *propertyReflection = slot(slots::propertyReflection, "propertyReflection");
zval *resolvedDeclaringClass = propertyReflection != NULL ? slot(slots::resolvedDeclaringClass, "resolvedDeclaringClass") : NULL;
zval *fetchedOnType = resolvedDeclaringClass != NULL ? slot(slots::fetchedOnType, "fetchedOnType") : NULL;
if (UNEXPECTED(fetchedOnType == NULL)) return zv::Val();
zv::Val created = create(propertyReflection, resolvedDeclaringClass, false, fetchedOnType);
if (UNEXPECTED(created.isUndef())) return zv::Val();
write(slots::cachedDoNotResolveTemplateTypeMapToBounds, zv::Val::copyOf(zv::Ref(created.raw())));
return created;
}
/* getNakedProperty(): $this->propertyReflection; UNDEF = pending exception */
zv::Val getNakedProperty() const
{
zval *propertyReflection = slot(slots::propertyReflection, "propertyReflection");
return propertyReflection != NULL ? zv::Val::copyOf(zv::Ref(propertyReflection)) : zv::Val();
}
/* getTransformedProperty(): the memo, computed once through
* pt_prototype_resolved_property(); UNDEF = pending exception */
zv::Val getTransformedProperty() const
{
zval *transformedProperty = OBJ_PROP_NUM(self, slots::transformedProperty);
if (EXPECTED(Z_TYPE_P(transformedProperty) == IS_OBJECT)) return zv::Val::copyOf(zv::Ref(transformedProperty));
zval *resolvedDeclaringClass = slot(slots::resolvedDeclaringClass, "resolvedDeclaringClass");
zval *propertyReflection = resolvedDeclaringClass != NULL ? slot(slots::propertyReflection, "propertyReflection") : NULL;
zval *resolveToBounds = propertyReflection != NULL ? slot(slots::resolveTemplateTypeMapToBounds, "resolveTemplateTypeMapToBounds") : NULL;
if (UNEXPECTED(resolveToBounds == NULL)) return zv::Val();
PrototypeTransformer transformer = { transformStaticType, self };
zv::Val result = pt_prototype_resolved_property(PT_PROTOTYPE_CALLED_ON_TYPE, transformer, Z_OBJ_P(resolvedDeclaringClass), Z_OBJ_P(propertyReflection), zend_is_true(resolveToBounds));
if (UNEXPECTED(result.isUndef())) return zv::Val();
write(slots::transformedProperty, zv::Val::copyOf(zv::Ref(result.raw())));
return result;
}
/* withFechedOnType(Type $type): a copy over $type; UNDEF = pending exception */
zv::Val withFechedOnType(zval *type) const
{
zval *propertyReflection = slot(slots::propertyReflection, "propertyReflection");
zval *resolvedDeclaringClass = propertyReflection != NULL ? slot(slots::resolvedDeclaringClass, "resolvedDeclaringClass") : NULL;
zval *resolveToBounds = resolvedDeclaringClass != NULL ? slot(slots::resolveTemplateTypeMapToBounds, "resolveTemplateTypeMapToBounds") : NULL;
if (UNEXPECTED(resolveToBounds == NULL)) return zv::Val();
return create(propertyReflection, resolvedDeclaringClass, zend_is_true(resolveToBounds), type);
}
private:
zend_object *self;
void write(uint32_t index, zv::Val value) const
{
zval *slot = OBJ_PROP_NUM(self, index);
zval previous;
ZVAL_COPY_VALUE(&previous, slot);
zval v = value.take();
ZVAL_COPY_VALUE(slot, &v);
Z_PROP_FLAG_P(slot) = 0; /* no longer IS_PROP_UNINIT */
if (Z_TYPE(previous) != IS_UNDEF) {
zval_ptr_dtor(&previous);
}
}
zval *slot(uint32_t index, const char *propertyName) const
{
zval *value = OBJ_PROP_NUM(self, index);
if (UNEXPECTED(Z_TYPE_P(value) == IS_UNDEF)) {
zend_throw_error(NULL, "Typed property %s::$%s must not be accessed before initialization", ZSTR_VAL(pt_ce_called_on_type_unresolved_property_prototype_reflection->name), propertyName);
return NULL;
}
return value;
}
/* transformStaticType($type): TypeTraverser::map($type, the closure
* below over $this); UNDEF = pending exception */
static zv::Val transformStaticType(void *context, zval *type)
{
zval thisZv;
ZVAL_OBJ(&thisZv, (zend_object *) context);
zv::Val callback = pt_type_native_callback(traverse, &thisZv, NULL);
if (UNEXPECTED(callback.isUndef())) return zv::Val();
return pt_type_traverser_map_of(type, callback.raw());
}
/* the closure of transformStaticType(): `function (Type $type, callable
* $traverse): Type` — a GenericStaticType becomes the static object
* type of its base class changed to the single fetched-on class
* (traversed on), or the fetched-on type itself; any other StaticType
* the fetched-on type; everything else traverses */
static void traverse(zval *thisZv, zval *state1, uint32_t argc, zval *argv, zval *return_value)
{
(void) state1;
if (UNEXPECTED(argc < 2)) {
zend_argument_count_error("Too few arguments to function %s::{closure}(), %u passed and exactly 2 expected", ZSTR_VAL(pt_ce_called_on_type_unresolved_property_prototype_reflection->name), argc);
return;
}
if (UNEXPECTED(Z_TYPE(argv[0]) != IS_OBJECT)) {
zend_type_error("%s::{closure}(): Argument #1 ($type) must be of type %s, %s given", ZSTR_VAL(pt_ce_called_on_type_unresolved_property_prototype_reflection->name), ptcls::type, zend_zval_value_name(&argv[0]));
return;
}
zval *type = &argv[0];
bool isGenericStatic;
pt_type_instanceof_ce(type, pt_ce_generic_static_type, isGenericStatic);
if (isGenericStatic) {
zval *fetchedOnType = CalledOnTypeUnresolvedPropertyPrototypeReflection(Z_OBJ_P(thisZv)).slot(slots::fetchedOnType, "fetchedOnType");
if (UNEXPECTED(fetchedOnType == NULL)) return;
/* $fetchedOnTypeReflections = $this->fetchedOnType->getObjectClassReflections(); */
zv::Val reflections = pt_type_call(Z_OBJ_P(fetchedOnType), PT_LC("getobjectclassreflections"), 0, NULL);
if (UNEXPECTED(reflections.isUndef())) return;
if (UNEXPECTED(!zv::Ref(reflections.raw()).isArray())) {
zend_type_error("phpstan_turbo: getObjectClassReflections() must return an array");
return;
}
if (zend_hash_num_elements(Z_ARRVAL_P(reflections.raw())) == 1) {
/* $type->changeBaseClass($fetchedOnTypeReflections[0])->getStaticObjectType() */
zval *first = zend_hash_index_find(Z_ARRVAL_P(reflections.raw()), 0);
if (UNEXPECTED(first == NULL)) {
zend_throw_error(NULL, "phpstan_turbo: getObjectClassReflections() must return a list");
return;
}
ZVAL_DEREF(first);
zv::Val changed = pt_type_call(Z_OBJ_P(type), PT_LC("changebaseclass"), 1, first);
if (UNEXPECTED(changed.isUndef())) return;
if (UNEXPECTED(!zv::Ref(changed.raw()).isObject())) {
zend_type_error("phpstan_turbo: changeBaseClass() must return an object");
return;
}
zv::Val staticObjectType = pt_type_call(Z_OBJ_P(changed.raw()), PT_LC("getstaticobjecttype"), 0, NULL);
if (UNEXPECTED(staticObjectType.isUndef())) return;
/* a GenericObjectType is rebuilt without the class reflection of the
* declaring class, which would not know the resolved type arguments:
* new GenericObjectType($t->getClassName(), $t->getTypes(), $t->getSubtractedType(), variances: $t->getVariances()) */
bool isGenericObject;
pt_type_instanceof_ce(staticObjectType.raw(), pt_ce_generic_object_type, isGenericObject);
if (isGenericObject) {
zend_object *objectType = Z_OBJ_P(staticObjectType.raw());
zv::Val className = pt_type_call(objectType, PT_LC("getclassname"), 0, NULL);
if (UNEXPECTED(className.isUndef())) return;
zv::Val objectTypes = pt_type_call(objectType, PT_LC("gettypes"), 0, NULL);
if (UNEXPECTED(objectTypes.isUndef())) return;
zv::Val subtracted = pt_type_call(objectType, PT_LC("getsubtractedtype"), 0, NULL);
if (UNEXPECTED(subtracted.isUndef())) return;
zv::Val objectVariances = pt_type_call(objectType, PT_LC("getvariances"), 0, NULL);
if (UNEXPECTED(objectVariances.isUndef())) return;
if (UNEXPECTED(Z_TYPE_P(className.raw()) != IS_STRING)) {
zend_type_error("phpstan_turbo: getClassName() must return a string");
return;
}
zval rebuiltRaw;
if (UNEXPECTED(!pt_generic_object_type_new(&rebuiltRaw, Z_STR_P(className.raw()), objectTypes.raw(), subtracted.raw(), NULL, objectVariances.raw()))) return;
staticObjectType = zv::Val::adopt(rebuiltRaw);
}
zv::Val traversed = pt_type_call_callable(&argv[1], 1, staticObjectType.raw());
if (UNEXPECTED(traversed.isUndef())) return;
traversed.intoReturnValue(return_value);
return;
}
ZVAL_COPY(return_value, fetchedOnType);
return;
}
bool isStatic;
pt_type_instanceof_ce(type, pt_ce_static_type, isStatic);
if (isStatic) {
zval *fetchedOnType = CalledOnTypeUnresolvedPropertyPrototypeReflection(Z_OBJ_P(thisZv)).slot(slots::fetchedOnType, "fetchedOnType");
if (UNEXPECTED(fetchedOnType == NULL)) return;
ZVAL_COPY(return_value, fetchedOnType);
return;
}
zv::Val traversed = pt_type_call_callable(&argv[1], 1, type);
if (UNEXPECTED(traversed.isUndef())) return;
traversed.intoReturnValue(return_value);
}
};
} // namespace phpstanturbo
using phpstanturbo::CalledOnTypeUnresolvedPropertyPrototypeReflection;
/* {{{ exported helpers: the shadowing class for native callers */
/* new CalledOnTypeUnresolvedPropertyPrototypeReflection(...$argv) over
* values as PHP code hands them (borrowed): directly when they already
* have the parameter types, through the constructor's parameter parsing
* otherwise; UNDEF = pending exception */
zv::Val pt_called_on_type_unresolved_property_prototype_reflection_new(uint32_t argc, zval *argv)
{
if (EXPECTED(argc == 4 && Z_TYPE(argv[0]) == IS_OBJECT && Z_TYPE(argv[1]) == IS_OBJECT && (Z_TYPE(argv[2]) == IS_TRUE || Z_TYPE(argv[2]) == IS_FALSE) && Z_TYPE(argv[3]) == IS_OBJECT)) {
return CalledOnTypeUnresolvedPropertyPrototypeReflection::create(&argv[0], &argv[1], Z_TYPE(argv[2]) == IS_TRUE, &argv[3]);
}
return pt_type_new_ce(pt_ce_called_on_type_unresolved_property_prototype_reflection, argc, argv);
}
/* }}} */
/* {{{ engine ABI glue: parameter parsing + registration */
#include "reg.h"
#define PT_THIS CalledOnTypeUnresolvedPropertyPrototypeReflection(Z_OBJ_P(ZEND_THIS))
PT_MINIT_REGISTRATION(pt_register_called_on_type_unresolved_property_prototype_reflection)
{
static const char *selfClass = "PHPStan\\Reflection\\Type\\CalledOnTypeUnresolvedPropertyPrototypeReflection";
static const char *extendedPropertyReflectionClass = "PHPStan\\Reflection\\ExtendedPropertyReflection";
reg::Class cls("PHPStan\\Reflection\\Type\\CalledOnTypeUnresolvedPropertyPrototypeReflection");
ptdecl::CalledOnTypeUnresolvedPropertyPrototypeReflection::declareClass(cls);
cls.privateTypedClassPropertyDefaultNull("transformedProperty", extendedPropertyReflectionClass);
cls.privateTypedClassPropertyDefaultNull("cachedDoNotResolveTemplateTypeMapToBounds", selfClass);
cls.privateTypedClassProperty("propertyReflection", extendedPropertyReflectionClass, false);
cls.privateTypedClassProperty("resolvedDeclaringClass", ptcls::classReflection, false);
cls.privateTypedProperty("resolveTemplateTypeMapToBounds", MAY_BE_BOOL);
cls.privateTypedClassProperty("fetchedOnType", ptcls::type, false);
cls.method<&CalledOnTypeUnresolvedPropertyPrototypeReflection::construct, zp::Obj, zp::Obj, zp::Bool, zp::Obj>(sigs::__construct);
cls.method<&CalledOnTypeUnresolvedPropertyPrototypeReflection::doNotResolveTemplateTypeMapToBounds>(sigs::doNotResolveTemplateTypeMapToBounds);
cls.op<PT_OP_DO_NOT_RESOLVE_TEMPLATE_TYPE_MAP_TO_BOUNDS, &CalledOnTypeUnresolvedPropertyPrototypeReflection::doNotResolveTemplateTypeMapToBounds>();
cls.method<&CalledOnTypeUnresolvedPropertyPrototypeReflection::getNakedProperty>(sigs::getNakedProperty);
cls.op<PT_OP_GET_NAKED_PROPERTY, &CalledOnTypeUnresolvedPropertyPrototypeReflection::getNakedProperty>();
cls.method<&CalledOnTypeUnresolvedPropertyPrototypeReflection::getTransformedProperty>(sigs::getTransformedProperty);
cls.op<PT_OP_GET_TRANSFORMED_PROPERTY, &CalledOnTypeUnresolvedPropertyPrototypeReflection::getTransformedProperty>();
cls.method<&CalledOnTypeUnresolvedPropertyPrototypeReflection::withFechedOnType, zp::Obj>(sigs::withFechedOnType);
cls.shadow(&pt_ce_called_on_type_unresolved_property_prototype_reflection);
}
/* }}} */