Skip to content

Commit

Permalink
#2276 - Add support for mixed type in parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeckerson committed Sep 20, 2021
1 parent c0dd5e8 commit 1d17840
Show file tree
Hide file tree
Showing 11 changed files with 291 additions and 58 deletions.
18 changes: 11 additions & 7 deletions Library/Backends/ZendEngine3/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function getStringsManager()
/**
* {@inheritdoc}
*/
public function getTypeDefinition($type)
public function getTypeDefinition($type): array
{
switch ($type) {
case 'zend_ulong':
Expand Down Expand Up @@ -154,6 +154,7 @@ public function getTypeDefinition($type)
case 'variable':
case 'array':
case 'null':
case 'mixed':
$pointer = '*';
$code = 'zval';
break;
Expand Down Expand Up @@ -316,7 +317,7 @@ public function onPostCompile(ClassMethod $method, CompilationContext $context)

public function generateInitCode(&$groupVariables, $type, $pointer, Variable $variable)
{
$isComplex = \in_array($type, ['variable', 'string', 'array', 'resource', 'callable', 'object'], true);
$isComplex = \in_array($type, ['variable', 'string', 'array', 'resource', 'callable', 'object', 'mixed'], true);

if ($isComplex && !$variable->isDoublePointer()) { /* && $variable->mustInitNull() */
$groupVariables[] = $variable->getName();
Expand Down Expand Up @@ -365,6 +366,7 @@ public function generateInitCode(&$groupVariables, $type, $pointer, Variable $va
case 'resource':
case 'callable':
case 'object':
case 'mixed':
$groupVariables[] = $pointer.$variable->getName();
break;

Expand Down Expand Up @@ -594,7 +596,7 @@ public function addArrayEntry(Variable $variable, $key, $value, CompilationConte
$keyType = 'append';
} elseif ($key instanceof CompiledExpression) {
$typeKey = $key->getType();
if ('variable' == $typeKey) {
if ('variable' === $typeKey || 'mixed' === $typeKey) {
$var = $context->symbolTable->getVariableForRead($key->getCode(), $context);
$typeKey = $var->getType();
}
Expand Down Expand Up @@ -629,6 +631,7 @@ public function addArrayEntry(Variable $variable, $key, $value, CompilationConte

case 'variable':
case 'array':
case 'mixed':
$type = 'zval';
break;
}
Expand Down Expand Up @@ -846,20 +849,21 @@ public function resolveValue($value, CompilationContext $context, $usePointer =
$tempVariable = new Variable('variable', $varName, $context->branchManager->getCurrentBranch());
$context->symbolTable->addRawVariable($tempVariable);
}

$tempVariable = $context->symbolTable->getVariableForWrite($varName, $context);
$tempVariable->increaseUses();
$tempVariable->setUsed(true, null);
$tempVariable->setUsed(true);

if ('null' == $value) {
$tempVariable->setDynamicTypes('null');
} else {
$tempVariable->setDynamicTypes('bool');
}

$value = $this->getVariableCode($tempVariable);
} else {
if ($value instanceof CompiledExpression) {
if ('array' == $value->getType()) {
$value = $context->symbolTable->getVariableForWrite($value->getCode(), $context, null);
} elseif ('variable' == $value->getType()) {
if (in_array($value->getType(), ['array', 'variable', 'mixed'])) {
$value = $context->symbolTable->getVariableForWrite($value->getCode(), $context);
} else {
return $value->getCode();
Expand Down
4 changes: 3 additions & 1 deletion Library/ClassMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,7 @@ public function assignZvalValue(array $parameter, CompilationContext $compilatio
{
$dataType = $this->getParamDataType($parameter);

if (in_array($dataType, ['variable', 'callable', 'object', 'resource'])) {
if (in_array($dataType, ['variable', 'callable', 'object', 'resource', 'mixed'])) {
return "";
}

Expand Down Expand Up @@ -1652,6 +1652,7 @@ public function compile(CompilationContext $compilationContext): void
case 'callable':
case 'resource':
case 'variable':
case 'mixed':
$symbol = $symbolTable->addVariable($parameter['data-type'], $parameter['name'], $compilationContext);
/* TODO: Move this to the respective backend, which requires refactoring how this works */
if ($compilationContext->backend->isZE3()) {
Expand Down Expand Up @@ -1878,6 +1879,7 @@ public function compile(CompilationContext $compilationContext): void
case 'callable':
case 'resource':
case 'variable':
case 'mixed':
$name = $parameter['name'];
break;

Expand Down
1 change: 1 addition & 0 deletions Library/ClassMethodParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public function fetchParameters(bool $isMethodInternal): array
case 'callable':
case 'resource':
case 'variable':
case 'mixed':
$parameters[] = $isMethodInternal ? $name : '&'.$name;
break;

Expand Down
1 change: 1 addition & 0 deletions Library/Expression/NativeArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public function getArrayValue(CompiledExpression $exprCompiled, CompilationConte
case 'string':
case 'variable':
case 'array':
case 'mixed':
return $itemVariable;

default:
Expand Down
4 changes: 4 additions & 0 deletions Library/Statements/ReturnStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public function compile(CompilationContext $compilationContext): void
break;

case Types::T_VARIABLE:
case Types::T_MIXED:
$symbolVariable = $compilationContext->symbolTable->getVariableForRead(
$resolvedExpr->getCode(),
$compilationContext,
Expand Down Expand Up @@ -176,6 +177,7 @@ public function compile(CompilationContext $compilationContext): void
break;

case Types::T_VARIABLE:
case Types::T_MIXED:
break;
}
break;
Expand Down Expand Up @@ -221,6 +223,7 @@ public function compile(CompilationContext $compilationContext): void
break;

case Types::T_VARIABLE:
case Types::T_MIXED:
if (!isset($symbolVariable)) {
$symbolVariable = $compilationContext->symbolTable->getVariableForRead(
$resolvedExpr->getCode(),
Expand Down Expand Up @@ -256,6 +259,7 @@ public function compile(CompilationContext $compilationContext): void
break;

case Types::T_VARIABLE:
case Types::T_MIXED:
if ('this_ptr' == $symbolVariable->getName()) {
$codePrinter->output('RETURN_THIS();');
} else {
Expand Down
8 changes: 4 additions & 4 deletions Library/SymbolTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public function getVariablesByBranch($branchId)
*/
public function getVariableForRead($name, CompilationContext $compilationContext = null, array $statement = null)
{
/*
/**
* Validate that 'this' cannot be used in a static function
*/
if ('this' == $name || 'this_ptr' == $name) {
Expand All @@ -262,7 +262,7 @@ public function getVariableForRead($name, CompilationContext $compilationContext
}
}

/*
/**
* Create superglobals just in time
*/
if ($this->globalsManager->isSuperGlobal($name)) {
Expand Down Expand Up @@ -295,7 +295,7 @@ public function getVariableForRead($name, CompilationContext $compilationContext

$variable->increaseUses();

/*
/**
* Analise branches to detect possible initialization of variables in conditional branches
*/
if (!$variable->isTemporal() && !$variable->getSkipVariant()) {
Expand All @@ -306,6 +306,7 @@ public function getVariableForRead($name, CompilationContext $compilationContext
case 'variable':
case 'string':
case 'array':
case 'mixed':
if (!$variable->isLocalOnly()) {
$variable->setMustInitNull(true);
}
Expand Down Expand Up @@ -380,7 +381,6 @@ public function getVariableForRead($name, CompilationContext $compilationContext
foreach ($branches as $branch) {
$graph->addLeaf($branch);
}
//echo $graph->getRoot()->show();
} else {
/*
* Variable is assigned just once and it's assigned in a conditional branch
Expand Down
119 changes: 118 additions & 1 deletion ext/stub/types/mixedtype.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions ext/stub/types/mixedtype.zep.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1d17840

Please sign in to comment.