Repository navigation
Expand file tree
/
Copy pathStaticVariableHandler.cpp
More file actions
331 lines (294 loc) · 15.4 KB
/
Copy pathStaticVariableHandler.cpp
File metadata and controls
331 lines (294 loc) · 15.4 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
/*
* PHPStanTurbo\StaticVariableHandler — native implementation of
* PHPStan\Analyser\StmtHandler\StaticVariableHandler.
*
* A DI service (#[AutowiredService]): the constructor keeps the twin's
* arginfo so Nette autowires it. processStmt() is registered as the class's
* statement-handler entry (Engine.h).
*
* ImpurePoint, ExpressionResult, VariableFlow, MixedType, TrinaryLogic,
* MutatingScope, VarAnnotationProcessor, the contexts, the statement results
* and NodeScopeResolver are called through their direct entries.
*/
#include "support.h"
#include "generated/StaticVariableHandler.h"
namespace slots = ptdecl::StaticVariableHandler::slot;
namespace sigs = ptdecl::StaticVariableHandler::sig;
#include "zv.h"
#include "TypeTraits.h"
#include "Engine.h"
#include "AnalyserValues.h"
#include "StmtHandlerCalls.h"
zend_class_entry *pt_ce_static_variable_handler = nullptr;
namespace {
pt_property_site pt_svh_vars_site;
pt_property_site pt_svh_static_var_var_site;
pt_property_site pt_svh_static_var_default_site;
pt_property_site pt_svh_variable_name_site;
/* the impure point's literals, permanent interned strings (module startup) */
zend_string *pt_svh_static = nullptr;
zend_string *pt_svh_static_variable = nullptr;
/* $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); false = pending exception */
[[nodiscard]] bool mergeImpurePoints(zv::Arr &impurePoints, zval *result)
{
zv::Val hold;
zval *resultImpurePoints = pt_expression_result_impure_points(result, hold);
return resultImpurePoints != NULL && pt_callable_array_merge_into(impurePoints, resultImpurePoints);
}
/* $dependencies[] = $result->getDependencies(); false = pending exception */
[[nodiscard]] bool collectDependencies(zv::Arr &dependencies, zval *result)
{
zv::Val hold;
zval *resultDependencies = pt_expression_result_dependencies(result, hold);
if (UNEXPECTED(resultDependencies == NULL)) return false;
dependencies.push(zv::Ref(resultDependencies));
return true;
}
} // namespace
namespace phpstanturbo {
/* Mirrors PHPStan\Analyser\StmtHandler\StaticVariableHandler; UNDEF =
* pending exception. */
class StaticVariableHandler
{
public:
explicit StaticVariableHandler(zend_object *self) : self(self) {}
/* the constructor body: the promoted properties */
void construct(zval *varAnnotationProcessor, zval *staticVariableInference)
{
zv::ObjRef(self).propAtWrite(slots::varAnnotationProcessor, zv::Val::copyOf(zv::Ref(varAnnotationProcessor)));
zv::ObjRef(self).propAtWrite(slots::staticVariableInference, zv::Val::copyOf(zv::Ref(staticVariableInference)));
}
/* Mirrors supports(); false = pending exception */
[[nodiscard]] bool supports(zval *stmt, bool &out) const
{
bool error = false;
out = ptsh::isInstanceOf(stmt, PT_CLASS_STATIC_STMT, error);
return !error;
}
/* Mirrors processStmt(). */
zv::Val processStmt(zval *nodeScopeResolver, zval *stmt, zval *scope, zval *storage, zval *nodeCallback, zval *context) const
{
zv::Val scopeHold = zv::Val::copyOf(zv::Ref(scope));
zv::Arr impurePoints = zv::Arr::create(1);
{
zv::Val impurePoint = pt_impure_point_new(scope, stmt, pt_svh_static, pt_svh_static_variable, true);
if (UNEXPECTED(impurePoint.isUndef())) return zv::Val();
impurePoints.push(std::move(impurePoint));
}
zv::Arr vars = zv::Arr::empty();
zv::Arr variableFlows = zv::Arr::empty();
zv::Arr dependencies = zv::Arr::empty();
zval *stmtVars = ptsh::readNodeProperty(pt_svh_vars_site, stmt, PT_LC("vars"));
if (UNEXPECTED(stmtVars == NULL)) return zv::Val();
if (UNEXPECTED(Z_TYPE_P(stmtVars) != IS_ARRAY)) {
zend_error(E_WARNING, "foreach() argument must be of type array|object, %s given", zend_zval_value_name(stmtVars));
if (UNEXPECTED(EG(exception))) return zv::Val();
} else {
/* foreach iterates the array it started with */
zv::Val iterated = zv::Val::copyOf(zv::Ref(stmtVars));
for (auto entry : zv::ArrRef(iterated.raw())) {
if (UNEXPECTED(!processVar(nodeScopeResolver, stmt, entry.value().deref().raw(), scopeHold, storage, nodeCallback, context, impurePoints, vars, variableFlows, dependencies))) return zv::Val();
}
}
zv::Val annotatedScope = pt_var_annotation_processor_process_var_annotation(OBJ_PROP_NUM(self, slots::varAnnotationProcessor), scopeHold.raw(), vars.raw(), stmt, NULL);
if (UNEXPECTED(annotatedScope.isUndef())) return zv::Val();
// how the types of a run of `static` variables depend on each other -
// see StaticVariableInference::getRuns()
zv::Val conditionalExpressions = pt_static_variable_inference_get_resolved_conditional_expressions(OBJ_PROP_NUM(self, slots::staticVariableInference), annotatedScope.raw(), stmt);
if (UNEXPECTED(conditionalExpressions.isUndef())) return zv::Val();
for (auto entry : zv::ArrRef(conditionalExpressions.raw())) {
zend_string *exprString = entry.stringKeyOrNull();
if (UNEXPECTED(exprString == NULL)) continue;
zv::Val added = pt_mutating_scope_add_conditional_expressions(Z_OBJ_P(annotatedScope.raw()), exprString, Z_ARRVAL_P(entry.value().deref().raw()));
if (UNEXPECTED(added.isUndef())) return zv::Val();
annotatedScope = std::move(added);
}
zv::Val variableFlow = pt_variable_flow_sequence_list(variableFlows.table());
if (UNEXPECTED(variableFlow.isUndef())) return zv::Val();
zv::Val mergedDependencies = pt_dependencies_merge_list(dependencies.table());
if (UNEXPECTED(mergedDependencies.isUndef())) return zv::Val();
zval emptyArray;
ZVAL_EMPTY_ARRAY(&emptyArray);
return pt_internal_statement_result_new(annotatedScope.raw(), false, false, &emptyArray, &emptyArray, impurePoints.raw(), NULL, variableFlow.raw(), -1, mergedDependencies.raw());
}
/* the statement-handler entry (Engine.h) */
static zv::Val processStmtEntry(zend_object *handler, zval *nodeScopeResolver, zval *stmt, zval *scope, zval *storage, zval *nodeCallback, zval *context)
{
return StaticVariableHandler(handler).processStmt(nodeScopeResolver, stmt, scope, storage, nodeCallback, context);
}
private:
zend_object *self;
/* ExpressionContext::createDeep($context->shouldResolveTemplateArguments()) */
static zv::Val deepContext(zval *context)
{
bool resolveTemplateArguments;
if (UNEXPECTED(!pt_statement_context_should_resolve_template_arguments(context, resolveTemplateArguments))) return zv::Val();
return pt_expression_context_create_deep(resolveTemplateArguments);
}
/* the loop body over one static variable; false = pending exception */
[[nodiscard]] bool processVar(zval *nodeScopeResolver, zval *stmt, zval *staticVar, zv::Val &scope, zval *storage, zval *nodeCallback, zval *context, zv::Arr &impurePoints, zv::Arr &vars, zv::Arr &variableFlows, zv::Arr &dependencies) const
{
if (UNEXPECTED(Z_TYPE_P(staticVar) != IS_OBJECT)) {
zend_error(E_WARNING, "Attempt to read property \"var\" on %s", zend_zval_value_name(staticVar));
if (UNEXPECTED(EG(exception))) return false;
zend_error(E_WARNING, "Attempt to read property \"name\" on null");
if (UNEXPECTED(EG(exception))) return false;
pt_throw_should_not_happen();
return false;
}
zval *var = ptsh::readNodeProperty(pt_svh_static_var_var_site, staticVar, PT_LC("var"));
if (UNEXPECTED(var == NULL)) return false;
zval *name;
if (UNEXPECTED(Z_TYPE_P(var) != IS_OBJECT)) {
zend_error(E_WARNING, "Attempt to read property \"name\" on %s", zend_zval_value_name(var));
if (UNEXPECTED(EG(exception))) return false;
name = &EG(uninitialized_zval);
} else {
name = ptsh::readNodeProperty(pt_svh_variable_name_site, var, PT_LC("name"));
if (UNEXPECTED(name == NULL)) return false;
}
if (Z_TYPE_P(name) != IS_STRING) {
pt_throw_should_not_happen();
return false;
}
zval *defaultValue = ptsh::readNodeProperty(pt_svh_static_var_default_site, staticVar, PT_LC("default"));
if (UNEXPECTED(defaultValue == NULL)) return false;
zv::Val defaultExprResult = zv::Val::null();
if (Z_TYPE_P(defaultValue) != IS_NULL) {
zv::Val defaultHold = zv::Val::copyOf(zv::Ref(defaultValue));
zv::Val expressionContext = deepContext(context);
if (UNEXPECTED(expressionContext.isUndef())) return false;
defaultExprResult = pt_node_scope_resolver_process_expr_node(nodeScopeResolver, stmt, defaultHold.raw(), scope.raw(), storage, nodeCallback, expressionContext.raw());
if (UNEXPECTED(defaultExprResult.isUndef())) return false;
if (UNEXPECTED(!collectDependencies(dependencies, defaultExprResult.raw()))) return false;
zv::Val variableFlow = pt_expression_result_variable_flow(defaultExprResult.raw());
if (UNEXPECTED(variableFlow.isUndef())) return false;
variableFlows.push(std::move(variableFlow));
if (UNEXPECTED(!mergeImpurePoints(impurePoints, defaultExprResult.raw()))) return false;
/* $scope = $scope->addTemplateArgumentConstraints($defaultExprResult->getScope()->getTemplateArgumentConstraints()); */
zv::Val resultScopeHold;
zval *resultScope = pt_expression_result_scope(defaultExprResult.raw(), resultScopeHold);
if (UNEXPECTED(resultScope == NULL)) return false;
if (UNEXPECTED(Z_TYPE_P(resultScope) != IS_OBJECT)) {
zend_throw_error(NULL, "Call to a member function getTemplateArgumentConstraints() on %s", zend_zval_value_name(resultScope));
return false;
}
zv::Val constraints = pt_mutating_scope_get_template_argument_constraints(Z_OBJ_P(resultScope));
if (UNEXPECTED(constraints.isUndef())) return false;
zv::Val constrainedScope = pt_mutating_scope_add_template_argument_constraints(Z_OBJ_P(scope.raw()), constraints.raw());
if (UNEXPECTED(constrainedScope.isUndef())) return false;
scope = std::move(constrainedScope);
}
var = ptsh::readNodeProperty(pt_svh_static_var_var_site, staticVar, PT_LC("var"));
if (UNEXPECTED(var == NULL)) return false;
zv::Val varHold = zv::Val::copyOf(zv::Ref(var));
if (UNEXPECTED(Z_TYPE_P(varHold.raw()) != IS_OBJECT)) {
zend_type_error("PHPStan\\Analyser\\MutatingScope::enterExpressionAssign(): Argument #1 ($expr) must be of type PhpParser\\Node\\Expr, %s given", zend_zval_value_name(varHold.raw()));
return false;
}
zv::Val assignScope = pt_mutating_scope_enter_expression_assign(Z_OBJ_P(scope.raw()), Z_OBJ_P(varHold.raw()), true);
if (UNEXPECTED(assignScope.isUndef())) return false;
scope = std::move(assignScope);
{
zval *varName = ptsh::readNodeProperty(pt_svh_variable_name_site, varHold.raw(), PT_LC("name"));
if (UNEXPECTED(varName == NULL)) return false;
if (UNEXPECTED(Z_TYPE_P(varName) != IS_STRING)) {
zend_type_error("PHPStan\\Analyser\\VariableFlow::escape(): Argument #1 ($name) must be of type string, %s given", zend_zval_value_name(varName));
return false;
}
zv::Val escape = pt_variable_flow_escape(Z_STR_P(varName));
if (UNEXPECTED(escape.isUndef())) return false;
variableFlows.push(std::move(escape));
}
{
zv::Val expressionContext = deepContext(context);
if (UNEXPECTED(expressionContext.isUndef())) return false;
zv::Val varResult = pt_node_scope_resolver_process_expr_node(nodeScopeResolver, stmt, varHold.raw(), scope.raw(), storage, nodeCallback, expressionContext.raw());
if (UNEXPECTED(varResult.isUndef())) return false;
if (UNEXPECTED(!collectDependencies(dependencies, varResult.raw()))) return false;
if (UNEXPECTED(!mergeImpurePoints(impurePoints, varResult.raw()))) return false;
}
zv::Val exitScope = pt_mutating_scope_exit_expression_assign(Z_OBJ_P(scope.raw()), Z_OBJ_P(varHold.raw()));
if (UNEXPECTED(exitScope.isUndef())) return false;
scope = std::move(exitScope);
zval *varName = ptsh::readNodeProperty(pt_svh_variable_name_site, varHold.raw(), PT_LC("name"));
if (UNEXPECTED(varName == NULL)) return false;
if (UNEXPECTED(Z_TYPE_P(varName) != IS_STRING)) {
zend_type_error("PHPStan\\Analyser\\MutatingScope::assignVariable(): Argument #1 ($variableName) must be of type string, %s given", zend_zval_value_name(varName));
return false;
}
zv::Val nameHold = zv::Val::copyOf(zv::Ref(varName));
// the type the previous calls may have left - see StaticVariableInference
zval *inference = OBJ_PROP_NUM(self, slots::staticVariableInference);
zv::Val type, nativeType;
zv::Val types = pt_static_variable_inference_get_resolved_types(inference, scope.raw(), varHold.raw());
if (UNEXPECTED(types.isUndef())) return false;
if (types.ref().isArray()) {
type = zv::Val::copyOf(zv::Ref(zend_hash_index_find(Z_ARRVAL_P(types.raw()), 0)));
nativeType = zv::Val::copyOf(zv::Ref(zend_hash_index_find(Z_ARRVAL_P(types.raw()), 1)));
} else {
bool inferred;
if (UNEXPECTED(!pt_static_variable_inference_is_inferred(inference, scope.raw(), varHold.raw(), inferred))) return false;
if (inferred && !defaultExprResult.isNull()) {
type = pt_expression_result_get_type(defaultExprResult.raw());
if (UNEXPECTED(type.isUndef())) return false;
nativeType = pt_expression_result_get_native_type(defaultExprResult.raw());
if (UNEXPECTED(nativeType.isUndef())) return false;
} else if (inferred) {
zval nullType, nativeNullType;
if (UNEXPECTED(!pt_null_type_new(&nullType))) return false;
type = zv::Val::adopt(nullType);
if (UNEXPECTED(!pt_null_type_new(&nativeNullType))) return false;
nativeType = zv::Val::adopt(nativeNullType);
} else {
zval mixed, nativeMixed;
if (UNEXPECTED(!pt_mixed_type_new(&mixed))) return false;
type = zv::Val::adopt(mixed);
if (UNEXPECTED(!pt_mixed_type_new(&nativeMixed))) return false;
nativeType = zv::Val::adopt(nativeMixed);
}
}
zv::Val assigned = pt_mutating_scope_assign_variable(Z_OBJ_P(scope.raw()), Z_STR_P(nameHold.raw()), type.raw(), nativeType.raw(), pt_trinary_singleton(PT_TRI_YES));
if (UNEXPECTED(assigned.isUndef())) return false;
scope = std::move(assigned);
zval *pushedName = ptsh::readNodeProperty(pt_svh_variable_name_site, varHold.raw(), PT_LC("name"));
if (UNEXPECTED(pushedName == NULL)) return false;
vars.push(zv::Ref(pushedName));
return true;
}
};
} // namespace phpstanturbo
using phpstanturbo::StaticVariableHandler;
/* {{{ engine ABI glue: parameter parsing + registration */
#include "reg.h"
PT_MINIT_REGISTRATION(pt_register_static_variable_handler)
{
pt_svh_static = zend_string_init_interned(PT_LC("static"), 1);
pt_svh_static_variable = zend_string_init_interned(PT_LC("static variable"), 1);
reg::Class cls("PHPStan\\Analyser\\StmtHandler\\StaticVariableHandler");
ptdecl::StaticVariableHandler::declareClass(cls);
ptdecl::StaticVariableHandler::declareProperties(cls);
/* the real parameter class names: the DI container autowires the
* service by reflecting the constructor */
cls.method(sigs::__construct, [](INTERNAL_FUNCTION_PARAMETERS) {
zval *varAnnotationProcessor, *staticVariableInference;
if (!zp::parse<zp::Obj, zp::Obj>(execute_data, varAnnotationProcessor, staticVariableInference)) RETURN_THROWS();
StaticVariableHandler(Z_OBJ_P(ZEND_THIS)).construct(varAnnotationProcessor, staticVariableInference);
});
cls.method<&StaticVariableHandler::supports, zp::Obj>(sigs::supports);
cls.method(sigs::processStmt, [](INTERNAL_FUNCTION_PARAMETERS) {
zval *nodeScopeResolver, *stmt, *scope, *storage, *nodeCallback, *context;
ZEND_PARSE_PARAMETERS_START(6, 6)
Z_PARAM_OBJECT(nodeScopeResolver)
Z_PARAM_OBJECT(stmt)
Z_PARAM_OBJECT(scope)
Z_PARAM_OBJECT(storage)
Z_PARAM_ZVAL(nodeCallback)
Z_PARAM_OBJECT(context)
ZEND_PARSE_PARAMETERS_END();
PT_RETURN_VAL(StaticVariableHandler(Z_OBJ_P(ZEND_THIS)).processStmt(nodeScopeResolver, stmt, scope, storage, nodeCallback, context));
});
cls.shadow(&pt_ce_static_variable_handler);
pt_stmt_handler_entry_register(&pt_ce_static_variable_handler, &StaticVariableHandler::processStmtEntry);
}
/* }}} */