Skip to content

Commit

Permalink
#2407 - Mass code refactor and reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeckerson committed Sep 2, 2023
1 parent 0c1d61e commit 3d46965
Show file tree
Hide file tree
Showing 77 changed files with 380 additions and 916 deletions.
91 changes: 26 additions & 65 deletions Library/Backend/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Zephir\Variable;
use Zephir\Variable\Globals;

use function in_array;
use function Zephir\add_slashes;

class Backend
Expand Down Expand Up @@ -74,10 +75,10 @@ public function getFcallManager(): FcallManagerInterface
return $this->fcallManager;
}

public function declareVariables($method, $typeToVariables, CompilationContext $compilationContext)
public function declareVariables($method, $typeToVariables)
{
$varInitCode = [];
$additionalCode = $method ? $this->onPreInitVar($method, $compilationContext) : '';
$additionalCode = $method ? $this->onPreInitVar($method) : '';

foreach ($typeToVariables as $type => $variables) {
list($pointer, $code) = $this->getTypeDefinition($type);
Expand Down Expand Up @@ -119,24 +120,18 @@ public function initVar(Variable $variable, CompilationContext $context, $useCod
return $code;
}

/**
* {@inheritdoc}
*/
public function getVariableCode(Variable $variable): string
{
if ($variable->isDoublePointer() ||
\in_array($variable->getName(), ['this_ptr', 'return_value']) ||
\in_array($variable->getType(), ['int', 'long'])) {
in_array($variable->getName(), ['this_ptr', 'return_value']) ||
in_array($variable->getType(), ['int', 'long'])) {
return $variable->getName();
}

return '&'.$variable->getName();
}

/**
* {@inheritdoc}
*/
public function getBoolCode(Variable $variable, CompilationContext $context, $useCodePrinter = true)
public function getBoolCode(Variable $variable, CompilationContext $context, $useCodePrinter = true): string
{
$code = '(Z_TYPE_P('.$this->getVariableCode($variable).') == IS_TRUE)';
if ($useCodePrinter) {
Expand All @@ -146,9 +141,6 @@ public function getBoolCode(Variable $variable, CompilationContext $context, $us
return $code;
}

/**
* {@inheritdoc}
*/
public function getTypeDefinition($type): array
{
switch ($type) {
Expand Down Expand Up @@ -252,12 +244,9 @@ public function getTypeDefinition($type): array
}

/**
* {@inheritdoc}
*
* @param Variable $variableVariable
* @param string $operator
* @param string $value
* @param CompilationContext $context
*
* @throws CompilerException
*
Expand All @@ -267,7 +256,6 @@ public function getTypeofCondition(
Variable $variableVariable,
string $operator,
string $value,
CompilationContext $context
): string {
$variableName = $this->getVariableCode($variableVariable);

Expand Down Expand Up @@ -319,10 +307,7 @@ public function getTypeofCondition(
return $condition;
}

/**
* {@inheritdoc}
*/
public function onPreInitVar(ClassMethod $method, CompilationContext $context): string
public function onPreInitVar(ClassMethod $method): string
{
if (!$method instanceof FunctionDefinition && !$method->isInternal()) {
return "zval *this_ptr = getThis();\n"; //TODO: think about a better way to solve this.
Expand Down Expand Up @@ -360,7 +345,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', 'mixed'], 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 @@ -441,8 +426,6 @@ public function generateInitCode(&$groupVariables, $type, $pointer, Variable $va
}

/**
* {@inheritdoc}
*
* @param Variable[] $variables
* @param CompilationContext $context
*
Expand Down Expand Up @@ -522,8 +505,6 @@ public function declareConstant($type, $name, $value, CompilationContext $contex
}

/**
* {@inheritdoc}
*
* @param ClassMethod $method
* @param CompilationContext $context
*
Expand Down Expand Up @@ -574,8 +555,6 @@ public function getInternalSignature(ClassMethod $method, CompilationContext $co
}

/**
* {@inheritdoc}
*
* @param Variable $variable
* @param string|Variable|null $value
* @param CompilationContext $context
Expand Down Expand Up @@ -617,7 +596,7 @@ public function assignZval(Variable $variable, $code, CompilationContext $contex
}
}

public function returnString($value, CompilationContext $context, $useCodePrinter = true, $doCopy = true)
public function returnString($value, CompilationContext $context, $useCodePrinter = true)
{
return $this->returnHelper('RETURN_MM_STRING', $value, $context, $useCodePrinter);
}
Expand Down Expand Up @@ -668,7 +647,7 @@ public function addArrayEntry(Variable $variable, $key, $value, CompilationConte
$var = $context->symbolTable->getVariableForRead($key->getCode(), $context);
$typeKey = $var->getType();
}
if (\in_array($typeKey, ['int', 'uint', 'long', 'ulong'])) {
if (in_array($typeKey, ['int', 'uint', 'long', 'ulong'])) {
$keyType = 'index';
}
}
Expand Down Expand Up @@ -809,7 +788,7 @@ public function arrayIssetFetch2(Variable $target, Variable $var, $resolvedExpr,
throw new CompilerException('arrayIssetFetch ['.$resolvedExpr->getType().']', $expression);
}

public function propertyIsset(Variable $var, $key, CompilationContext $context)
public function propertyIsset(Variable $var, $key)
{
return new CompiledExpression('bool', 'zephir_isset_property('.$this->getVariableCode($var).', SL("'.$key.'"))', null);
}
Expand Down Expand Up @@ -893,8 +872,6 @@ public function fetchClass(Variable $zendClassEntry, $className, $guarded, Compi
}

/**
* {@inheritdoc}
*
* @param Variable $symbolVariable
* @param Variable $variableVariable
* @param Variable|string $property
Expand Down Expand Up @@ -1028,16 +1005,14 @@ public function resolveValue($value, CompilationContext $context): Variable|bool
}

/**
* {@inheritdoc}
*
* @param Variable $variable
* @param string|Variable $property
* @param mixed $value
* @param CompilationContext $context
*
* @return void
*/
public function updateProperty(Variable $variable, $property, $value, CompilationContext $context)
public function updateProperty(Variable $variable, $property, $value, CompilationContext $context): void
{
$value = $this->resolveValue($value, $context);

Expand Down Expand Up @@ -1094,7 +1069,7 @@ public function assignArrayProperty(Variable $variable, $property, $key, $value,
}
}

public function callMethod($symbolVariable, Variable $variable, $methodName, $cachePointer, $params, CompilationContext $context)
public function callMethod($symbolVariable, Variable $variable, $methodName, $cachePointer, $params, CompilationContext $context): void
{
$paramStr = null != $params ? ', '.implode(', ', $params) : '';
$macro = 'CALL_METHOD';
Expand All @@ -1104,6 +1079,7 @@ public function callMethod($symbolVariable, Variable $variable, $methodName, $ca
} else {
$methodName = '"'.$methodName.'"';
}

if (!isset($symbolVariable)) {
$context->codePrinter->output('ZEPHIR_'.$macro.'(NULL, '.$this->getVariableCode($variable).', '.$methodName.', '.$cachePointer.$paramStr.');');
} elseif ('return_value' == $symbolVariable->getName()) {
Expand All @@ -1114,9 +1090,9 @@ public function callMethod($symbolVariable, Variable $variable, $methodName, $ca
}
}

public function copyOnWrite(Variable $target, $var, CompilationContext $context)
public function copyOnWrite(Variable $target, $var, CompilationContext $context): void
{
if ('EG(exception)' == $var) {
if ('EG(exception)' === $var) {
$context->codePrinter->output('ZVAL_OBJ('.$this->getVariableCode($target).', EG(exception));');
$context->codePrinter->output('Z_ADDREF_P('.$this->getVariableCode($target).');');

Expand Down Expand Up @@ -1275,26 +1251,16 @@ public function forStatement(Variable $exprVariable, $keyVariable, $variable, $d
$codePrinter->output('}');

/* Since we do not observe, still do cleanup */
if (isset($variable)) {
$variable->initVariant($compilationContext);
}
if (isset($keyVariable)) {
$keyVariable->initVariant($compilationContext);
}
$variable?->initVariant($compilationContext);
$keyVariable?->initVariant($compilationContext);
}

public function forStatementIterator(Variable $iteratorVariable, Variable $targetVariable, CompilationContext $compilationContext)
{
$compilationContext->symbolTable->mustGrownStack(true);
$compilationContext->codePrinter->output('ZEPHIR_ITERATOR_COPY('.$this->getVariableCode($targetVariable).', '.$iteratorVariable->getName().');');
}

public function destroyIterator(Variable $iteratorVariable, CompilationContext $context)
public function destroyIterator(Variable $iteratorVariable, CompilationContext $context): void
{
$context->codePrinter->output('zend_iterator_dtor('.$iteratorVariable->getName().');');
}

public function ifVariableValueUndefined(Variable $var, CompilationContext $context, $useBody = false, $useCodePrinter = true)
public function ifVariableValueUndefined(Variable $var, CompilationContext $context, $useBody = false, $useCodePrinter = true): string
{
if ($var->isDoublePointer()) {
return $this->ifVariableValueUndefined2($var, $context, $useBody, $useCodePrinter);
Expand All @@ -1308,7 +1274,7 @@ public function ifVariableValueUndefined(Variable $var, CompilationContext $cont
return $useBody ? $body : $output;
}

public function ifVariableValueUndefined2(Variable $var, CompilationContext $context, $onlyBody = false, $useCodePrinter = true)
public function ifVariableValueUndefined2(Variable $var, CompilationContext $context, $onlyBody = false, $useCodePrinter = true): string
{
$body = '!'.$var->getName();
$output = 'if ('.$body.') {';
Expand All @@ -1319,24 +1285,20 @@ public function ifVariableValueUndefined2(Variable $var, CompilationContext $con
return $onlyBody ? $body : $output;
}

public function fetchClassEntry($str)
public function fetchClassEntry(string $str): string
{
return 'zephir_get_internal_ce(SL("'.$str.'"))';
}

/**
* {@inheritdoc}
*
* @param string $type
* @param CompilationContext $compilationContext
* @param bool $isLocal
*
* @return Variable
*/
public function getScalarTempVariable(
string $type,
CompilationContext $compilationContext,
$isLocal = true
): Variable {
return $compilationContext->symbolTable->getTempNonTrackedVariable($type, $compilationContext);
}
Expand Down Expand Up @@ -1707,7 +1669,7 @@ protected function returnHelper(string $macro, $value, CompilationContext $conte
{
if ($value instanceof Variable) {
$value = $value->getName();
} elseif ($macro === 'RETURN_MM_STRING' && !preg_match('/^ZEPHIR_GLOBAL/', $value)) {
} elseif ($macro === 'RETURN_MM_STRING' && !str_starts_with($value, 'ZEPHIR_GLOBAL')) {
$value = '"'.$value.'"';
}

Expand All @@ -1734,11 +1696,10 @@ protected function returnHelper(string $macro, $value, CompilationContext $conte
* @param CompiledExpression[]|string[] $offsetExprs
* @param CompilationContext $compilationContext
*
* @throws CompilerException
*
* @return array
* @throws CompilerException
*/
private function resolveOffsetExprs($offsetExprs, CompilationContext $compilationContext)
private function resolveOffsetExprs(array $offsetExprs, CompilationContext $compilationContext): array
{
$keys = '';
$offsetItems = [];
Expand All @@ -1750,6 +1711,7 @@ private function resolveOffsetExprs($offsetExprs, CompilationContext $compilatio
++$numberParams;
continue;
}

switch ($offsetExpr->getType()) {
case 'int':
case 'uint':
Expand All @@ -1770,7 +1732,6 @@ private function resolveOffsetExprs($offsetExprs, CompilationContext $compilatio
$variableIndex = $compilationContext->symbolTable->getVariableForRead(
$offsetExpr->getCode(),
$compilationContext,
null
);

switch ($variableIndex->getType()) {
Expand Down
8 changes: 2 additions & 6 deletions Library/Backend/FcallManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@

use Zephir\CodePrinter;
use Zephir\Fcall\FcallManagerInterface;

use function Zephir\file_put_contents_ex;

class FcallManager implements FcallManagerInterface
{
protected array $requiredMacros = [];

public function macroIsRequired($macro): bool
{
return isset($this->requiredMacros[$macro]);
}

/**
* {@inheritdoc}
*
Expand All @@ -47,7 +43,7 @@ public function getMacro($static, $doReturn, $paramCount)

$macroName = 'ZEPHIR_'.($scope ? $scope.'_' : '').$mode.$paramCount;

if (!$this->macroIsRequired($macroName)) {
if (!isset($this->requiredMacros[$macroName])) {
$this->requiredMacros[$macroName] = [$scope, $mode, $paramCount];
}

Expand Down
10 changes: 6 additions & 4 deletions Library/Backend/StringsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
namespace Zephir\Backend;

use Zephir\StringsManager as BaseStringsManager;

use function strlen;
use function Zephir\file_put_contents_ex;

/**
Expand Down Expand Up @@ -59,7 +61,7 @@ public function genConcatCode(): void
$macros = [];
ksort($this->concatKeys, SORT_STRING);
foreach ($this->concatKeys as $key => $one) {
$len = \strlen($key);
$len = strlen($key);
$params = [];
$zvalCopy = [];
$useCopy = [];
Expand Down Expand Up @@ -94,7 +96,7 @@ public function genConcatCode(): void

$proto = 'void zephir_concat_'.$key.'(zval *result, '.implode(', ', $params).', int self_var)';

$codeh .= ''.$proto.';'.PHP_EOL;
$codeh .= $proto.';'.PHP_EOL;

$code .= $proto.'{'.PHP_EOL.PHP_EOL;

Expand All @@ -118,7 +120,7 @@ public function genConcatCode(): void

$code .= "\t".'length = '.implode(' + ', $lengths).';'.PHP_EOL;
$code .= "\t".'if (self_var) {'.PHP_EOL;
$code .= ''.PHP_EOL;
$code .= PHP_EOL;
$code .= "\t\t".'if (Z_TYPE_P(result) != IS_STRING) {'.PHP_EOL;
$code .= "\t\t\t".'use_copy = zend_make_printable_zval(result, &result_copy);'.PHP_EOL;
$code .= "\t\t\t".'if (use_copy) {'.PHP_EOL;
Expand All @@ -128,7 +130,7 @@ public function genConcatCode(): void
$code .= "\t\t".'offset = Z_STRLEN_P(result);'.PHP_EOL;
$code .= "\t\t".'length += offset;'.PHP_EOL;
$code .= "\t\t".'Z_STR_P(result) = zend_string_realloc(Z_STR_P(result), length, 0);'.PHP_EOL;
$code .= ''.PHP_EOL;
$code .= PHP_EOL;
$code .= "\t".'} else {'.PHP_EOL;
$code .= "\t\t".'ZVAL_STR(result, zend_string_alloc(length, 0));'.PHP_EOL;
$code .= "\t".'}'.PHP_EOL.PHP_EOL;
Expand Down
Loading

0 comments on commit 3d46965

Please sign in to comment.