|
23 | 23 | #include "support.h" |
24 | 24 | #include "generated/AssignHandler.h" |
25 | 25 | #include "generated/PreparedAssignTarget.h" |
| 26 | +#include "generated/MutatingScope.h" |
26 | 27 |
|
27 | 28 | namespace slots = ptdecl::AssignHandler::slot; |
28 | 29 | namespace sigs = ptdecl::AssignHandler::sig; |
| 30 | +namespace msSlots = ptdecl::MutatingScope::slot; |
29 | 31 | #include "zv.h" |
30 | 32 | #include "TypeTraits.h" |
31 | 33 | #include "TypeOps.h" |
@@ -2058,6 +2060,23 @@ class AssignHandler |
2058 | 2060 | } |
2059 | 2061 | AH_OKB(processSureTypesForConditionalExpressionsAfterAssign(nsr, scope.raw(), storage, varName, conditionalExpressions, falseySpecifiedTypes.raw(), falseyType.raw(), impurePoints.raw(), assignedExpr.raw(), stored)); |
2060 | 2062 | AH_OKB(processSureNotTypesForConditionalExpressionsAfterAssign(nsr, scope.raw(), storage, varName, conditionalExpressions, falseySpecifiedTypes.raw(), falseyType.raw(), impurePoints.raw(), assignedExpr.raw(), stored)); |
| 2063 | + |
| 2064 | + // An assignment inside a short-circuit operand may or may not have run |
| 2065 | + // (its variable is only maybe-defined after the RHS walk), but a truthy |
| 2066 | + // && (or falsey ||) guarantees the right operand was evaluated. The |
| 2067 | + // operand walk's truthy/falsey scope knows exactly which variables it |
| 2068 | + // defined - record their certainty as a consequence of the assigned |
| 2069 | + // boolean, e.g. "$bool = $x && ($var = 'foo'); if ($bool) { … $var … }". |
| 2070 | + bool isAnd = ahIs(assignedExpr.raw(), PT_CLASS_BOOLEAN_AND_EXPR) || ahIs(assignedExpr.raw(), PT_CLASS_LOGICAL_AND_EXPR); |
| 2071 | + bool isOr = !isAnd && (ahIs(assignedExpr.raw(), PT_CLASS_BOOLEAN_OR_EXPR) || ahIs(assignedExpr.raw(), PT_CLASS_LOGICAL_OR_EXPR)); |
| 2072 | + if (stored != NULL && (isAnd || isOr)) { |
| 2073 | + bool containsAssignment; |
| 2074 | + AH_OKB(containsVariableAssignment(assignedExpr.raw(), containsAssignment)); |
| 2075 | + if (containsAssignment) { |
| 2076 | + AH_VALB(branchScope, isAnd ? pt_expression_result_get_truthy_scope(stored) : pt_expression_result_get_falsey_scope(stored)); |
| 2077 | + AH_OKB(processDefinednessForConditionalExpressionsAfterAssign(conditionalExpressions, varName, isAnd ? truthyType.raw() : falseyType.raw(), branchScope.raw(), scope.raw())); |
| 2078 | + } |
| 2079 | + } |
2061 | 2080 | } |
2062 | 2081 |
|
2063 | 2082 | for (int falseyIndex = 0; falseyIndex < 7; falseyIndex++) { |
@@ -2466,6 +2485,74 @@ class AssignHandler |
2466 | 2485 | return true; |
2467 | 2486 | } |
2468 | 2487 |
|
| 2488 | + /* Records "if the assigned variable has $variableType, the target variable is |
| 2489 | + * certainly defined (with its branch-scope type)" holders for variables whose |
| 2490 | + * certainty is Yes in the given branch continuation scope of the RHS (the |
| 2491 | + * truthy scope of a `&&`, the falsey scope of a `||`) but only Maybe in the |
| 2492 | + * merged after-RHS scope. */ |
| 2493 | + [[nodiscard]] static bool processDefinednessForConditionalExpressionsAfterAssign(zv::Val &conditionalExpressions, zend_string *variableName, zval *variableType, zval *branchScope, zval *mergedScope) |
| 2494 | + { |
| 2495 | + zval *branchTypes = OBJ_PROP_NUM(Z_OBJ_P(branchScope), msSlots::expressionTypes); |
| 2496 | + zval *mergedTypes = OBJ_PROP_NUM(Z_OBJ_P(mergedScope), msSlots::expressionTypes); |
| 2497 | + ZVAL_DEREF(branchTypes); |
| 2498 | + ZVAL_DEREF(mergedTypes); |
| 2499 | + if (UNEXPECTED(Z_TYPE_P(branchTypes) != IS_ARRAY || Z_TYPE_P(mergedTypes) != IS_ARRAY)) { |
| 2500 | + pt_throw_should_not_happen(); |
| 2501 | + return false; |
| 2502 | + } |
| 2503 | + |
| 2504 | + for (auto entry : zv::TableRef(Z_ARRVAL_P(branchTypes))) { |
| 2505 | + zval *holder = entry.value().deref().raw(); |
| 2506 | + if (pt_holder_certainty_value(Z_OBJ_P(holder)) != PT_TRI_YES) continue; |
| 2507 | + zval *expr = OBJ_PROP_NUM(Z_OBJ_P(holder), PT_ETH_PROP_EXPR); |
| 2508 | + ZVAL_DEREF(expr); |
| 2509 | + zend_string *name = ahStringVariableName(expr); |
| 2510 | + if (name == NULL || zend_string_equals(name, variableName)) continue; |
| 2511 | + |
| 2512 | + zend_string *stringKey = entry.stringKeyOrNull(); |
| 2513 | + zval *mergedHolder = stringKey != NULL ? zend_hash_find(Z_ARRVAL_P(mergedTypes), stringKey) : zend_hash_index_find(Z_ARRVAL_P(mergedTypes), entry.indexKey()); |
| 2514 | + if (mergedHolder == NULL) continue; |
| 2515 | + ZVAL_DEREF(mergedHolder); |
| 2516 | + if (pt_holder_certainty_value(Z_OBJ_P(mergedHolder)) != PT_TRI_MAYBE) continue; |
| 2517 | + |
| 2518 | + AH_VALB(variable, newVariable(variableName)); |
| 2519 | + AH_VALB(conditionHolder, newExpressionTypeHolder(variable.raw(), variableType, PT_TRI_YES)); |
| 2520 | + zv::Str conditionKey = zv::Str::adopt(zend_string_concat2(PT_LC("$"), ZSTR_VAL(variableName), ZSTR_LEN(variableName))); |
| 2521 | + zv::Val conditions = ahEmptyArray(); |
| 2522 | + ahSetKey(conditions, conditionKey.get(), conditionHolder.raw()); |
| 2523 | + AH_VALB(conditionalHolder, newConditionalExpressionHolder(conditions.raw(), holder)); |
| 2524 | + zv::Str holderKey = zv::Str::adopt(conditionalHolderKey(conditionalHolder.raw())); |
| 2525 | + AH_OKB(!holderKey.isNull()); |
| 2526 | + zv::Str exprString = stringKey != NULL ? zv::Str::copyOf(stringKey) : zv::Str::adopt(zend_long_to_str((zend_long) entry.indexKey())); |
| 2527 | + nestedSet(conditionalExpressions, exprString.get(), holderKey.get(), conditionalHolder.raw()); |
| 2528 | + } |
| 2529 | + |
| 2530 | + return true; |
| 2531 | + } |
| 2532 | + |
| 2533 | + /* matcher for containsVariableAssignment() */ |
| 2534 | + static bool variableAssignmentMatcher(zend_object *node, void *ctx) |
| 2535 | + { |
| 2536 | + zend_class_entry *assignCe = pt_class(PT_CLASS_ASSIGN_EXPR); |
| 2537 | + zend_class_entry *assignOpCe = pt_class(PT_CLASS_ASSIGN_OP_EXPR); |
| 2538 | + zend_class_entry *assignRefCe = pt_class(PT_CLASS_ASSIGN_REF_EXPR); |
| 2539 | + if (UNEXPECTED(assignCe == NULL || assignOpCe == NULL || assignRefCe == NULL)) { |
| 2540 | + static_cast<pt_find_ctx *>(ctx)->failed = true; |
| 2541 | + return false; |
| 2542 | + } |
| 2543 | + return instanceof_function(node->ce, assignCe) || instanceof_function(node->ce, assignOpCe) || instanceof_function(node->ce, assignRefCe); |
| 2544 | + } |
| 2545 | + |
| 2546 | + /* Whether the expression contains an assignment whose execution short-circuit |
| 2547 | + * evaluation may have skipped - a cheap AST gate so the truthy/falsey scope |
| 2548 | + * is only derived for boolean RHS expressions that can define a variable. */ |
| 2549 | + [[nodiscard]] static bool containsVariableAssignment(zval *node, bool &contains) |
| 2550 | + { |
| 2551 | + pt_find_ctx ctx = {}; |
| 2552 | + contains = pt_find_first_recursive(Z_OBJ_P(node), variableAssignmentMatcher, &ctx) != NULL; |
| 2553 | + return !ctx.failed; |
| 2554 | + } |
| 2555 | + |
2469 | 2556 | /* (twin 2081) */ |
2470 | 2557 | static zv::Val currentTypeForConditionalHolder(zval *nsr, zval *scope, zval *storage, zval *expr, zval *assignedExpr, zval *assignedValueResult) |
2471 | 2558 | { |
|
0 commit comments