Skip to content

Commit

Permalink
#2330 - Improve support of mixed type casting
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeckerson committed Dec 24, 2021
1 parent 2a9e10a commit 13bbd46
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions Library/Operators/Bitwise/BitwiseNotOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function compile($expression, CompilationContext $compilationContext)
return new CompiledExpression('int', '~'.$variable->getName(), $expression);

case 'variable':
case 'mixed':
$compilationContext->headersManager->add('kernel/operators');

return new CompiledExpression('int', '~zephir_get_intval('.$variable->getName().')', $expression);
Expand Down
11 changes: 11 additions & 0 deletions Library/Operators/Comparison/ComparisonBaseOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ public function compile(array $expression, CompilationContext $compilationContex
return new CompiledExpression('bool', '0 '.$this->operator.' '.$variableRight->getName(), $expression);

case 'variable':
case 'mixed':
case 'string':
$compilationContext->headersManager->add('kernel/operators');
$condition = $compilationContext->backend->getTypeofCondition($variableRight, $this->operator, 'null', $compilationContext);
Expand Down Expand Up @@ -281,6 +282,7 @@ public function compile(array $expression, CompilationContext $compilationContex
return new CompiledExpression('bool', $left->getCode().' '.$this->operator.' '.$variableRight->getName(), $expression);

case 'variable':
case 'mixed':
$compilationContext->headersManager->add('kernel/operators');
$variableCode = $compilationContext->backend->getVariableCode($variableRight);

Expand Down Expand Up @@ -330,6 +332,7 @@ public function compile(array $expression, CompilationContext $compilationContex
return new CompiledExpression('bool', $left->getBooleanCode().' '.$this->operator.' '.$variableRight->getName(), $expression);

case 'variable':
case 'mixed':
$compilationContext->headersManager->add('kernel/operators');
$boolOperator = '1' == $left->getBooleanCode() ? $this->zvalBoolTrueOperator : $this->zvalBoolFalseOperator;
$variableRight = $compilationContext->backend->getVariableCode($variableRight);
Expand Down Expand Up @@ -370,6 +373,7 @@ public function compile(array $expression, CompilationContext $compilationContex
switch ($variableRight->getType()) {
case 'string':
case 'variable':
case 'mixed':
$compilationContext->headersManager->add('kernel/operators');
$variableRight = $compilationContext->backend->getVariableCode($variableRight);

Expand Down Expand Up @@ -424,6 +428,7 @@ public function compile(array $expression, CompilationContext $compilationContex
return new CompiledExpression('bool', $variable->getName().' '.$this->operator.' '.$variableRight->getName(), $expression);

case 'variable':
case 'mixed':
$compilationContext->headersManager->add('kernel/operators');
$variableRightCode = $compilationContext->backend->getVariableCode($variableRight);
$variableCode = $compilationContext->backend->getVariableCode($variable);
Expand Down Expand Up @@ -468,6 +473,7 @@ public function compile(array $expression, CompilationContext $compilationContex
return new CompiledExpression('bool', $variable->getName().' '.$this->operator.' '.$variableRight->getName(), $expression);

case 'variable':
case 'mixed':
$compilationContext->headersManager->add('kernel/operators');
$variableRightCode = $compilationContext->backend->getVariableCode($variableRight);
$variableCode = $compilationContext->backend->getVariableCode($variable);
Expand Down Expand Up @@ -508,6 +514,7 @@ public function compile(array $expression, CompilationContext $compilationContex
return new CompiledExpression('bool', $variable->getName().' '.$this->operator.' '.$variableRight->getName(), $expression);

case 'variable':
case 'mixed':
$compilationContext->headersManager->add('kernel/operators');
$boolOperator = '1' == $left->getBooleanCode() ? $this->zvalBoolTrueOperator : $this->zvalBoolFalseOperator;
$variableRightCode = $compilationContext->backend->getVariableCode($variableRight);
Expand All @@ -534,6 +541,7 @@ public function compile(array $expression, CompilationContext $compilationContex
switch ($variableRight->getType()) {
case 'string':
case 'variable':
case 'mixed':
case 'array':
$compilationContext->headersManager->add('kernel/operators');
$variableRight = $compilationContext->backend->getVariableCode($variableRight);
Expand Down Expand Up @@ -566,6 +574,7 @@ public function compile(array $expression, CompilationContext $compilationContex
switch ($variableRight->getType()) {
case 'string':
case 'variable':
case 'mixed':
$variableRight = $compilationContext->backend->getVariableCode($variableRight);

return new CompiledExpression('bool', $this->zvalOperator.'('.$variableCode.', '.$variableRight.')', $expression);
Expand All @@ -581,6 +590,7 @@ public function compile(array $expression, CompilationContext $compilationContex
break;

case 'variable':
case 'mixed':
$compilationContext->headersManager->add('kernel/operators');

switch ($right->getType()) {
Expand Down Expand Up @@ -625,6 +635,7 @@ public function compile(array $expression, CompilationContext $compilationContex

case 'string':
case 'variable':
case 'mixed':
case 'array':
$variableRight = $compilationContext->backend->getVariableCode($variableRight);

Expand Down
1 change: 1 addition & 0 deletions Library/Operators/Unary/NotOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function compile($expression, CompilationContext $compilationContext): Co
return new CompiledExpression('bool', '!'.$variable->getName(), $expression);

case 'variable':
case 'mixed':
$compilationContext->headersManager->add('kernel/operators');
$symbol = $compilationContext->backend->getVariableCode($variable);

Expand Down
1 change: 1 addition & 0 deletions Library/Statements/EchoStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function compile(CompilationContext $compilationContext)

case 'string':
case 'variable':
case 'mixed':
$compilationContext->codePrinter->output('zend_print_zval('.$compilationContext->backend->getVariableCode($variable).', 0);');
break;

Expand Down
4 changes: 4 additions & 0 deletions Library/Statements/Let/Variable.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ private function doNumericAssignment(
break;

case 'variable':
case 'mixed':
$compilationContext->headersManager->add('kernel/operators');
$exprVariable = $compilationContext->symbolTable->getVariableForWrite($resolvedExpr->resolve(null, $compilationContext), $compilationContext);
$exprVariableCode = $compilationContext->backend->getVariableCode($exprVariable);
Expand Down Expand Up @@ -503,6 +504,7 @@ private function doDoubleAssignment(
break;

case 'variable':
case 'mixed':
$compilationContext->headersManager->add('kernel/operators');
$exprVariableCode = $compilationContext->backend->getVariableCode($itemVariable);
switch ($statement['operator']) {
Expand Down Expand Up @@ -691,6 +693,7 @@ private function doStringAssignment(
break;

case 'variable':
case 'mixed':
switch ($statement['operator']) {
case 'assign':
$symbolVariable->setMustInitNull(true);
Expand Down Expand Up @@ -879,6 +882,7 @@ private function doBoolAssignment(
break;

case 'variable':
case 'mixed':
case 'string':
case 'array':
switch ($statement['operator']) {
Expand Down
3 changes: 3 additions & 0 deletions Library/Statements/Let/VariableAppend.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function assign($variable, ZephirVariable $symbolVariable, CompiledExpres
switch ($type) {
case 'array':
case 'variable':
case 'mixed':
switch ($resolvedExpr->getType()) {
case 'null':
$compilationContext->backend->addArrayEntry($symbolVariable, null, 'null', $compilationContext, $statement);
Expand Down Expand Up @@ -112,6 +113,7 @@ public function assign($variable, ZephirVariable $symbolVariable, CompiledExpres
break;

case 'variable':
case 'mixed':
$exprVariable = $compilationContext->symbolTable->getVariableForRead($resolvedExpr->getCode(), $compilationContext, $statement);
switch ($exprVariable->getType()) {
case 'int':
Expand All @@ -138,6 +140,7 @@ public function assign($variable, ZephirVariable $symbolVariable, CompiledExpres
break;

case 'variable':
case 'mixed':
case 'string':
case 'array':
$compilationContext->backend->addArrayEntry($symbolVariable, null, $exprVariable, $compilationContext, $statement);
Expand Down
20 changes: 20 additions & 0 deletions stub/types/mixedtype.zep
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,24 @@ class MixedType

return val;
}

/**
* Only used during generation
*/
public function mixedInCondition(mixed val) -> void
{
if unlikely val {
// OK
}

if val === 1 {
// OK
}

if 1 === val {

}

echo val;
}
}

0 comments on commit 13bbd46

Please sign in to comment.