code
stringlengths
31
1.39M
docstring
stringlengths
23
16.8k
func_name
stringlengths
1
126
language
stringclasses
1 value
repo
stringlengths
7
63
path
stringlengths
7
166
url
stringlengths
50
220
license
stringclasses
7 values
public function getExpected(CompilationContext $compilationContext, array $expression, bool $init = true): ?Variable { if (!$this->expecting) { return $this->expectingVariable; } $symbolVariable = $this->expectingVariable; if (is_object($symbolVariable)) { if ('variable' === $symbolVariable->getType()) { if (!$init) { return $symbolVariable; } $symbolVariable->initVariant($compilationContext); } else { if (!$this->readOnly) { if (!$this->literalOnly) { $symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite( 'variable', $compilationContext, $expression ); } else { $symbolVariable = $compilationContext->symbolTable->getTempComplexLiteralVariableForWrite( 'variable', $compilationContext ); } } else { $symbolVariable = $compilationContext->symbolTable->getTempLocalVariableForWrite( 'variable', $compilationContext ); } } } else { if (!$this->readOnly) { if (!$this->literalOnly) { $symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite( 'variable', $compilationContext, $expression ); } else { $symbolVariable = $compilationContext->symbolTable->getTempComplexLiteralVariableForWrite( 'variable', $compilationContext ); } } else { $symbolVariable = $compilationContext->symbolTable->getTempLocalVariableForWrite( 'variable', $compilationContext ); } } return $symbolVariable; }
Returns the expected variable for assignment or creates a temporary variable to store the result. @param CompilationContext $compilationContext @param array $expression @param bool $init @return Variable|null
getExpected
php
zephir-lang/zephir
src/Operators/AbstractOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/AbstractOperator.php
MIT
public function getExpectedComplexLiteral( CompilationContext $compilationContext, string $type = 'variable' ): ?Variable { $symbolVariable = $this->expectingVariable; if ($this->expecting) { if (is_object($symbolVariable)) { if ($symbolVariable->getType() === $type || 'return_value' === $symbolVariable->getName()) { $symbolVariable->initVariant($compilationContext); } else { if (!$this->readOnly) { $symbolVariable = $compilationContext->symbolTable->getTempComplexLiteralVariableForWrite( $type, $compilationContext ); } else { $symbolVariable = $compilationContext->symbolTable->getTempLocalVariableForWrite( $type, $compilationContext ); } } } else { if (!$this->readOnly) { $symbolVariable = $compilationContext->symbolTable->getTempComplexLiteralVariableForWrite( $type, $compilationContext ); } else { $symbolVariable = $compilationContext->symbolTable->getTempLocalVariableForWrite( $type, $compilationContext ); } } } return $symbolVariable; }
Returns the expected variable for assignment or creates a temporary variable to store the result, if a temporary variable is created it use whose body is only freed on every iteration. @param CompilationContext $compilationContext @param string $type @return Variable|null
getExpectedComplexLiteral
php
zephir-lang/zephir
src/Operators/AbstractOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/AbstractOperator.php
MIT
public function getExpectedNonLiteral( CompilationContext $compilationContext, array $expression, bool $init = true ): ?Variable { $symbolVariable = $this->expectingVariable; if (!$this->expecting) { return $symbolVariable; } if ($symbolVariable !== null) { if ('variable' === $symbolVariable->getType() && !$symbolVariable->isLocalOnly()) { if (!$init) { return $symbolVariable; } $symbolVariable->initVariant($compilationContext); } else { $symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite( 'variable', $compilationContext, $expression ); } } else { $symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite( 'variable', $compilationContext, $expression ); } return $symbolVariable; }
Returns the expected variable for assignment or creates a temporary variable to store the result. This method returns a variable that is always stored in the heap. @param CompilationContext $compilationContext @param array $expression @param bool $init @return Variable|null
getExpectedNonLiteral
php
zephir-lang/zephir
src/Operators/AbstractOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/AbstractOperator.php
MIT
public function isExpecting(): bool { return $this->expecting; }
Sets if the result of the evaluated expression is read only. @return bool
isExpecting
php
zephir-lang/zephir
src/Operators/AbstractOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/AbstractOperator.php
MIT
public function isReadOnly(): bool { return $this->readOnly; }
Checks if the result of the evaluated expression is read only. @return bool
isReadOnly
php
zephir-lang/zephir
src/Operators/AbstractOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/AbstractOperator.php
MIT
public function setExpectReturn(bool $expecting, ?Variable $expectingVariable = null): void { $this->expecting = $expecting; $this->expectingVariable = $expectingVariable; }
Sets if the variable must be resolved into a direct variable symbol create a temporary value or ignore the return value. @param bool $expecting @param Variable|null $expectingVariable
setExpectReturn
php
zephir-lang/zephir
src/Operators/AbstractOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/AbstractOperator.php
MIT
public function setReadOnly(bool $readOnly): void { $this->readOnly = $readOnly; }
Sets if the result of the evaluated expression is read only. @param bool $readOnly
setReadOnly
php
zephir-lang/zephir
src/Operators/AbstractOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/AbstractOperator.php
MIT
public function compile($expression, CompilationContext $compilationContext) { $this->checkLeft($expression, CompilerException::class, $expression); $this->checkRight($expression, CompilerException::class, $expression); // Check for constant folding optimizations if ($optimized = $this->optimizeConstantFolding($expression, $compilationContext)) { return $optimized; } $leftExpr = new Expression($expression['left']); $leftExpr->setReadOnly(true); $left = $leftExpr->compile($compilationContext); $rightExpr = new Expression($expression['right']); $rightExpr->setReadOnly(true); $right = $rightExpr->compile($compilationContext); switch ($left->getType()) { case 'int': case 'uint': case 'long': case 'ulong': switch ($right->getType()) { case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'int', '(' . $left->getCode() . ' ' . $this->operator . ' ' . $right->getCode() . ')', $expression ); case 'double': return new CompiledExpression( 'double', '((double) ' . $left->getCode() . ' ' . $this->operator . ' ' . $right->getCode() . ')', $expression ); case 'bool': return new CompiledExpression( 'int', '(' . $left->getCode() . ' ' . $this->operator . ' ' . $right->getBooleanCode() . ')', $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'bool': return new CompiledExpression( 'int', '(' . $left->getCode() . ' ' . $this->operator . ' ' . $variableRight->getName( ) . ')', $expression ); case 'double': return new CompiledExpression( 'double', '(double) (' . $left->getCode( ) . ' ' . $this->operator . ' ' . $variableRight->getName() . ')', $expression ); case 'variable': $compilationContext->headersManager->add('kernel/operators'); $variableRight = $compilationContext->backend->getVariableCode($variableRight); return new CompiledExpression( 'int', '(' . $left->getCode( ) . ' ' . $this->operator . ' zephir_get_numberval(' . $variableRight . '))', $expression ); default: throw new CompilerException( "Cannot operate variable('int') with variable('" . $variableRight->getType() . "')", $expression ); } default: throw new CompilerException( "Cannot operate 'int' with '" . $right->getType() . "'", $expression ); } case 'bool': return match ($right->getType()) { Types::T_INT, Types::T_UINT, Types::T_LONG, Types::T_ULONG, Types::T_DOUBLE => new CompiledExpression( 'long', '(' . $left->getBooleanCode() . ' + ' . $right->getCode() . ')', $expression ), Types::T_BOOL => new CompiledExpression( 'bool', '(' . $left->getBooleanCode() . ' ' . $this->bitOperator . ' ' . $right->getBooleanCode() . ')', $expression ), default => throw new CompilerException( "Cannot operate 'bool' with '" . $right->getType() . "'", $expression ), }; case 'double': switch ($right->getType()) { case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'double', '(' . $left->getCode() . ' ' . $this->operator . ' (double) (' . $right->getCode() . '))', $expression ); case 'double': return new CompiledExpression( 'double', '(' . $left->getCode() . ' ' . $this->operator . ' ' . $right->getCode() . ')', $expression ); case 'bool': return new CompiledExpression( 'double', '(' . $left->getCode() . ' ' . $this->operator . ' ' . $right->getBooleanCode() . ')', $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'bool': return new CompiledExpression( 'double', '(' . $left->getCode() . ' ' . $this->operator . ' ' . $variableRight->getName( ) . ')', $expression ); case 'double': return new CompiledExpression( 'double', '(double) (' . $left->getCode( ) . ' ' . $this->operator . ' ' . $variableRight->getName() . ')', $expression ); case 'variable': $compilationContext->headersManager->add('kernel/operators'); $variableRight = $compilationContext->backend->getVariableCode( $variableRight, $compilationContext ); return new CompiledExpression( 'double', '(' . $left->getCode( ) . ' ' . $this->operator . ' zephir_get_numberval(' . $variableRight . '))', $expression ); default: throw new CompilerException( "Cannot operate variable('double') with variable('" . $variableRight->getType( ) . "')", $expression ); } default: throw new CompilerException( "Cannot operate 'double' with '" . $right->getType() . "'", $expression ); } case 'string': throw match ($right->getType()) { default => new CompilerException( 'Operation is not supported between strings', $expression ), }; case 'variable': $variableLeft = $compilationContext->symbolTable->getVariableForRead( $left->resolve(null, $compilationContext), $compilationContext, $expression ); switch ($variableLeft->getType()) { case 'int': case 'uint': case 'long': case 'ulong': switch ($right->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'double': return new CompiledExpression( 'int', '(' . $left->getCode() . ' ' . $this->operator . ' ' . $right->getCode() . ')', $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['right'] ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'bool': return new CompiledExpression( 'int', '(' . $variableLeft->getName( ) . ' ' . $this->operator . ' ' . $variableRight->getName() . ')', $expression ); case 'double': return new CompiledExpression( 'double', '((double) ' . $variableLeft->getName( ) . ' ' . $this->operator . ' ' . $variableRight->getName() . ')', $expression ); case 'variable': $compilationContext->headersManager->add('kernel/operators'); $variableRight = $compilationContext->backend->getVariableCode( $variableRight, $compilationContext ); return new CompiledExpression( 'int', '(' . $variableLeft->getName( ) . ' ' . $this->operator . ' zephir_get_numberval(' . $variableRight . '))', $expression ); default: throw new CompilerException( "Cannot operate variable('int') with variable('" . $variableRight->getType( ) . "')", $expression ); } default: throw new CompilerException( "Cannot operate variable('int') with '" . $right->getType() . "'", $expression ); } case 'char': switch ($right->getType()) { case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'int', '(' . $left->getCode() . ' ' . $this->operator . ' ' . $right->getCode() . ')', $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['right'] ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'int', '(' . $variableLeft->getName( ) . ' ' . $this->operator . ' ' . $variableRight->getName() . ')', $expression ); case 'variable': $compilationContext->headersManager->add('kernel/operators'); $variableRight = $compilationContext->backend->getVariableCode( $variableRight, $compilationContext ); return new CompiledExpression( 'int', '(' . $variableLeft->getName( ) . ' ' . $this->operator . ' zephir_get_numberval(' . $variableRight . '))', $expression ); default: throw new CompilerException( "Cannot operate variable('char') with variable('" . $variableRight->getType( ) . "')", $expression ); } default: throw new CompilerException( "Cannot operate variable('char') with '" . $right->getType() . "'", $expression ); } case 'bool': switch ($right->getType()) { case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'bool', '(' . $left->getCode() . ' ' . $this->operator . ' ' . $right->getCode() . ')', $expression ); case 'bool': return new CompiledExpression( 'bool', '(' . $left->getCode() . ' ' . $this->bitOperator . ' ' . $right->getBooleanCode( ) . ')', $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['right'] ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'double': return new CompiledExpression( 'int', '(' . $variableLeft->getName( ) . ' ' . $this->operator . ' ' . $variableRight->getName() . ')', $expression ); case 'bool': return new CompiledExpression( 'bool', '(' . $variableLeft->getName( ) . ' ' . $this->bitOperator . ' ' . $variableRight->getName() . ')', $expression ); case 'variable': $compilationContext->headersManager->add('kernel/operators'); $variableRight = $compilationContext->backend->getVariableCode( $variableRight, $compilationContext ); return new CompiledExpression( 'int', '(' . $variableLeft->getName( ) . ' ' . $this->operator . ' zephir_get_numberval(' . $variableRight . '))', $expression ); default: throw new CompilerException( "Cannot operate variable('int') with variable('" . $variableRight->getType( ) . "')", $expression ); } default: throw new CompilerException( "Cannot operate variable('int') with '" . $right->getType() . "'", $expression ); } case 'double': switch ($right->getType()) { case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'double', '(' . $left->getCode() . ' ' . $this->operator . ' (double) ' . $right->getCode( ) . ')', $expression ); case 'double': return new CompiledExpression( 'double', '(' . $left->getCode() . ' ' . $this->operator . ' ' . $right->getCode() . ')', $expression ); case 'bool': return new CompiledExpression( 'bool', '(' . $left->getCode() . ' ' . $this->bitOperator . ' ' . $right->getBooleanCode( ) . ')', $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['right'] ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'double', '(' . $variableLeft->getName( ) . ' ' . $this->operator . ' (double) ' . $variableRight->getName() . ')', $expression ); case 'double': return new CompiledExpression( 'double', '(' . $variableLeft->getName( ) . ' ' . $this->operator . ' ' . $variableRight->getName() . ')', $expression ); case 'bool': return new CompiledExpression( 'bool', '(' . $variableLeft->getName( ) . ' ' . $this->bitOperator . ' ' . $variableRight->getName() . ')', $expression ); case 'variable': $compilationContext->headersManager->add('kernel/operators'); $variableRight = $compilationContext->backend->getVariableCode($variableRight); return new CompiledExpression( 'int', '(' . $variableLeft->getName( ) . ' ' . $this->operator . ' zephir_get_numberval(' . $variableRight . '))', $expression ); default: throw new CompilerException( "Cannot operate variable('double') with variable('" . $variableRight->getType( ) . "')", $expression ); } default: throw new CompilerException( "Cannot operate variable('int') with '" . $right->getType() . "'", $expression ); } case 'string': throw new CompilerException("Cannot operate string variables'", $expression); case 'array': switch ($right->getType()) { /* a(var) + a(x) */ case 'array': case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->resolve(null, $compilationContext), $compilationContext, $expression ); switch ($variableRight->getType()) { /* a(var) + a(var) */ case 'array': case 'variable': $compilationContext->headersManager->add('kernel/operators'); $expected = $this->getExpected($compilationContext, $expression); $compilationContext->backend->zvalOperator( $this->zvalOperator, $expected, $variableLeft, $variableRight, $compilationContext ); $this->checkVariableTemporal($variableLeft); $this->checkVariableTemporal($variableRight); $expected->setDynamicTypes( $this->getDynamicTypes($variableLeft, $variableRight) ); return new CompiledExpression('variable', $expected->getName(), $expression); default: throw new CompilerException( "Cannot operate 'array with variable ('" . $variableRight->getType() . "')", $expression ); } } // no break case 'variable': switch ($right->getType()) { /* a + 1 */ case 'int': case 'uint': case 'long': case 'ulong': case 'double': $compilationContext->headersManager->add('kernel/operators'); $op = $this->operator; $op1 = $compilationContext->backend->getVariableCode($variableLeft); $op2 = $right->getCode(); if ('double' == $right->getType()) { return new CompiledExpression( 'double', '(zephir_get_numberval(' . $op1 . ') ' . $op . ' ' . $op2 . ')', $expression ); } else { return new CompiledExpression( 'int', '(zephir_get_numberval(' . $op1 . ') ' . $op . ' ' . $op2 . ')', $expression ); } /* a(var) + a(x) */ case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->resolve(null, $compilationContext), $compilationContext, $expression ); switch ($variableRight->getType()) { /* a(var) + a(int) */ case 'int': case 'uint': case 'long': case 'ulong': case 'double': $compilationContext->headersManager->add('kernel/operators'); $variableLeft = $compilationContext->backend->getVariableCode($variableLeft); return new CompiledExpression( 'double' == $variableRight->getType() ? 'double' : 'int', sprintf( '(zephir_get_numberval(%s) %s %s)', $variableLeft, $this->operator, $variableRight->getName() ), $expression ); /* a(var) + a(bool) */ case 'bool': $compilationContext->headersManager->add('kernel/operators'); $variableLeft = $compilationContext->backend->getVariableCode($variableLeft); return new CompiledExpression( 'int', '(zephir_get_numberval(' . $variableLeft . ') ' . $this->operator . ' ' . $variableRight->getName( ) . ')', $expression ); /* a(var) + a(var) */ case 'variable': case 'array': $compilationContext->headersManager->add('kernel/operators'); $expected = $this->getExpected($compilationContext, $expression); $compilationContext->backend->zvalOperator( $this->zvalOperator, $expected, $variableLeft, $variableRight, $compilationContext ); $this->checkVariableTemporal($variableLeft); $this->checkVariableTemporal($variableRight); $expected->setDynamicTypes( $this->getDynamicTypes($variableLeft, $variableRight) ); return new CompiledExpression('variable', $expected->getName(), $expression); default: throw new CompilerException( "Cannot operate 'variable' with variable ('" . $variableRight->getType( ) . "')", $expression ); } default: throw new CompilerException( "Cannot operate 'variable' with '" . $right->getType() . "'", $expression ); } default: throw CompilerException::unknownType($variableLeft, $expression); } // no break default: throw CompilerException::unsupportedType($left, $expression); } }
Compiles the arithmetical operation. @param array $expression @param CompilationContext $compilationContext @return CompiledExpression @throws Exception @throws CompilerException
compile
php
zephir-lang/zephir
src/Operators/Arithmetical/ArithmeticalBaseOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Arithmetical/ArithmeticalBaseOperator.php
MIT
public function optimizeConstantFolding(array $expression, CompilationContext $compilationContext) { if ('int' != $expression['left']['type'] && 'double' != $expression['left']['type']) { return false; } if ($compilationContext->config->get('constant-folding', 'optimizations')) { if ('int' == $expression['left']['type'] && 'int' == $expression['right']['type']) { switch ($this->operator) { case '+': return new CompiledExpression( 'int', $expression['left']['value'] + $expression['right']['value'], $expression ); case '-': return new CompiledExpression( 'int', $expression['left']['value'] - $expression['right']['value'], $expression ); case '*': return new CompiledExpression( 'int', $expression['left']['value'] * $expression['right']['value'], $expression ); } } if (('double' == $expression['left']['type'] && 'double' == $expression['right']['type']) || ('double' == $expression['left']['type'] && 'int' == $expression['right']['type']) || ('int' == $expression['left']['type'] && 'double' == $expression['right']['type'])) { switch ($this->operator) { case '+': return new CompiledExpression( 'double', $expression['left']['value'] + $expression['right']['value'], $expression ); case '-': return new CompiledExpression( 'double', $expression['left']['value'] - $expression['right']['value'], $expression ); case '*': return new CompiledExpression( 'double', $expression['left']['value'] * $expression['right']['value'], $expression ); } } } return false; }
This tries to perform arithmetical operations. Probably gcc/clang will optimize them without this optimization @see https://en.wikipedia.org/wiki/Constant_folding @param array $expression @param CompilationContext $compilationContext @return bool|CompiledExpression
optimizeConstantFolding
php
zephir-lang/zephir
src/Operators/Arithmetical/ArithmeticalBaseOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Arithmetical/ArithmeticalBaseOperator.php
MIT
protected function preCompileChecks( array $expression, CompilationContext $compilationContext ): array { $this->checkLeft($expression); $this->checkRight($expression); $leftExpr = new Expression($expression['left']); $leftExpr->setReadOnly(true); $left = $leftExpr->compile($compilationContext); $rightExpr = new Expression($expression['right']); $rightExpr->setReadOnly(true); $right = $rightExpr->compile($compilationContext); $compilationContext->headersManager->add('kernel/operators'); return [$left, $right]; }
@param array $expression @param CompilationContext $compilationContext @return array @throws ReflectionException @throws ZephirException
preCompileChecks
php
zephir-lang/zephir
src/Operators/Arithmetical/ArithmeticalBaseOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Arithmetical/ArithmeticalBaseOperator.php
MIT
protected function processLeftBoolean( CompiledExpression $right, CompiledExpression $left, array $expression ): CompiledExpression { return match ($right->getType()) { Types::T_INT, Types::T_UINT, Types::T_LONG, Types::T_ULONG, Types::T_DOUBLE => new CompiledExpression( 'long', '(' . $left->getBooleanCode() . ' - ' . $right->getCode() . ')', $expression ), Types::T_BOOL => new CompiledExpression( 'bool', '(' . $left->getBooleanCode() . ' ' . $this->bitOperator . ' ' . $right->getBooleanCode() . ')', $expression ), default => throw new CompilerException( "Cannot operate 'bool' with '" . $right->getType() . "'", $expression ), }; }
@param CompiledExpression $right @param CompiledExpression $left @param array $expression @return CompiledExpression
processLeftBoolean
php
zephir-lang/zephir
src/Operators/Arithmetical/ArithmeticalBaseOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Arithmetical/ArithmeticalBaseOperator.php
MIT
private function getDynamicTypes(Variable $left, Variable $right): string { if ('/' === $this->operator) { return Types::T_DOUBLE; } switch ($left->getType()) { case Types::T_INT: case Types::T_UINT: case Types::T_LONG: case Types::T_ULONG: switch ($right->getType()) { case Types::T_INT: case Types::T_UINT: case Types::T_LONG: case Types::T_ULONG: return Types::T_INT; } break; } return Types::T_DOUBLE; }
Returns proper dynamic types. @param Variable $left @param Variable $right @return string
getDynamicTypes
php
zephir-lang/zephir
src/Operators/Arithmetical/ArithmeticalBaseOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Arithmetical/ArithmeticalBaseOperator.php
MIT
public function compile($expression, CompilationContext $compilationContext): CompiledExpression|bool { [$left, $right] = $this->preCompileChecks($expression, $compilationContext); switch ($left->getType()) { case 'int': case 'uint': case 'long': case 'ulong': switch ($right->getType()) { case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'double', 'zephir_safe_div_long_long(' . $left->getCode() . ', ' . $right->getCode() . ')', $expression ); case 'double': return new CompiledExpression( 'double', 'zephir_safe_div_long_double((double) ' . $left->getCode() . ', ' . $right->getCode() . ')', $expression ); case 'bool': return new CompiledExpression( 'bool', '(' . $left->getCode() . ' - ' . $right->getBooleanCode() . ')', $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'bool': return new CompiledExpression( 'double', 'zephir_safe_div_long_long(' . $left->getCode() . ', ' . $variableRight->getName( ) . ')', $expression ); case 'double': return new CompiledExpression( 'double', 'zephir_safe_div_long_double(' . $left->getCode() . ', ' . $variableRight->getName( ) . ')', $expression ); case 'variable': $variableRightCode = $compilationContext->backend->getVariableCode($variableRight); return new CompiledExpression( 'double', 'zephir_safe_div_long_zval(' . $left->getCode() . ', ' . $variableRightCode . ')', $expression ); default: throw new CompilerException( "Cannot operate variable('int') with variable('" . $variableRight->getType() . "')", $expression ); } default: throw new CompilerException( "Cannot operate 'int' with '" . $right->getType() . "'", $expression ); } case 'bool': return $this->processLeftBoolean($right, $left, $expression); case 'double': switch ($right->getType()) { case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'double', 'zephir_safe_div_double_long(' . $left->getCode() . ', (double) (' . $right->getCode( ) . '))', $expression ); case 'double': return new CompiledExpression( 'double', 'zephir_safe_div_double_long(' . $left->getCode() . ', ' . $right->getCode() . ')', $expression ); case 'bool': return new CompiledExpression( 'double', 'zephir_safe_div_double_long(' . $left->getCode() . ', ' . $right->getBooleanCode() . ')', $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'bool': return new CompiledExpression( 'double', 'zephir_safe_div_double_long(' . $left->getCode() . ', ' . $variableRight->getName( ) . ')', $expression ); case 'double': return new CompiledExpression( 'double', 'zephir_safe_div_double_double(' . $left->getCode( ) . ', ' . $variableRight->getName() . ')', $expression ); case 'variable': $symbolRight = $compilationContext->backend->getVariableCode($variableRight); return new CompiledExpression( 'double', 'zephir_safe_div_double_zval(' . $left->getCode() . ', ' . $symbolRight . ')', $expression ); default: throw new CompilerException( "Cannot operate variable('double') with variable('" . $variableRight->getType( ) . "')", $expression ); } default: throw new CompilerException( "Cannot operate 'double' with '" . $right->getType() . "'", $expression ); } case 'string': case 'array': throw match ($right->getType()) { default => new CompilerException( 'Operation is not supported between ' . $right->getType(), $expression ), }; case 'variable': $variableLeft = $compilationContext->symbolTable->getVariableForRead( $left->resolve(null, $compilationContext), $compilationContext, $expression ); switch ($variableLeft->getType()) { case 'int': case 'uint': case 'long': case 'ulong': switch ($right->getType()) { case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'double', 'zephir_safe_div_long_long(' . $left->getCode() . ', ' . $right->getCode() . ')', $expression ); case 'double': return new CompiledExpression( 'double', 'zephir_safe_div_long_double(' . $left->getCode() . ', ' . $right->getCode() . ')', $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['right'] ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'bool': return new CompiledExpression( 'double', 'zephir_safe_div_long_long(' . $variableLeft->getName( ) . ', ' . $variableRight->getName() . ')', $expression ); case 'double': return new CompiledExpression( 'double', 'zephir_safe_div_long_double(' . $variableLeft->getName( ) . ', ' . $variableRight->getName() . ')', $expression ); case 'variable': $compilationContext->headersManager->add('kernel/operators'); if ($variableRight->isLocalOnly()) { return new CompiledExpression( 'double', 'zephir_safe_div_long_zval(' . $variableLeft->getName( ) . ', &' . $variableRight->getName() . ')', $expression ); } else { $variableRightCode = $compilationContext->backend->getVariableCode( $variableRight ); return new CompiledExpression( 'double', 'zephir_safe_div_long_zval(' . $variableLeft->getName( ) . ', ' . $variableRightCode . ')', $expression ); } default: throw new CompilerException( "Cannot operate variable('int') with variable('" . $variableRight->getType( ) . "')", $expression ); } default: throw new CompilerException( "Cannot operate variable('int') with '" . $right->getType() . "'", $expression ); } case 'bool': switch ($right->getType()) { case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'bool', '(' . $left->getCode() . ', ' . $right->getCode() . ')', $expression ); case 'bool': return new CompiledExpression( 'bool', '(' . $left->getCode() . ' ' . $this->bitOperator . ' ' . $right->getBooleanCode( ) . ')', $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['right'] ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'double', 'zephir_safe_div_long_long(' . $variableLeft->getName() . ', ' . $variableRight->getName() . ')', $expression ); case 'double': return new CompiledExpression( 'double', 'zephir_safe_div_long_double(' . $variableLeft->getName() . ', ' . $variableRight->getName() . ')', $expression ); case 'bool': return new CompiledExpression( 'double', 'zephir_safe_div_long_long(' . $variableLeft->getName() . ' ' . $this->bitOperator . ' ' . $variableRight->getName() . ')', $expression ); case 'variable': $compilationContext->headersManager->add('kernel/operators'); if ($variableRight->isLocalOnly()) { return new CompiledExpression( 'double', 'zephir_safe_div_long_zval(' . $variableLeft->getName() . ', &' . $variableRight->getName() . ')', $expression ); } else { $variableRightCode = $compilationContext->backend->getVariableCode( $variableRight ); return new CompiledExpression( 'double', 'zephir_safe_div_long_zval(' . $variableLeft->getName() . ', ' . $variableRightCode . ')', $expression ); } default: throw new CompilerException( "Cannot operate variable('int') with variable('" . $variableRight->getType() . "')", $expression ); } default: throw new CompilerException( "Cannot operate variable('int') with '" . $right->getType() . "'", $expression ); } case 'double': switch ($right->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'double': return new CompiledExpression( 'double', 'zephir_safe_div_double_long(' . $left->getCode() . ', ' . $right->getCode() . ')', $expression ); case 'bool': return new CompiledExpression( 'bool', '(' . $left->getCode() . ' ' . $this->bitOperator . ' ' . $right->getBooleanCode( ) . ')', $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['right'] ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'double', 'zephir_safe_div_double_long(' . $variableLeft->getName( ) . ', (double) ' . $variableRight->getName() . ')', $expression ); case 'double': return new CompiledExpression( 'double', 'zephir_safe_div_double_long(' . $variableLeft->getName( ) . ', ' . $variableRight->getName() . ')', $expression ); case 'bool': return new CompiledExpression( 'bool', '(' . $variableLeft->getName( ) . ' ' . $this->bitOperator . ' ' . $variableRight->getName() . ')', $expression ); case 'variable': $compilationContext->headersManager->add('kernel/operators'); $symbolRight = $compilationContext->backend->getVariableCode($variableRight); return new CompiledExpression( 'double', 'zephir_safe_div_double_zval(' . $variableLeft->getName() . ', ' . $symbolRight . ')', $expression ); default: throw new CompilerException( "Cannot operate variable('double') with variable('" . $variableRight->getType( ) . "')", $expression ); } default: throw new CompilerException( "Cannot operate variable('int') with '" . $right->getType() . "'", $expression ); } case 'string': case 'array': throw new CompilerException( 'Cannot operate ' . $variableLeft->getType() . " variables'", $expression ); case 'variable': $op1 = $compilationContext->backend->getVariableCode($variableLeft); switch ($right->getType()) { /* a + 1 */ case 'int': case 'uint': case 'long': case 'ulong': $op2 = $right->getCode(); return new CompiledExpression( 'double', 'zephir_safe_div_zval_long(' . $op1 . ', ' . $op2 . ')', $expression ); case 'double': $op2 = $right->getCode(); return new CompiledExpression( 'double', 'zephir_safe_div_zval_double(' . $op1 . ', ' . $op2 . ')', $expression ); /* a(var) + a(x) */ case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->resolve(null, $compilationContext), $compilationContext, $expression ); switch ($variableRight->getType()) { /* a(var) + a(int) */ case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'double', 'zephir_safe_div_zval_long(' . $op1 . ', ' . $variableRight->getName( ) . ')', $expression ); case 'double': return new CompiledExpression( 'double', 'zephir_safe_div_zval_double(' . $op1 . ', ' . $variableRight->getName( ) . ')', $expression ); /* a(var) + a(bool) */ case 'bool': return new CompiledExpression( 'int', 'zephir_safe_div_zval_long(' . $op1 . ', ' . $variableRight->getName( ) . ')', $expression ); /* a(var) + a(var) */ case 'variable': $op2 = $compilationContext->backend->getVariableCode($variableRight); $expected = $this->getExpected($compilationContext, $expression); $expectedCode = $compilationContext->backend->getVariableCode($expected); $compilationContext->codePrinter->output( $this->zvalOperator . '(' . $expectedCode . ', ' . $op1 . ', ' . $op2 . ');' ); $this->checkVariableTemporal($variableLeft); $this->checkVariableTemporal($variableRight); return new CompiledExpression( 'variable', $expected->getName(), $expression ); default: throw new CompilerException( "Cannot operate 'variable' with variable ('" . $variableRight->getType( ) . "')", $expression ); } default: throw new CompilerException( "Cannot operate 'variable' with '" . $right->getType() . "'", $expression ); } default: throw CompilerException::unknownType($variableLeft, $expression); } default: throw CompilerException::unsupportedType($left, $expression); } }
Compiles the arithmetical division operation. @throws ReflectionException @throws Exception
compile
php
zephir-lang/zephir
src/Operators/Arithmetical/DivOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Arithmetical/DivOperator.php
MIT
public function compile($expression, CompilationContext $compilationContext) { [$left, $right] = $this->preCompileChecks($expression, $compilationContext); switch ($left->getType()) { case Types::T_INT: case Types::T_UINT: case Types::T_LONG: case Types::T_ULONG: switch ($right->getType()) { case Types::T_INT: case Types::T_UINT: case Types::T_LONG: case Types::T_ULONG: return new CompiledExpression( 'double', 'zephir_safe_mod_long_long(' . $left->getCode() . ', ' . $right->getCode() . ')', $expression ); case Types::T_DOUBLE: return new CompiledExpression( 'double', 'zephir_safe_mod_long_double((double) ' . $left->getCode() . ', ' . $right->getCode() . ')', $expression ); case Types::T_BOOL: return new CompiledExpression( 'bool', '(' . $left->getCode() . ' - ' . $right->getBooleanCode() . ')', $expression ); case Types::T_VARIABLE: $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression ); return match ($variableRight->getType()) { Types::T_INT, Types::T_UINT, Types::T_LONG, Types::T_ULONG, Types::T_BOOL => new CompiledExpression( 'double', 'zephir_safe_mod_long_long(' . $left->getCode() . ', ' . $variableRight->getName() . ')', $expression ), Types::T_DOUBLE => new CompiledExpression( 'double', 'zephir_safe_mod_long_double(' . $left->getCode() . ', ' . $variableRight->getName() . ')', $expression ), Types::T_VARIABLE => new CompiledExpression( 'double', 'zephir_safe_mod_long_zval(' . $left->getCode() . ', ' . $this->getIsLocal($variableRight) . $variableRight->getName() . ')', $expression ), default => throw new CompilerException( "Cannot operate variable('int') with variable('" . $variableRight->getType() . "')", $expression ), }; default: throw new CompilerException( "Cannot operate 'int' with '" . $right->getType() . "'", $expression ); } case Types::T_BOOL: return $this->processLeftBoolean($left, $right, $expression); case Types::T_DOUBLE: switch ($right->getType()) { case Types::T_INT: case Types::T_UINT: case Types::T_LONG: case Types::T_ULONG: return new CompiledExpression( 'double', 'zephir_safe_mod_double_long(' . $left->getCode() . ', (double) (' . $right->getCode( ) . '))', $expression ); case Types::T_DOUBLE: return new CompiledExpression( 'double', 'zephir_safe_mod_double_long(' . $left->getCode() . ', ' . $right->getCode() . ')', $expression ); case Types::T_BOOL: return new CompiledExpression( 'double', 'zephir_safe_mod_double_long(' . $left->getCode() . ', ' . $right->getBooleanCode() . ')', $expression ); case Types::T_VARIABLE: $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression ); return match ($variableRight->getType()) { Types::T_INT, Types::T_UINT, Types::T_LONG, Types::T_ULONG, Types::T_BOOL => new CompiledExpression( 'double', 'zephir_safe_mod_double_long(' . $left->getCode() . ', ' . $variableRight->getName() . ')', $expression ), Types::T_DOUBLE => new CompiledExpression( 'double', 'zephir_safe_mod_double_double(' . $left->getCode() . ', ' . $variableRight->getName() . ')', $expression ), Types::T_VARIABLE => new CompiledExpression( 'double', 'zephir_safe_mod_double_zval(' . $left->getCode() . ', ' . $this->getIsLocal($variableRight) . $variableRight->getName() . ')', $expression ), default => throw new CompilerException( "Cannot operate variable('double') with variable('" . $variableRight->getType() . "')", $expression ), }; default: throw new CompilerException( "Cannot operate 'double' with '" . $right->getType() . "'", $expression ); } case Types::T_STRING: case Types::T_ARRAY: throw new CompilerException( 'Operation is not supported between ' . $right->getType(), $expression ); case Types::T_VARIABLE: $variableLeft = $compilationContext->symbolTable->getVariableForRead( $left->resolve(null, $compilationContext), $compilationContext, $expression ); switch ($variableLeft->getType()) { case Types::T_INT: case Types::T_UINT: case Types::T_LONG: case Types::T_ULONG: switch ($right->getType()) { case Types::T_INT: case Types::T_UINT: case Types::T_LONG: case Types::T_ULONG: return new CompiledExpression( 'double', 'zephir_safe_mod_long_long(' . $left->getCode() . ', ' . $right->getCode() . ')', $expression ); case Types::T_DOUBLE: return new CompiledExpression( 'double', 'zephir_safe_mod_long_double(' . $left->getCode() . ', ' . $right->getCode() . ')', $expression ); case Types::T_VARIABLE: $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['right'] ); return match ($variableRight->getType()) { Types::T_INT, Types::T_UINT, Types::T_LONG, Types::T_ULONG, Types::T_BOOL => new CompiledExpression( 'double', 'zephir_safe_mod_long_long(' . $variableLeft->getName() . ', ' . $variableRight->getName() . ')', $expression ), Types::T_DOUBLE => new CompiledExpression( 'double', 'zephir_safe_mod_long_double(' . $variableLeft->getName() . ', ' . $variableRight->getName() . ')', $expression ), Types::T_VARIABLE => new CompiledExpression( 'double', 'zephir_safe_mod_long_zval(' . $variableLeft->getName() . ', ' . $this->getIsLocal($variableRight) . $variableRight->getName() . ')', $expression ), default => throw new CompilerException( "Cannot operate variable('int') with variable('" . $variableRight->getType() . "')", $expression ), }; default: throw new CompilerException( "Cannot operate variable('int') with '" . $right->getType() . "'", $expression ); } case Types::T_BOOL: switch ($right->getType()) { case Types::T_INT: case Types::T_UINT: case Types::T_LONG: case Types::T_ULONG: return new CompiledExpression( 'bool', '(' . $left->getCode() . ', ' . $right->getCode() . ')', $expression ); case Types::T_BOOL: return new CompiledExpression( 'bool', '(' . $left->getCode() . ' ' . $this->bitOperator . ' ' . $right->getBooleanCode( ) . ')', $expression ); case Types::T_VARIABLE: $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['right'] ); switch ($variableRight->getType()) { case Types::T_INT: case Types::T_UINT: case Types::T_LONG: case Types::T_ULONG: return new CompiledExpression( 'double', 'zephir_safe_mod_long_long(' . $variableLeft->getName() . ', ' . $variableRight->getName() . ')', $expression ); case Types::T_DOUBLE: return new CompiledExpression( 'double', 'zephir_safe_mod_long_double(' . $variableLeft->getName() . ', ' . $variableRight->getName() . ')', $expression ); case Types::T_BOOL: return new CompiledExpression( 'double', 'zephir_safe_mod_long_long(' . $variableLeft->getName() . ' ' . $this->bitOperator . ' ' . $variableRight->getName() . ')', $expression ); case Types::T_VARIABLE: $compilationContext->headersManager->add('kernel/operators'); return new CompiledExpression( 'double', 'zephir_safe_mod_long_zval(' . $variableLeft->getName() . ', ' . $this->getIsLocal($variableRight) . $variableRight->getName() . ')', $expression ); default: throw new CompilerException( "Cannot operate variable('int') with variable('" . $variableRight->getType() . "')", $expression ); } default: throw new CompilerException( "Cannot operate variable('int') with '" . $right->getType() . "'", $expression ); } case Types::T_DOUBLE: switch ($right->getType()) { case Types::T_INT: case Types::T_UINT: case Types::T_LONG: case Types::T_ULONG: case Types::T_DOUBLE: return new CompiledExpression( 'double', 'zephir_safe_mod_double_long(' . $left->getCode() . ', ' . $right->getCode() . ')', $expression ); case Types::T_BOOL: return new CompiledExpression( 'bool', '(' . $left->getCode() . ' ' . $this->bitOperator . ' ' . $right->getBooleanCode( ) . ')', $expression ); case Types::T_VARIABLE: $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['right'] ); switch ($variableRight->getType()) { case Types::T_INT: case Types::T_UINT: case Types::T_LONG: case Types::T_ULONG: return new CompiledExpression( 'double', 'zephir_safe_mod_double_long(' . $variableLeft->getName( ) . ', (double) ' . $variableRight->getName() . ')', $expression ); case Types::T_DOUBLE: return new CompiledExpression( 'double', 'zephir_safe_mod_double_long(' . $variableLeft->getName( ) . ', ' . $variableRight->getName() . ')', $expression ); case Types::T_BOOL: return new CompiledExpression( 'bool', '(' . $variableLeft->getName( ) . ' ' . $this->bitOperator . ' ' . $variableRight->getName() . ')', $expression ); case Types::T_VARIABLE: $compilationContext->headersManager->add('kernel/operators'); return new CompiledExpression( 'double', 'zephir_safe_mod_double_zval(' . $variableLeft->getName() . ', ' . $this->getIsLocal($variableRight) . $variableRight->getName() . ')', $expression ); default: throw new CompilerException( "Cannot operate variable('double') with variable('" . $variableRight->getType( ) . "')", $expression ); } default: throw new CompilerException( "Cannot operate variable('int') with '" . $right->getType() . "'", $expression ); } case Types::T_STRING: case Types::T_ARRAY: throw new CompilerException( 'Cannot operate ' . $variableLeft->getType() . " variables'", $expression ); case Types::T_VARIABLE: $op1 = $compilationContext->backend->getVariableCode($variableLeft); switch ($right->getType()) { /* a + 1 */ case Types::T_INT: case Types::T_UINT: case Types::T_LONG: case Types::T_ULONG: $op2 = $right->getCode(); return new CompiledExpression( 'double', 'zephir_safe_mod_zval_long(' . $op1 . ', ' . $op2 . ')', $expression ); case Types::T_DOUBLE: $op2 = $right->getCode(); return new CompiledExpression( 'double', 'zephir_safe_mod_zval_double(' . $op1 . ', ' . $op2 . ')', $expression ); /* a(var) + a(x) */ case Types::T_VARIABLE: $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->resolve(null, $compilationContext), $compilationContext, $expression ); switch ($variableRight->getType()) { /* a(var) + a(int) */ case Types::T_INT: case Types::T_UINT: case Types::T_LONG: case Types::T_ULONG: return new CompiledExpression( 'double', 'zephir_safe_mod_zval_long(' . $op1 . ', ' . $variableRight->getName() . ')', $expression ); /* a(var) + a(bool) */ case Types::T_BOOL: return new CompiledExpression( 'int', 'zephir_safe_mod_zval_long(' . $op1 . ', ' . $variableRight->getName() . ')', $expression ); /* a(var) + a(var) */ case Types::T_VARIABLE: $compilationContext->headersManager->add('kernel/operators'); $op2 = $compilationContext->backend->getVariableCode($variableRight); $expected = $this->getExpected($compilationContext, $expression); $symbol = $compilationContext->backend->getVariableCode($expected); $compilationContext->codePrinter->output( $this->zvalOperator . '(' . $symbol . ', ' . $op1 . ', ' . $op2 . ');' ); $this->checkVariableTemporal($variableLeft); $this->checkVariableTemporal($variableRight); return new CompiledExpression( 'variable', $expected->getName(), $expression ); default: throw new CompilerException( "Cannot operate 'variable' with variable ('" . $variableRight->getType() . "')", $expression ); } default: throw new CompilerException( "Cannot operate 'variable' with '" . $right->getType() . "'", $expression ); } default: throw CompilerException::unknownType($variableLeft, $expression); } default: throw CompilerException::unsupportedType($left, $expression); } }
Compiles the arithmetical modulus operation. @param array $expression @param CompilationContext $compilationContext
compile
php
zephir-lang/zephir
src/Operators/Arithmetical/ModOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Arithmetical/ModOperator.php
MIT
public function compile($expression, CompilationContext $compilationContext) { $this->checkLeft($expression, CompilerException::class, $expression); $this->checkRight($expression, CompilerException::class, $expression); /** * Check for constant folding optimizations. */ $optimized = $this->optimizeConstantFolding($expression, $compilationContext); if ($optimized !== null) { return $optimized; } $leftExpr = new Expression($expression['left']); $leftExpr->setReadOnly(true); $left = $leftExpr->compile($compilationContext); $rightExpr = new Expression($expression['right']); $rightExpr->setReadOnly(true); $right = $rightExpr->compile($compilationContext); switch ($left->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'char': case 'uchar': switch ($right->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'char': case 'uchar': return new CompiledExpression( 'int', '(' . $left->getCode() . ' ' . $this->operator . ' ' . $right->getCode() . ')', $expression ); case 'double': return new CompiledExpression( 'int', '(' . $left->getCode() . ' ' . $this->operator . ' (int) (' . $right->getCode() . '))', $expression ); case 'bool': return new CompiledExpression( 'int', '(' . $left->getCode() . ' ' . $this->operator . ' ' . $right->getBooleanCode() . ')', $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'bool': case 'ulong': return new CompiledExpression( 'int', '(' . $left->getCode() . ' ' . $this->operator . ' ' . $variableRight->getName( ) . ')', $expression ); case 'double': return new CompiledExpression( 'int', '(' . $left->getCode( ) . ' ' . $this->operator . ' (int) (' . $variableRight->getName() . '))', $expression ); case 'variable': $compilationContext->headersManager->add('kernel/operators'); $symbol = $compilationContext->backend->getVariableCode($variableRight); return new CompiledExpression( 'int', '(' . $left->getCode( ) . ' ' . $this->operator . ' zephir_get_numberval(' . $symbol . '))', $expression ); default: throw new CompilerException( "Cannot operate variable('int') with variable('" . $variableRight->getType() . "')", $expression ); } default: throw new CompilerException( "Cannot operate 'int' with '" . $right->getType() . "'", $expression ); } case 'bool': switch ($right->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'double': return new CompiledExpression( 'int', '(' . $left->getBooleanCode() . ' ' . $this->bitOperator . '((' . $right->getCode( ) . ') ? 1 : 0))', $expression ); case 'bool': return new CompiledExpression( 'int', '(' . $left->getBooleanCode() . ' ' . $this->bitOperator . ' ' . $right->getBooleanCode( ) . ')', $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $expression['right']['value'], $compilationContext, $expression ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'bool': case 'ulong': return new CompiledExpression( 'int', '((int) (' . $left->getBooleanCode( ) . ') ' . $this->operator . ' ' . $variableRight->getName() . ')', $expression ); case 'double': return new CompiledExpression( 'int', '((int) (' . $left->getBooleanCode( ) . ') ' . $this->operator . ' (int) (' . $variableRight->getName() . '))', $expression ); case 'variable': $compilationContext->headersManager->add('kernel/operators'); $symbol = $compilationContext->backend->getVariableCode($variableRight); return new CompiledExpression( 'int', '((int) (' . $left->getBooleanCode( ) . ') ' . $this->operator . ' zephir_get_numberval(' . $symbol . '))', $expression ); default: throw new CompilerException( "Cannot operate ('bool') with variable('" . $variableRight->getType() . "')", $expression ); } default: throw new CompilerException( "Cannot operate 'bool' with '" . $right->getType() . "'", $expression ); } case 'double': switch ($right->getType()) { case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'int', '((int) (' . $left->getCode() . ') ' . $this->operator . ' ' . $right->getCode() . ')', $expression ); case 'double': return new CompiledExpression( 'int', '((int) (' . $left->getCode() . ') ' . $this->operator . ' (int) (' . $right->getCode( ) . '))', $expression ); case 'bool': return new CompiledExpression( 'int', '((int) (' . $left->getCode() . ') ' . $this->operator . ' ' . $right->getBooleanCode( ) . ')', $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $expression['right']['value'], $compilationContext, $expression ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'bool': case 'ulong': return new CompiledExpression( 'int', '((int) (' . $left->getCode( ) . ') ' . $this->operator . ' ' . $variableRight->getName() . ')', $expression ); case 'double': return new CompiledExpression( 'int', '((int) (' . $left->getCode( ) . ') ' . $this->operator . ' (int) (' . $variableRight->getName() . '))', $expression ); case 'variable': $compilationContext->headersManager->add('kernel/operators'); $symbol = $compilationContext->backend->getVariableCode($variableRight); return new CompiledExpression( 'int', '((int) (' . $left->getCode( ) . ') ' . $this->operator . ' zephir_get_numberval(' . $symbol . '))', $expression ); default: throw new CompilerException( "Cannot operate variable('double') with variable('" . $variableRight->getType( ) . "')", $expression ); } default: throw new CompilerException( "Cannot operate 'double' with '" . $right->getType() . "'", $expression ); } case 'string': throw match ($right->getType()) { default => new CompilerException( 'Operation is not supported between strings', $expression ), }; case 'variable': $variableLeft = $compilationContext->symbolTable->getVariableForRead( $left->resolve(null, $compilationContext), $compilationContext, $expression ); switch ($variableLeft->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'char': case 'uchar': switch ($right->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'double': case 'char': case 'uchar': return new CompiledExpression( 'int', '(' . $left->getCode() . ' ' . $this->operator . ' ' . $right->getCode() . ')', $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['right'] ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'char': case 'bool': case 'uchar': return new CompiledExpression( 'int', '(' . $variableLeft->getName( ) . ' ' . $this->operator . ' ' . $variableRight->getName() . ')', $expression ); case 'double': return new CompiledExpression( 'int', '(' . $variableLeft->getName( ) . ' ' . $this->operator . ' (int) (' . $variableRight->getName() . '))', $expression ); case 'variable': $compilationContext->headersManager->add('kernel/operators'); $symbol = $compilationContext->backend->getVariableCode($variableRight); return new CompiledExpression( 'int', '(' . $variableLeft->getName( ) . ' ' . $this->operator . ' (int) (zephir_get_numberval(' . $symbol . ')))', $expression ); default: throw new CompilerException( "Cannot operate variable('int') with variable('" . $variableRight->getType( ) . "')", $expression ); } default: throw new CompilerException( "Cannot operate variable('int') with '" . $right->getType() . "'", $expression ); } case 'bool': switch ($right->getType()) { case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'int', '(' . $left->getCode() . ' ' . $this->operator . ' ' . $right->getCode() . ')', $expression ); case 'bool': return new CompiledExpression( 'int', '(' . $left->getCode() . ' ' . $this->bitOperator . ' ' . $right->getBooleanCode( ) . ')', $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['right'] ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'int', '(' . $variableLeft->getName( ) . ' ' . $this->operator . ' ' . $variableRight->getName() . ')', $expression ); case 'double': return new CompiledExpression( 'int', '(' . $variableLeft->getName( ) . ' ' . $this->operator . ' (int) (' . $variableRight->getName() . '))', $expression ); case 'bool': return new CompiledExpression( 'int', '(' . $variableLeft->getName( ) . ' ' . $this->bitOperator . ' ' . $variableRight->getName() . ')', $expression ); case 'variable': $compilationContext->headersManager->add('kernel/operators'); $symbol = $compilationContext->backend->getVariableCode($variableRight); return new CompiledExpression( 'int', '(' . $variableLeft->getName( ) . ' ' . $this->operator . ' zephir_get_numberval(' . $symbol . '))', $expression ); default: throw new CompilerException( "Cannot operate variable('int') with variable('" . $variableRight->getType( ) . "')", $expression ); } default: throw new CompilerException( "Cannot operate variable('int') with '" . $right->getType() . "'", $expression ); } case 'double': switch ($right->getType()) { case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'int', '((int) (' . $left->getCode() . ') ' . $this->operator . ' ' . $right->getCode( ) . ')', $expression ); case 'double': return new CompiledExpression( 'int', '((int) (' . $left->getCode( ) . ') ' . $this->operator . ' (int) (' . $right->getCode() . '))', $expression ); case 'bool': return new CompiledExpression( 'int', '((int) (' . $left->getCode( ) . ') ' . $this->bitOperator . ' ' . $right->getBooleanCode() . ')', $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $expression['right']['value'], $compilationContext, $expression['right'] ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'int', '((int) (' . $variableLeft->getName( ) . ') ' . $this->operator . ' ' . $variableRight->getName() . ')', $expression ); case 'double': return new CompiledExpression( 'int', '((int) (' . $variableLeft->getName( ) . ') ' . $this->operator . ' (int) (' . $variableRight->getName() . '))', $expression ); case 'bool': return new CompiledExpression( 'int', '((int) (' . $variableLeft->getName( ) . ') ' . $this->bitOperator . ' ' . $variableRight->getName() . ')', $expression ); case 'variable': $compilationContext->headersManager->add('kernel/operators'); $symbol = $compilationContext->backend->getVariableCode($variableRight); return new CompiledExpression( 'int', '((int) (' . $variableLeft->getName( ) . ') ' . $this->operator . ' (int) (zephir_get_numberval(' . $symbol . ')))', $expression ); default: throw new CompilerException( "Cannot operate variable('double') with variable('" . $variableRight->getType( ) . "')", $expression ); } default: throw new CompilerException( "Cannot operate variable('int') with '" . $right->getType() . "'", $expression ); } case 'string': throw new CompilerException("Cannot operate string variables'", $expression); case 'variable': switch ($right->getType()) { /* a + 1 */ case 'int': case 'uint': case 'long': case 'ulong': case 'double': $compilationContext->headersManager->add('kernel/operators'); $op = $this->operator; $op1 = $compilationContext->backend->getVariableCode($variableLeft); $op2 = $right->getCode(); if ('double' == $right->getType()) { return new CompiledExpression( 'int', '((int) (zephir_get_numberval(' . $op1 . ')) ' . $op . ' (int) (' . $op2 . '))', $expression ); } else { return new CompiledExpression( 'int', '((int) (zephir_get_numberval(' . $op1 . ')) ' . $op . ' ' . $op2 . ')', $expression ); } /* a(var) + a(x) */ case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->resolve(null, $compilationContext), $compilationContext, $expression ); $symbol = $compilationContext->backend->getVariableCode($variableLeft); switch ($variableRight->getType()) { /* a(var) + a(int) */ case 'int': case 'uint': case 'long': case 'ulong': /* a(var) + a(bool) */ case 'bool': $compilationContext->headersManager->add('kernel/operators'); return new CompiledExpression( 'int', '((int) (zephir_get_numberval(' . $symbol . ')) ' . $this->operator . ' ' . $variableRight->getName( ) . ')', $expression ); /* a(var) + a(var) */ case 'variable': $compilationContext->headersManager->add('kernel/operators'); $op1 = $symbol; $op2 = $compilationContext->backend->getVariableCode($variableRight); $expected = $this->getExpected($compilationContext, $expression); $expectedSymbol = $compilationContext->backend->getVariableCode($expected); $compilationContext->codePrinter->output( $this->zvalOperator . '(' . $expectedSymbol . ', ' . $op1 . ', ' . $op2 . ');' ); $this->checkVariableTemporal($variableLeft); $this->checkVariableTemporal($variableRight); return new CompiledExpression('variable', $expected->getName(), $expression); default: throw new CompilerException( "Cannot operate 'variable' with variable ('" . $variableRight->getType( ) . "')", $expression ); } default: throw new CompilerException( "Cannot operate 'variable' with '" . $right->getType() . "'", $expression ); } default: throw CompilerException::unknownType($variableLeft, $expression); } default: throw CompilerException::unsupportedType($left, $expression); } }
@param array $expression @param CompilationContext $compilationContext @return bool|CompiledExpression
compile
php
zephir-lang/zephir
src/Operators/Bitwise/BitwiseBaseOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Bitwise/BitwiseBaseOperator.php
MIT
public function optimizeConstantFolding( array $expression, CompilationContext $compilationContext ): ?CompiledExpression { if (!$compilationContext->config->get('constant-folding', 'optimizations')) { return null; } switch ($expression['left']['type']) { case 'int': case 'uint': case 'long': case 'ulong': case 'double': // continue to next switch break; default: return null; } switch ($expression['right']['type']) { case 'int': case 'uint': case 'long': case 'ulong': case 'double': // continue to operator switch break; default: return null; } /* * Return value will be always int */ return match ($this->operator) { '&' => new CompiledExpression( 'int', $expression['left']['value'] & $expression['right']['value'], $expression ), '|' => new CompiledExpression( 'int', $expression['left']['value'] | $expression['right']['value'], $expression ), '^' => new CompiledExpression( 'int', $expression['left']['value'] ^ $expression['right']['value'], $expression ), '<<' => new CompiledExpression( 'int', $expression['left']['value'] << $expression['right']['value'], $expression ), '>>' => new CompiledExpression( 'int', $expression['left']['value'] >> $expression['right']['value'], $expression ), default => null, }; }
This tries to perform arithmetical operations Probably gcc/clang will optimize them without this optimization. @see https://en.wikipedia.org/wiki/Constant_folding @param array $expression @param CompilationContext $compilationContext @return CompiledExpression|null
optimizeConstantFolding
php
zephir-lang/zephir
src/Operators/Bitwise/BitwiseBaseOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Bitwise/BitwiseBaseOperator.php
MIT
public function compile($expression, CompilationContext $compilationContext) { $this->checkLeft($expression, CompilerException::class, $expression); $leftExpr = new Expression($expression['left']); $leftExpr->setReadOnly($this->readOnly); $left = $leftExpr->compile($compilationContext); switch ($left->getType()) { case 'bool': case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression('int', '~(' . $left->getCode() . ')', $expression); case 'variable': $variable = $compilationContext->symbolTable->getVariableForRead( $left->getCode(), $compilationContext, $expression['left'] ); switch ($variable->getType()) { case 'bool': case 'int': case 'uint': case 'long': 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 ); default: throw new CompilerException('Unknown type: ' . $variable->getType(), $expression); } default: throw new CompilerException('Unknown type: ' . $left->getType(), $expression); } }
@param $expression @param CompilationContext $compilationContext @return CompiledExpression @throws ReflectionException @throws Exception
compile
php
zephir-lang/zephir
src/Operators/Bitwise/BitwiseNotOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Bitwise/BitwiseNotOperator.php
MIT
public function compile(array $expression, CompilationContext $compilationContext) { $conditions = $this->optimizeTypeOf($expression, $compilationContext); if (null !== $conditions) { return $conditions; } $this->checkLeft($expression, CompilerException::class, $expression); $this->checkRight($expression, CompilerException::class, $expression); $leftExpr = new Expression($expression['left']); $leftExpr->setReadOnly(true); $left = $leftExpr->compile($compilationContext); $rightExpr = new Expression($expression['right']); $rightExpr->setReadOnly(true); $right = $rightExpr->compile($compilationContext); switch ($left->getType()) { case 'null': switch ($right->getType()) { case 'null': return new CompiledExpression('bool', '(0 ' . $this->operator . ' 0)', $expression); case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'bool', '(0 ' . $this->operator . ' ' . $right->getCode() . ')', $expression ); case 'char': case 'uchar': return new CompiledExpression( 'bool', '(\'\\0\' ' . $this->operator . ' \'' . $right->getCode() . '\')', $expression ); case 'double': return new CompiledExpression( 'bool', '(0 ' . $this->operator . ' (int) ' . $right->getCode() . ')', $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['left'] ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'double': $compilationContext->headersManager->add('kernel/operators'); 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' ); return new CompiledExpression('bool', $condition, $expression); default: throw new CompilerException( 'Unknown type: ' . $variableRight->getType(), $expression['right'] ); } default: throw new CompilerException('Unknown type: ' . $right->getType(), $expression); } case 'int': case 'uint': case 'long': case 'double': case 'ulong': case 'char': case 'uchar': switch ($right->getType()) { case 'null': return new CompiledExpression('bool', $left->getCode() . ' ' . $this->operator, $expression); case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'bool', $left->getCode() . ' ' . $this->operator . ' ' . $right->getCode(), $expression ); case 'char': case 'uchar': return new CompiledExpression( 'bool', $left->getCode() . ' ' . $this->operator . ' \'' . $right->getCode() . '\'', $expression ); case 'double': return new CompiledExpression( 'bool', $left->getCode() . ' ' . $this->operator . ' (int) ' . $right->getCode(), $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['left'] ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'double': $compilationContext->headersManager->add('kernel/operators'); 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); return new CompiledExpression( 'bool', $this->zvalLongNegOperator . '(' . $variableCode . ', ' . $left->getCode() . ')', $expression ); default: throw new CompilerException( 'Unknown type: ' . $variableRight->getType(), $expression['right'] ); } default: throw new CompilerException( 'Cannot compare ' . $left->getType() . ' with ' . $right->getType(), $expression ); } case 'bool': switch ($right->getType()) { case 'null': return new CompiledExpression( 'bool', $left->getBooleanCode() . ' ' . $this->operator . ' 0', $expression ); case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'bool', $left->getBooleanCode() . ' ' . $this->operator . ' ' . $right->getCode(), $expression ); case 'char': case 'uchar': return new CompiledExpression( 'bool', $left->getBooleanCode() . ' ' . $this->operator . ' \'' . $right->getCode() . '\'', $expression ); case 'double': return new CompiledExpression( 'bool', $left->getBooleanCode() . ' ' . $this->operator . ' (int) ' . $right->getCode(), $expression ); case 'bool': return new CompiledExpression( 'bool', $left->getBooleanCode() . ' ' . $this->operator . ' ' . $right->getBooleanCode(), $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['left'] ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'double': $compilationContext->headersManager->add('kernel/operators'); 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); return new CompiledExpression( 'bool', $boolOperator . '(' . $variableRight . ')', $expression ); default: throw new CompilerException( 'Unknown type: ' . $variableRight->getType(), $expression['right'] ); } default: throw new CompilerException( 'Cannot compare ' . $left->getType() . ' with ' . $right->getType(), $expression ); } case 'string': $variableLeft = $compilationContext->symbolTable->getTempLocalVariableForWrite( 'variable', $compilationContext ); $variableLeftCode = $compilationContext->backend->getVariableCode($variableLeft); $compilationContext->backend->assignString( $variableLeft, $left->getCode(), $compilationContext, true ); switch ($right->getType()) { case 'null': return new CompiledExpression( 'bool', $this->zvalNullOperator . '(' . $variableLeftCode . ')', $expression['left'] ); case 'string': $compilationContext->headersManager->add('kernel/operators'); return new CompiledExpression( 'bool', $this->zvalStringOperator . '(' . $variableLeftCode . ', "' . $right->getCode() . '")', $expression['left'] ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['left'] ); switch ($variableRight->getType()) { case 'string': case 'variable': case 'mixed': $compilationContext->headersManager->add('kernel/operators'); $variableRight = $compilationContext->backend->getVariableCode($variableRight); return new CompiledExpression( 'bool', $this->zvalOperator . '(' . $variableLeftCode . ', ' . $variableRight . ')', $expression ); default: throw new CompilerException( 'Unknown type: ' . $variableRight->getType(), $expression['right'] ); } default: throw new CompilerException('Unknown type: ' . $right->getType(), $expression['left']); } case 'variable': $variable = $compilationContext->symbolTable->getVariableForRead( $left->getCode(), $compilationContext, $expression['left'] ); $variableCode = $compilationContext->backend->getVariableCode($variable); switch ($variable->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'char': case 'uchar': switch ($right->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'double': return new CompiledExpression( 'bool', $left->getCode() . ' ' . $this->operator . ' ' . $right->getCode(), $expression ); case 'char': case 'uchar': return new CompiledExpression( 'bool', $left->getCode() . ' ' . $this->operator . ' \'' . $right->getCode() . '\'', $expression ); case 'bool': return new CompiledExpression( 'bool', $left->getCode() . ' ' . $this->operator . ' ' . $right->getBooleanCode(), $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['left'] ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'char': case 'uchar': case 'double': 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); return new CompiledExpression( 'bool', $this->zvalLongNegOperator . '(' . $variableRightCode . ', ' . $variableCode . ')', $expression ); default: throw new CompilerException( 'Unknown type: ' . $variableRight->getType(), $expression['right'] ); } default: throw new CompilerException( 'Cannot compare variable: ' . $variable->getType() . ' with: ' . $right->getType(), $expression ); } case 'double': switch ($right->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'double': return new CompiledExpression( 'bool', $left->getCode() . ' ' . $this->operator . ' ' . $right->getCode(), $expression ); case 'bool': return new CompiledExpression( 'bool', $left->getCode() . ' ' . $this->operator . ' ' . $right->getBooleanCode(), $expression ); case 'char': case 'uchar': return new CompiledExpression( 'bool', $left->getCode() . ' ' . $this->operator . ' \'' . $right->getCode() . '\'', $expression ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['left'] ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'double': 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); return new CompiledExpression( 'bool', $this->zvalDoubleNegOperator . '(' . $variableRightCode . ', ' . $variableCode . ')', $expression ); default: throw new CompilerException( 'Unknown type: ' . $variableRight->getType(), $expression['right'] ); } default: throw new CompilerException( 'Cannot compare variable: ' . $variable->getType() . ' with: ' . $right->getType(), $expression ); } case 'bool': switch ($right->getType()) { case 'int': return new CompiledExpression( 'bool', $left->getCode() . ' ' . $this->operator . ' ' . $right->getCode(), $expression['left'] ); case 'bool': return new CompiledExpression( 'bool', $left->getCode() . ' ' . $this->operator . ' ' . $right->getBooleanCode(), $expression['left'] ); case 'null': return new CompiledExpression( 'bool', $left->getCode() . ' ' . $this->operator . ' 0', $expression['left'] ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['left'] ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'bool': case 'double': 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 ); return new CompiledExpression( 'bool', $boolOperator . '(' . $variableRightCode . ')', $expression ); default: throw new CompilerException( 'Unknown type: ' . $variableRight->getType(), $expression['right'] ); } default: throw new CompilerException( 'Cannot compare variable: ' . $variable->getType() . ' with: ' . $right->getType(), $expression ); } case 'array': switch ($right->getType()) { case 'null': return new CompiledExpression( 'bool', $this->zvalNullOperator . '(' . $variableCode . ')', $expression['left'] ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['left'] ); switch ($variableRight->getType()) { case 'string': case 'variable': case 'mixed': case 'array': $compilationContext->headersManager->add('kernel/operators'); $variableRight = $compilationContext->backend->getVariableCode($variableRight); return new CompiledExpression( 'bool', $this->zvalOperator . '(' . $variableCode . ', ' . $variableRight . ')', $expression ); default: throw new CompilerException( 'Unknown type: ' . $variableRight->getType(), $expression['right'] ); } default: throw new CompilerException('Unknown type: ' . $right->getType(), $expression['left']); } case 'string': $compilationContext->headersManager->add('kernel/operators'); switch ($right->getType()) { case 'null': return new CompiledExpression( 'bool', $this->zvalNullOperator . '(' . $variableCode . ')', $expression['left'] ); case 'string': return new CompiledExpression( 'bool', $this->zvalStringOperator . '(' . $variableCode . ', "' . $right->getCode() . '")', $expression['left'] ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['left'] ); switch ($variableRight->getType()) { case 'string': case 'variable': case 'mixed': $variableRight = $compilationContext->backend->getVariableCode($variableRight); return new CompiledExpression( 'bool', $this->zvalOperator . '(' . $variableCode . ', ' . $variableRight . ')', $expression ); default: throw new CompilerException( 'Unknown type: ' . $variableRight->getType(), $expression['right'] ); } default: throw new CompilerException('Unknown type: ' . $right->getType(), $expression['left']); } case 'variable': case 'mixed': $compilationContext->headersManager->add('kernel/operators'); switch ($right->getType()) { case 'null': $condition = $compilationContext->backend->getTypeofCondition( $variable, $this->operator, 'null' ); return new CompiledExpression('bool', $condition, $expression['left']); case 'int': case 'uint': case 'long': case 'ulong': case 'double': return new CompiledExpression( 'bool', $this->zvalLongOperator . '(' . $variableCode . ', ' . $right->getCode() . ')', $expression['left'] ); case 'char': case 'uchar': return new CompiledExpression( 'bool', $this->zvalLongOperator . '(' . $variableCode . ', \'' . $right->getCode() . '\')', $expression['left'] ); case 'bool': $zvalBoolOperator = 'true' === $right->getCode( ) ? $this->zvalBoolTrueOperator : $this->zvalBoolFalseOperator; return new CompiledExpression( 'bool', $zvalBoolOperator . '(' . $variableCode . ')', $expression['left'] ); case 'string': return new CompiledExpression( 'bool', $this->zvalStringOperator . '(' . $variableCode . ', "' . $right->getCode() . '")', $expression['left'] ); case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['left'] ); switch ($variableRight->getType()) { case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression( 'bool', $this->zvalLongOperator . '(' . $variableCode . ', ' . $variableRight->getName( ) . ')', $expression ); case 'double': return new CompiledExpression( 'bool', $this->zvalDoubleOperator . '(' . $variableCode . ', ' . $variableRight->getName( ) . ')', $expression ); case 'bool': return new CompiledExpression( 'bool', $this->zvalBoolOperator . '(' . $variableCode . ', ' . $variableRight->getName( ) . ')', $expression ); case 'string': case 'variable': case 'mixed': case 'array': $variableRight = $compilationContext->backend->getVariableCode($variableRight); return new CompiledExpression( 'bool', $this->zvalOperator . '(' . $variableCode . ', ' . $variableRight . ')', $expression ); default: throw new CompilerException( 'Unknown type: ' . $variableRight->getType(), $expression['right'] ); } default: throw new CompilerException('Unknown type: ' . $right->getType(), $expression['left']); } default: throw new CompilerException('Unknown type: ' . $variable->getType(), $expression); } default: throw new CompilerException('Unknown type: ' . $left->getType(), $expression); } }
Compile the expression. @param array $expression @param CompilationContext $compilationContext @return CompiledExpression @throws ReflectionException @throws Exception
compile
php
zephir-lang/zephir
src/Operators/Comparison/ComparisonBaseOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Comparison/ComparisonBaseOperator.php
MIT
public function optimizeTypeOf(array $expr, CompilationContext $compilationContext): ?CompiledExpression { if (!isset($expr['left'])) { return null; } if (!isset($expr['right']) && !isset($expr['right']['value'])) { return null; } if ('typeof' !== $expr['left']['type']) { return null; } if ('string' !== $expr['right']['type']) { $compilationContext->logger->warning( "Possible invalid comparison for 'typeof' operator with non-string", ['invalid-typeof-comparison', $expr['right']] ); return null; } switch ($expr['type']) { case 'identical': case 'equals': $operator = '=='; break; case 'not-identical': case 'not-equals': $operator = '!='; break; default: return null; } $code = (new Expression($expr['left']['left']))->compile($compilationContext)->getCode(); $variableVariable = $compilationContext->symbolTable->getVariableForRead($code, $compilationContext, $expr); if ('string' !== $expr['right']['type']) { throw new CompilerException('Right expression of typeof operator must be "string" type', $expr['right']); } $value = strtolower($expr['right']['value']); switch ($variableVariable->getType()) { case 'double': $condition = match ($value) { 'double', 'float' => '1 ' . $operator . ' 1', default => '1 ' . $operator . ' 0', }; break; case 'int': case 'integer': case 'long': $condition = match ($value) { 'int', 'integer', 'long' => '1 ' . $operator . ' 1', default => '1 ' . $operator . ' 0', }; break; case 'bool': $condition = match ($value) { 'bool', 'boolean' => '1 ' . $operator . ' 1', default => '1 ' . $operator . ' 0', }; break; case 'array': $condition = match ($value) { 'array' => '1 ' . $operator . ' 1', default => '1 ' . $operator . ' 0', }; break; case 'string': $condition = match ($value) { 'string' => '1 ' . $operator . ' 1', default => '1 ' . $operator . ' 0', }; break; case 'variable': $condition = $compilationContext->backend->getTypeofCondition($variableVariable, $operator, $value); break; default: return null; } return new CompiledExpression('bool', $condition, $expr); }
@param array $expr @param CompilationContext $compilationContext @return CompiledExpression|null @throws Exception @throws ReflectionException
optimizeTypeOf
php
zephir-lang/zephir
src/Operators/Comparison/ComparisonBaseOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Comparison/ComparisonBaseOperator.php
MIT
public function compile($expression, CompilationContext $compilationContext): CompiledExpression { $this->checkLeft($expression); $this->checkRight($expression); $leftExpr = new Expression($expression['left']); $leftExpr->setReadOnly($this->readOnly); $left = $leftExpr->compile($compilationContext); /** * This variable is used to check if the compound and expression is evaluated as true or false. */ $flagVariable = $compilationContext->symbolTable->getTempVariableForWrite('bool', $compilationContext); $assignExprLeft = $this->getAssignmentExpression($left, $expression['left']); /** * Create an implicit 'let' operation to update the evaluated left operator. */ $statement = new LetStatement([ 'type' => 'let', 'assignments' => [ [ 'assign-type' => 'variable', 'variable' => $flagVariable->getName(), 'operator' => 'assign', 'expr' => $assignExprLeft, 'file' => $expression['left']['file'], 'line' => $expression['left']['line'], 'char' => $expression['left']['char'], ], ], ]); $statement->compile($compilationContext); $compilationContext->codePrinter->output($this->getOutput($flagVariable)); $compilationContext->codePrinter->increaseLevel(); $rightExpr = new Expression($expression['right']); $rightExpr->setReadOnly($this->readOnly); $right = $rightExpr->compile($compilationContext); $assignExprRight = $this->getAssignmentExpression($right, $expression['right']); /** * Create an implicit 'let' operation to update the evaluated right operator. */ $statement = new LetStatement([ 'type' => 'let', 'assignments' => [ [ 'assign-type' => 'variable', 'variable' => $flagVariable->getName(), 'operator' => 'assign', 'expr' => $assignExprRight, 'file' => $expression['right']['file'], 'line' => $expression['right']['line'], 'char' => $expression['right']['char'], ], ], ]); $statement->compile($compilationContext); $compilationContext->codePrinter->decreaseLevel(); $compilationContext->codePrinter->output('}'); return new CompiledExpression('bool', $flagVariable->getName(), $expression); }
@param $expression @param CompilationContext $compilationContext @return CompiledExpression @throws ReflectionException @throws Exception
compile
php
zephir-lang/zephir
src/Operators/Logical/AndOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Logical/AndOperator.php
MIT
private function getAssignmentExpression(CompiledExpression $left, array $expression): array { return match ($left->getType()) { Types::T_INT, Types::T_BOOL, Types::T_CHAR, Types::T_DOUBLE, Types::T_UINT, Types::T_UCHAR => [ 'type' => $left->getType(), 'value' => $left->getCode(), ], Types::T_VARIABLE => [ 'type' => 'variable', 'value' => $left->getCode(), ], Types::T_NULL => [ 'type' => 'null', 'value' => null, ], default => throw new CompilerException( $left->getType(), $expression ), }; }
@param CompiledExpression $left @param array $expression @return array
getAssignmentExpression
php
zephir-lang/zephir
src/Operators/Logical/AndOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Logical/AndOperator.php
MIT
public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression { try { $expr = new Expression($expression['right']); $resolved = $expr->compile($compilationContext); } catch (Exception $e) { throw new CompilerException($e->getMessage(), $expression, $e->getCode(), $e); } switch ($expression['left']) { case Types::T_INT: switch ($resolved->getType()) { case Types::T_NULL: return new CompiledExpression('int', '0', $expression); case Types::T_CHAR: case Types::T_UCHAR: return new CompiledExpression('int', "'{$resolved->getCode()}'", $expression); case Types::T_INT: return new CompiledExpression('int', $resolved->getCode(), $expression); case Types::T_DOUBLE: return new CompiledExpression('int', '(int) ' . $resolved->getCode(), $expression); case Types::T_BOOL: return new CompiledExpression('int', $resolved->getBooleanCode(), $expression); case Types::T_STRING: $compilationContext->headersManager->add('kernel/operators'); /** * zephir_get_intval_ex use zval variable * before use with it we create a new variable and assign value of literal. * * TODO: Optimize by creating native function for string without zval using */ $symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite( 'string', $compilationContext ); $original = $resolved->getOriginal(); $original['operator'] = 'assign'; $let = new LetVariable(); $let->assign( $symbolVariable->getName(), $symbolVariable, $resolved, new ReadDetector(), $compilationContext, $original ); $symbol = $compilationContext->backend->getVariableCode($symbolVariable); return new CompiledExpression( 'int', 'zephir_get_intval_ex(' . $symbol . ')', $expression ); case Types::T_ARRAY: $compilationContext->headersManager->add('kernel/operators'); $symbolVariable = $compilationContext->symbolTable->getVariableForRead( $resolved->getCode(), $compilationContext, $expression ); $symbol = $compilationContext->backend->getVariableCode($symbolVariable); return new CompiledExpression('int', 'zephir_get_intval(' . $symbol . ')', $expression); case Types::T_VARIABLE: $compilationContext->headersManager->add('kernel/operators'); $symbolVariable = $compilationContext->symbolTable->getVariableForRead( $resolved->getCode(), $compilationContext, $expression ); switch ($symbolVariable->getType()) { case Types::T_INT: case Types::T_CHAR: case Types::T_UCHAR: return new CompiledExpression('int', $symbolVariable->getName(), $expression); case Types::T_DOUBLE: case Types::T_BOOL: return new CompiledExpression( 'int', '(int) (' . $symbolVariable->getName() . ')', $expression ); case Types::T_ARRAY: case Types::T_VARIABLE: case Types::T_MIXED: case Types::T_STRING: $symbol = $compilationContext->backend->getVariableCode($symbolVariable); return new CompiledExpression( 'int', 'zephir_get_intval(' . $symbol . ')', $expression ); default: throw new CompilerException( sprintf( 'Cannot cast: %s(%s) to %s', $resolved->getType(), $symbolVariable->getType(), $expression['left'] ), $expression ); } default: throw new CompilerException( sprintf('Cannot cast: %s to %s', $resolved->getType(), $expression['left']), $expression ); } case Types::T_LONG: switch ($resolved->getType()) { case Types::T_INT: return new CompiledExpression('long', $resolved->getCode(), $expression); case Types::T_CHAR: case Types::T_UCHAR: return new CompiledExpression( 'long', "(long) '{$resolved->getCode()}'", $expression ); case Types::T_DOUBLE: return new CompiledExpression('long', '(long) ' . $resolved->getCode(), $expression); case Types::T_BOOL: return new CompiledExpression('long', $resolved->getBooleanCode(), $expression); case Types::T_ARRAY: $compilationContext->headersManager->add('kernel/operators'); $symbolVariable = $compilationContext->symbolTable->getVariableForRead( $resolved->getCode(), $compilationContext, $expression ); return new CompiledExpression( 'long', 'zephir_get_intval(' . $symbolVariable->getName() . ')', $expression ); case Types::T_VARIABLE: $compilationContext->headersManager->add('kernel/operators'); $symbolVariable = $compilationContext->symbolTable->getVariableForRead( $resolved->getCode(), $compilationContext, $expression ); switch ($symbolVariable->getType()) { case Types::T_INT: case Types::T_CHAR: case Types::T_UCHAR: return new CompiledExpression('long', $symbolVariable->getName(), $expression); case Types::T_DOUBLE: return new CompiledExpression( 'long', '(long) (' . $symbolVariable->getName() . ')', $expression ); case Types::T_VARIABLE: case Types::T_MIXED: $symbol = $compilationContext->backend->getVariableCode($symbolVariable); return new CompiledExpression( 'long', 'zephir_get_intval(' . $symbol . ')', $expression ); default: throw new CompilerException( sprintf( 'Cannot cast: %s(%s) to %s', $resolved->getType(), $symbolVariable->getType(), $expression['left'] ), $expression ); } default: throw new CompilerException( sprintf('Cannot cast: %s to %s', $resolved->getType(), $expression['left']), $expression ); } case Types::T_DOUBLE: switch ($resolved->getType()) { case Types::T_NULL: return new CompiledExpression('double', '0', $expression); case Types::T_BOOL: return new CompiledExpression('double', $resolved->getBooleanCode(), $expression); case Types::T_CHAR: case Types::T_UCHAR: return new CompiledExpression( 'double', "(double) '{$resolved->getCode()}'", $expression ); case Types::T_INT: case Types::T_LONG: case Types::T_DOUBLE: return new CompiledExpression('double', $resolved->getCode(), $expression); case Types::T_ARRAY: $compilationContext->headersManager->add('kernel/operators'); $symbolVariable = $compilationContext->symbolTable->getVariableForRead( $resolved->getCode(), $compilationContext, $expression ); $symbol = $compilationContext->backend->getVariableCode($symbolVariable); return new CompiledExpression( 'double', 'zephir_get_doubleval(' . $symbol . ')', $expression ); case Types::T_VARIABLE: $compilationContext->headersManager->add('kernel/operators'); $symbolVariable = $compilationContext->symbolTable->getVariableForRead( $resolved->getCode(), $compilationContext, $expression ); switch ($symbolVariable->getType()) { case Types::T_INT: case Types::T_CHAR: case Types::T_UCHAR: return new CompiledExpression( 'double', "(double) {$symbolVariable->getName()}", $expression ); case Types::T_DOUBLE: case Types::T_BOOL: return new CompiledExpression( 'double', sprintf('(double) (%s)', $symbolVariable->getName()), $expression ); case Types::T_ARRAY: case Types::T_VARIABLE: case Types::T_MIXED: $symbol = $compilationContext->backend->getVariableCode($symbolVariable); return new CompiledExpression( 'double', sprintf('zephir_get_doubleval(%s)', $symbol), $expression ); default: throw new CompilerException( sprintf( 'Cannot cast: %s(%s) to %s', $resolved->getType(), $symbolVariable->getType(), $expression['left'] ), $expression ); } default: throw new CompilerException( sprintf('Cannot cast: %s to %s', $resolved->getType(), $expression['left']), $expression ); } case Types::T_BOOL: switch ($resolved->getType()) { case Types::T_INT: return new CompiledExpression( 'bool', '(zend_bool) ' . $resolved->getCode(), $expression ); case Types::T_CHAR: case Types::T_UCHAR: return new CompiledExpression( 'bool', "(zend_bool) '{$resolved->getCode()}'", $expression ); case Types::T_BOOL: return new CompiledExpression('bool', $resolved->getCode(), $expression); case Types::T_VARIABLE: $compilationContext->headersManager->add('kernel/operators'); $symbolVariable = $compilationContext->symbolTable->getVariableForRead( $resolved->getCode(), $compilationContext, $expression ); $symbol = $compilationContext->backend->getVariableCode($symbolVariable); $this->checkVariableTemporal($symbolVariable); return match ($symbolVariable->getType()) { Types::T_INT, Types::T_CHAR, Types::T_UCHAR => new CompiledExpression( 'bool', sprintf('(zend_bool) %s', $symbolVariable->getName()), $expression ), Types::T_VARIABLE, Types::T_MIXED => new CompiledExpression( 'bool', sprintf('zephir_get_boolval(%s)', $symbol), $expression ), default => throw new CompilerException( sprintf( 'Cannot cast: %s(%s) to %s', $resolved->getType(), $symbolVariable->getType(), $expression['left'] ), $expression ), }; default: throw new CompilerException( sprintf('Cannot cast: %s to %s', $resolved->getType(), $expression['left']), $expression ); } case Types::T_CHAR: switch ($resolved->getType()) { case Types::T_UCHAR: return new CompiledExpression('uchar', "'{$resolved->getCode()}'", $expression); case Types::T_CHAR: return new CompiledExpression('char', "'{$resolved->getCode()}'", $expression); case Types::T_VARIABLE: case Types::T_MIXED: $compilationContext->headersManager->add('kernel/operators'); $symbolVariable = $compilationContext->symbolTable->getVariableForRead( $resolved->getCode(), $compilationContext, $expression ); $tempVariable = $compilationContext->symbolTable->getTempVariableForWrite( 'char', $compilationContext ); switch ($symbolVariable->getType()) { // TODO: uchar case Types::T_CHAR: $compilationContext->codePrinter->output( sprintf('%s = %s;', $tempVariable->getName(), $symbolVariable->getName()) ); break; default: $variableCode = $compilationContext->backend->getVariableCode($symbolVariable); $compilationContext->codePrinter->output( sprintf( '%s = (char) zephir_get_intval(%s);', $tempVariable->getName(), $variableCode ) ); } return new CompiledExpression('variable', $tempVariable->getName(), $expression); default: throw new CompilerException( sprintf('Cannot cast: %s to %s', $resolved->getType(), $expression['left']), $expression ); } case Types::T_STRING: switch ($resolved->getType()) { case Types::T_UCHAR: case Types::T_CHAR: return new CompiledExpression('string', $resolved->getCode(), $expression); case Types::T_VARIABLE: case Types::T_MIXED: $compilationContext->headersManager->add('kernel/operators'); $compilationContext->symbolTable->mustGrownStack(true); $resolvedVariable = $compilationContext->symbolTable->getVariableForRead( $resolved->getCode(), $compilationContext, $expression ); switch ($resolvedVariable->getType()) { case Types::T_CHAR: $symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite( 'string', $compilationContext ); $symbol = $compilationContext->backend->getVariableCode($symbolVariable); $resolvedCode = $compilationContext->backend->getVariableCode($resolvedVariable); $compilationContext->codePrinter->output( sprintf('ZVAL_STRINGL(%s, %s, 1);', $symbol, $resolvedCode) ); return new CompiledExpression( 'variable', $symbolVariable->getName(), $expression ); default: // TODO: I'm not a pretty sure this branch works // This is old code I just moved to "default" // See: https://github.com/zephir-lang/zephir/issues/1988 $symbolVariable = $compilationContext->symbolTable->getTempVariable( 'string', $compilationContext ); $symbolVariable->setMustInitNull(true); $symbolVariable->setIsInitialized(true, $compilationContext); $symbolVariable->increaseUses(); $symbol = $compilationContext->backend->getVariableCode($symbolVariable); $resolvedCode = $compilationContext->backend->getVariableCode($resolvedVariable); $compilationContext->codePrinter->output( sprintf('zephir_cast_to_string(%s, %s);', $symbol, $resolvedCode) ); $this->checkVariableTemporal($symbolVariable); return new CompiledExpression( 'variable', $symbolVariable->getName(), $expression ); } default: throw new CompilerException( sprintf('Cannot cast: %s to %s', $resolved->getType(), $expression['left']), $expression ); } case Types::T_ARRAY: switch ($resolved->getType()) { case Types::T_VARIABLE: case Types::T_MIXED: $compilationContext->headersManager->add('kernel/operators'); $compilationContext->symbolTable->mustGrownStack(true); $symbolVariable = $compilationContext->symbolTable->getTempVariable( 'array', $compilationContext ); $symbolVariable->setMustInitNull(true); $symbolVariable->setIsInitialized(true, $compilationContext); $symbolVariable->increaseUses(); $symbol = $compilationContext->backend->getVariableCode($symbolVariable); $resolvedVariable = $compilationContext->symbolTable->getVariableForRead( $resolved->getCode(), $compilationContext ); $resolvedCode = $compilationContext->backend->getVariableCode($resolvedVariable); $compilationContext->codePrinter->output( sprintf('zephir_get_arrval(%s, %s);', $symbol, $resolvedCode) ); $this->checkVariableTemporal($symbolVariable); return new CompiledExpression('variable', $symbolVariable->getName(), $expression); default: throw new CompilerException( sprintf('Cannot cast: %s to %s', $resolved->getType(), $expression['left']), $expression ); } case Types::T_OBJECT: switch ($resolved->getType()) { case Types::T_INT: case Types::T_DOUBLE: case Types::T_BOOL: case Types::T_NULL: case Types::T_STRING: case Types::T_ARRAY: $compilationContext->headersManager->add('kernel/operators'); $compilationContext->symbolTable->mustGrownStack(true); $symbolVariable = $compilationContext->symbolTable->getTempVariable( 'variable', $compilationContext ); $symbol = $compilationContext->backend->getVariableCode($symbolVariable); /** * zephir_convert_to_object use zval variable * before use it we create a new variable and assign value of literal. */ $let = new LetVariable(); $original = $resolved->getOriginal(); $original['operator'] = 'assign'; $let->assign( $symbolVariable->getName(), $symbolVariable, $resolved, new ReadDetector(), $compilationContext, $original ); $compilationContext->codePrinter->output('zephir_convert_to_object(' . $symbol . ');'); return new CompiledExpression('variable', $symbolVariable->getName(), $expression); case Types::T_VARIABLE: case Types::T_MIXED: $compilationContext->headersManager->add('kernel/operators'); $symbolVariable = $compilationContext->symbolTable->getVariableForRead( $resolved->getCode(), $compilationContext, $expression ); $symbol = $compilationContext->backend->getVariableCode($symbolVariable); $this->checkVariableTemporal($symbolVariable); $compilationContext->codePrinter->output('zephir_convert_to_object(' . $symbol . ');'); return new CompiledExpression('variable', $symbolVariable->getName(), $expression); default: throw new CompilerException( sprintf('Cannot cast: %s to %s', $resolved->getType(), $expression['left']), $expression ); } default: throw new CompilerException( sprintf('Cannot cast: %s to %s', $resolved->getType(), $expression['left']), $expression ); } }
Compiles a type cast operation. @param array $expression @param CompilationContext $compilationContext @return CompiledExpression @throws ReflectionException
compile
php
zephir-lang/zephir
src/Operators/Other/CastOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Other/CastOperator.php
MIT
public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression { $compilationContext->headersManager->add('kernel/object'); $exprVariable = new Expression($expression['left']); $exprVariable->setReadOnly(true); $exprVariable->setExpectReturn(true); $exprCompiledVariable = $exprVariable->compile($compilationContext); if ('variable' !== $exprCompiledVariable->getType()) { throw new CompilerException( 'Expression type: ' . $exprCompiledVariable->getType() . ' cannot be used as array', $expression ); } $clonedVariable = $compilationContext->symbolTable->getVariableForRead( $exprCompiledVariable->getCode(), $compilationContext, $expression ); if ('variable' !== $clonedVariable->getType()) { throw new CompilerException('Variable type: ' . $exprVariable->getType() . ' cannot be cloned'); } if ($clonedVariable->hasDifferentDynamicType(['undefined', 'object', 'null'])) { $compilationContext->logger->warning( 'Possible attempt to use non array in "clone" operator', ['non-valid-clone', $expression] ); } $symbolVariable = $this->getExpected($compilationContext, $expression); if (!$symbolVariable->isVariable()) { throw new CompilerException('Objects can only be cloned into dynamic variables', $expression); } $symbolVariable->setDynamicTypes('object'); $symbolVariable->setIsInitialized(true, $compilationContext); /* Inherit the dynamic type data from the cloned object */ $symbolVariable->setDynamicTypes($clonedVariable->getDynamicTypes()); $symbolVariable->setClassTypes($clonedVariable->getClassTypes()); $symbol = $compilationContext->backend->getVariableCode($symbolVariable); $clonedSymbol = $compilationContext->backend->getVariableCode($clonedVariable); $compilationContext->codePrinter->output( 'if (zephir_clone(' . $symbol . ', ' . $clonedSymbol . ') == FAILURE) {' ); $compilationContext->codePrinter->output("\t" . 'RETURN_MM();'); $compilationContext->codePrinter->output('}'); return new CompiledExpression('variable', $symbolVariable->getName(), $expression); }
@param array $expression @param CompilationContext $compilationContext @return CompiledExpression @throws Exception
compile
php
zephir-lang/zephir
src/Operators/Other/CloneOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Other/CloneOperator.php
MIT
public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression { $this->checkLeft($expression, CompilerException::class, $expression); $this->checkRight($expression, CompilerException::class, $expression); $compilationContext->headersManager->add('kernel/concat'); /** * Try to optimize the concatenation. */ $optimized = $this->_getOptimizedConcat($expression, $compilationContext, $isFullString); if (is_array($optimized)) { if (!$isFullString) { $expected = $this->getExpectedComplexLiteral($compilationContext); } else { $expected = $this->getExpectedComplexLiteral($compilationContext, 'string'); } $expected->setDynamicTypes('string'); $expectedCode = $compilationContext->backend->getVariableCode($expected); $compilationContext->codePrinter->output( sprintf('ZEPHIR_CONCAT_%s(%s, %s);', strtoupper($optimized[0]), $expectedCode, $optimized[1]) ); return new CompiledExpression('variable', $expected->getName(), $expression); } /** * If the expression cannot be optimized, fall back to the standard compilation. */ $leftExpr = new Expression($expression['left']); $left = $this->compileExpression($leftExpr, $compilationContext, $expression['left']['type']); if ('variable' == $left->getType()) { $variableLeft = $compilationContext->symbolTable->getVariableForRead( $left->getCode(), $compilationContext, $expression['right'] ); $variableLeft = $compilationContext->backend->getVariableCode($variableLeft); } $rightExpr = new Expression($expression['right']); $right = $this->compileExpression($rightExpr, $compilationContext, $expression['left']['type']); if ('variable' == $right->getType()) { $variableRight = $compilationContext->symbolTable->getVariableForRead( $right->getCode(), $compilationContext, $expression['right'] ); $variableRight = $compilationContext->backend->getVariableCode($variableRight); } $expected = $this->getExpectedComplexLiteral($compilationContext); $expectedCode = $compilationContext->backend->getVariableCode($expected); if ('string' == $left->getType() && 'variable' == $right->getType()) { $compilationContext->codePrinter->output( 'ZEPHIR_CONCAT_SV(' . $expectedCode . ', "' . $left->getCode() . '", ' . $variableRight . ');' ); } if ('variable' == $left->getType() && 'string' == $right->getType()) { $compilationContext->codePrinter->output( 'ZEPHIR_CONCAT_VS(' . $expectedCode . ', ' . $variableLeft . ', "' . $right->getCode() . '");' ); } if ('variable' == $left->getType() && 'variable' == $right->getType()) { $compilationContext->codePrinter->output( 'zephir_concat_function(' . $expectedCode . ', ' . $variableLeft . ', ' . $variableRight . ');' ); } $expected->setDynamicTypes('string'); return new CompiledExpression('variable', $expected->getName(), $expression); }
Performs concat compilation. @param array $expression @param CompilationContext $compilationContext @return CompiledExpression @throws CompilerException
compile
php
zephir-lang/zephir
src/Operators/Other/ConcatOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Other/ConcatOperator.php
MIT
private function _getOptimizedConcat( array $expression, CompilationContext $compilationContext, &$isFullString ): array { $originalExpr = $expression; $isFullString = true; $parts = []; while ($expression && isset($expression['left'])) { $parts[] = $expression['right']; if ('concat' == $expression['left']['type']) { $expression = $expression['left']; } else { $parts[] = $expression['left']; $expression = null; } } if ($expression) { $parts[] = $expression['right']; $parts[] = $expression['left']; } $key = ''; $concatParts = []; $parts = array_reverse($parts); foreach ($parts as $part) { $expr = new Expression($part); $compiledExpr = $this->compileExpression($expr, $compilationContext, $part['type']); switch ($compiledExpr->getType()) { case 'variable': $variable = $compilationContext->symbolTable->getVariableForRead( $compiledExpr->getCode(), $compilationContext, $originalExpr ); switch ($variable->getType()) { case 'variable': $key .= 'v'; $concatParts[] = $compilationContext->backend->getVariableCode($variable); $isFullString = false; break; case 'string': $key .= 'v'; $concatParts[] = $compilationContext->backend->getVariableCode($variable); break; case 'int': case 'uint': case 'long': case 'ulong': $key .= 'v'; $tempVariable = $compilationContext->symbolTable->getTempLocalVariableForWrite( 'variable', $compilationContext ); $compilationContext->backend->assignLong( $tempVariable, $compiledExpr->getCode(), $compilationContext ); $concatParts[] = $compilationContext->backend->getVariableCode($tempVariable); break; case 'double': $key .= 'v'; $tempVariable = $compilationContext->symbolTable->getTempLocalVariableForWrite( 'variable', $compilationContext ); $compilationContext->backend->assignDouble( $tempVariable, $compiledExpr->getCode(), $compilationContext ); $concatParts[] = $compilationContext->backend->getVariableCode($tempVariable); break; default: throw new CompilerException( sprintf( 'Variable type: %s cannot be used in concat operation', $variable->getType() ), $compiledExpr->getOriginal() ); } break; case 'string': $key .= 's'; $concatParts[] = '"' . Name::addSlashes($compiledExpr->getCode()) . '"'; break; case 'int': case 'uint': case 'long': case 'ulong': $key .= 'v'; $tempVariable = $compilationContext->symbolTable->getTempLocalVariableForWrite( 'variable', $compilationContext ); $compilationContext->codePrinter->output( sprintf( 'ZVAL_LONG(&%s, %s);', $tempVariable->getName(), $compiledExpr->getCode() ) ); $concatParts[] = '&' . $tempVariable->getName(); break; case 'double': $key .= 'v'; $tempVariable = $compilationContext->symbolTable->getTempLocalVariableForWrite( 'variable', $compilationContext ); $compilationContext->codePrinter->output( sprintf( 'ZVAL_DOUBLE(&%s, %s);', $tempVariable->getName(), $compiledExpr->getCode() ) ); $concatParts[] = '&' . $tempVariable->getName(); break; default: throw new CompilerException( sprintf( 'Variable type: %s cannot be used in concat operation', $compiledExpr->getType() ), $compiledExpr->getOriginal() ); } } $compilationContext->stringsManager->addConcatKey($key); return [$key, implode(', ', $concatParts)]; }
@param array $expression @param CompilationContext $compilationContext @param bool $isFullString @return array @throws CompilerException
_getOptimizedConcat
php
zephir-lang/zephir
src/Operators/Other/ConcatOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Other/ConcatOperator.php
MIT
public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression { $compilationContext->headersManager->add('kernel/operators'); if (!isset($expression['left'])) { throw new CompilerException("Invalid 'left' operand for 'empty' expression", $expression['left']); } $leftExpr = new Expression($expression['left']); $leftExpr->setReadOnly(true); $left = $leftExpr->compile($compilationContext); if ('variable' != $left->getType() && 'array' != $left->getType()) { throw new CompilerException("'empty' operand only can be a variable", $expression['left']); } $variableLeft = $compilationContext->symbolTable->getVariableForRead( $left->getCode(), $compilationContext, $expression['left'] ); if (!$variableLeft->isVariable() && !$variableLeft->isString() && !$variableLeft->isArray()) { throw new CompilerException( "Only dynamic/string variables can be used in 'empty' operators", $expression['left'] ); } return new CompiledExpression( 'bool', 'ZEPHIR_IS_EMPTY(' . $compilationContext->backend->getVariableCode($variableLeft) . ')', $expression ); }
@param array $expression @param CompilationContext $compilationContext @return CompiledExpression @throws Exception
compile
php
zephir-lang/zephir
src/Operators/Other/EmptyOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Other/EmptyOperator.php
MIT
public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression { $compilationContext->headersManager->add('kernel/array'); $variable = $compilationContext->symbolTable->getVariableForWrite( $expression['left']['value'], $compilationContext, $expression['left'] ); if ('variable' != $variable->getType()) { throw CompilerException::cannotUseVariableTypeAs( $variable, 'in "fetch" operator', $expression ); } /** * return_value must not be observed */ if ('return_value' !== $variable->getName()) { /** * TODO: use a read detector here */ $readOnly = false; $line = max( $compilationContext->symbolTable->getLastCallLine(), $compilationContext->symbolTable->getLastUnsetLine() ); if (false === $line || ($line > 0 && $line < $expression['line'])) { $numberMutations = $compilationContext->symbolTable->getExpectedMutations($variable->getName()); if (1 == $numberMutations) { if (1 == $variable->getNumberMutations()) { $variable->setIsInitialized(true, $compilationContext); $variable->setMemoryTracked(false); $variable->setDynamicTypes('undefined'); $readOnly = true; } } } if (!$readOnly || 'array-access' != $expression['right']['type']) { $variable->setIsInitialized(true, $compilationContext); $variable->observeVariant($compilationContext); $variable->setDynamicTypes('undefined'); $variable->setPossibleValue(new CompiledExpression('undefined', '', $expression), $compilationContext); } } else { $variable = $compilationContext->symbolTable->getTempVariableForObserve('variable', $compilationContext); } $flags = $readOnly ? '1' : '0'; switch ($expression['right']['type']) { case 'array-access': $exprVariable = new Expression($expression['right']['left']); $exprVariable->setReadOnly(true); $exprVariable->setNoisy(false); $exprCompiledVariable = $exprVariable->compile($compilationContext); if ('variable' != $exprCompiledVariable->getType()) { throw new CompilerException( 'Expression type: ' . $exprCompiledVariable->getType() . ' cannot be used as array', $expression['right']['left'] ); } $evalVariable = $compilationContext->symbolTable->getVariableForRead( $exprCompiledVariable->getCode(), $compilationContext, $expression['right']['left'] ); if ('variable' != $evalVariable->getType() && 'array' != $evalVariable->getType()) { throw new CompilerException( 'Variable type: ' . $variable->getType() . ' cannot be used as array', $expression['right']['left'] ); } if ('variable' == $evalVariable->getType()) { if ($evalVariable->hasDifferentDynamicType(['undefined', 'array', 'null'])) { $compilationContext->logger->warning( 'Possible attempt to use non array in fetch operator', ['non-valid-fetch', $expression['right']] ); } } $expr = new Expression($expression['right']['right']); $expr->setReadOnly(true); $expr->setNoisy(false); $resolvedExpr = $expr->compile($compilationContext); return $compilationContext->backend->arrayIssetFetch( $variable, $evalVariable, $resolvedExpr, $flags, $expression, $compilationContext ); case 'property-access': $exprVariable = new Expression($expression['right']['left']); $exprVariable->setReadOnly(true); $exprVariable->setNoisy(false); $exprCompiledVariable = $exprVariable->compile($compilationContext); if ('variable' != $exprCompiledVariable->getType()) { throw new CompilerException( 'Expression type: ' . $exprCompiledVariable->getType() . ' cannot be used as object', $expression['right']['left'] ); } $evalVariable = $compilationContext->symbolTable->getVariableForRead( $exprCompiledVariable->getCode(), $compilationContext, $expression['right']['left'] ); if ('variable' != $evalVariable->getType()) { throw new CompilerException( 'Variable type: ' . $variable->getType() . ' cannot be used as object', $expression['right']['left'] ); } if ($evalVariable->hasDifferentDynamicType(['undefined', 'object', 'null'])) { $compilationContext->logger->warning( 'Possible attempt to use non object in fetch operator', ['non-valid-fetch', $expression['right']] ); } $property = $expression['right']['right']['value']; $compilationContext->headersManager->add('kernel/object'); $symbol = $compilationContext->backend->getVariableCode($variable); $evalSymbol = $compilationContext->backend->getVariableCode($evalVariable); return new CompiledExpression( 'bool', 'zephir_fetch_property(' . $symbol . ', ' . $evalSymbol . ', SL("' . $property . '"), PH_SILENT_CC)', $expression ); case 'property-dynamic-access': $exprVariable = new Expression($expression['right']['left']); $exprVariable->setReadOnly(true); $exprVariable->setNoisy(false); $exprCompiledVariable = $exprVariable->compile($compilationContext); if ('variable' != $exprCompiledVariable->getType()) { throw new CompilerException( 'Expression type: ' . $exprCompiledVariable->getType() . ' cannot be used as object', $expression['right']['left'] ); } $evalVariable = $compilationContext->symbolTable->getVariableForRead( $exprCompiledVariable->getCode(), $compilationContext, $expression['right']['left'] ); if ('variable' != $evalVariable->getType()) { throw new CompilerException( 'Variable type: ' . $evalVariable->getType() . ' cannot be used as object', $expression['right']['left'] ); } if ($evalVariable->hasDifferentDynamicType(['undefined', 'object', 'null'])) { $compilationContext->logger->warning( 'Possible attempt to use non object in fetch operator', ['non-valid-fetch', $expression['right']] ); } $exprVariableProperty = new Expression($expression['right']['right']); $exprVariableProperty->setReadOnly(true); $exprCompiledVariableProperty = $exprVariableProperty->compile($compilationContext); if ('variable' != $exprCompiledVariableProperty->getType()) { throw new CompilerException( 'Expression type: ' . $exprCompiledVariableProperty->getType( ) . ' cannot be used in property-dynamic-access', $expression['right']['right'] ); } $evalVariableProperty = $compilationContext->symbolTable->getVariableForRead( $exprCompiledVariableProperty->getCode(), $compilationContext, $expression['right']['right'] ); if ('variable' != $evalVariableProperty->getType() && 'string' != $evalVariableProperty->getType()) { throw new CompilerException( 'Variable type: ' . $evalVariableProperty->getType( ) . ' cannot be used in property-dynamic-access', $expression['right']['right'] ); } $compilationContext->headersManager->add('kernel/object'); $symbol = $compilationContext->backend->getVariableCode($variable); $evalSymbol = $compilationContext->backend->getVariableCode($evalVariable); $evalPropertySymbol = $compilationContext->backend->getVariableCode($evalVariableProperty); return new CompiledExpression( 'bool', 'zephir_fetch_property_zval(' . $symbol . ', ' . $evalSymbol . ', ' . $evalPropertySymbol . ', PH_SILENT_CC)', $expression ); default: throw new CompilerException( 'Cannot use this expression for "fetch" operators: ' . $expression['right']['type'], $expression ); } }
@param array $expression @param CompilationContext $compilationContext @return CompiledExpression @throws Exception
compile
php
zephir-lang/zephir
src/Operators/Other/FetchOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Other/FetchOperator.php
MIT
public function compile($expression, CompilationContext $context): CompiledExpression { $left = new Expression($expression['left']); $resolved = $left->compile($context); if ('variable' != $resolved->getType()) { throw new CompilerException("InstanceOf requires a 'dynamic variable' in the left operand", $expression); } $symbolVariable = $context->symbolTable->getVariableForRead($resolved->getCode(), $context, $expression); if (!$symbolVariable->isVariable()) { throw new CompilerException("InstanceOf requires a 'dynamic variable' in the left operand", $expression); } $right = new Expression($expression['right']); $resolved = $right->compile($context); $resolvedVariable = $resolved->getCode(); switch ($resolved->getType()) { case 'string': $classEntry = (new Entry($resolvedVariable, $context)); break; default: switch ($resolved->getType()) { case 'variable': if ('this' === $resolvedVariable) { /** * TODO: It's an optimization variant, but maybe we need to get entry in runtime? */ $classEntry = $context->classDefinition->getClassEntry($context); } elseif (!$context->symbolTable->hasVariable($resolvedVariable)) { $className = $context->getFullName($resolvedVariable); if ('Traversable' === $className) { $symbol = $context->backend->getVariableCode($symbolVariable); return new CompiledExpression( 'bool', 'zephir_zval_is_traversable(' . $symbol . ')', $expression ); } if ($context->compiler->isClass($className)) { $classDefinition = $context->compiler->getClassDefinition($className); $classEntry = $classDefinition->getClassEntry($context); } else { if ($context->compiler->isInterface($className)) { $classDefinition = $context->compiler->getClassDefinition($className); $classEntry = $classDefinition->getClassEntry($context); } else { if (!class_exists($className, false)) { $code = 'SL("' . trim(Entry::escape($className), '\\') . '")'; } else { $entry = (new Entry($resolvedVariable, $context)); $classEntry = $entry->get(); if (!$entry->isInternal()) { $code = 'SL("' . trim(Entry::escape($className), '\\') . '")'; } } } } } else { $code = $this->prepareBackendSpecificCode($resolvedVariable, $context); } break; case 'property-access': case 'array-access': $context->headersManager->add('kernel/operators'); $tempVariable = $context->symbolTable->getTempVariableForWrite('string', $context); $tempVariable->setMustInitNull(true); $tempVariableName = $tempVariable->getName(); $context->codePrinter->output( 'zephir_get_strval(' . $tempVariableName . ', ' . $resolvedVariable . ');' ); $code = $this->prepareBackendSpecificCode($tempVariableName, $context); break; default: throw new CompilerException( "InstanceOf requires a 'variable' or a 'string' in the right operand", $expression ); } } /* TODO:, Possible optimization is use zephir_is_instance when the const class name is an internal class or interface */ $context->headersManager->add('kernel/object'); $symbol = $context->backend->getVariableCode($symbolVariable); if (isset($code)) { return new CompiledExpression('bool', 'zephir_is_instance_of(' . $symbol . ', ' . $code . ')', $expression); } return new CompiledExpression( 'bool', 'zephir_instance_of_ev(' . $symbol . ', ' . $classEntry . ')', $expression ); }
@param $expression @param CompilationContext $context @return CompiledExpression @throws Exception @throws ReflectionException
compile
php
zephir-lang/zephir
src/Operators/Other/InstanceOfOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Other/InstanceOfOperator.php
MIT
public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression { $left = 'list' === $expression['left']['type'] ? $expression['left']['left'] : $expression['left']; switch ($left['type']) { case 'array-access': $compilationContext->headersManager->add('kernel/array'); $exprVariable = new Expression($left['left']); $exprVariable->setReadOnly(true); $exprVariable->setNoisy(false); $exprCompiledVariable = $exprVariable->compile($compilationContext); if (!in_array($exprCompiledVariable->getType(), ['variable', 'array'], true)) { throw new CompilerException( 'Expression type: ' . $exprCompiledVariable->getType() . ' cannot be used as array', $left['left'] ); } $variable = $compilationContext->symbolTable->getVariableForRead( $exprCompiledVariable->getCode(), $compilationContext, $left['left'] ); switch ($variable->getType()) { case 'array': case 'variable': break; default: throw new CompilerException( 'Variable type: ' . $variable->getType() . ' cannot be used as array', $left['left'] ); } if ('variable' === $variable->getType()) { if ($variable->hasDifferentDynamicType(['undefined', 'array', 'null'])) { $compilationContext->logger->warning( 'Possible attempt to use non array in isset operator', ['non-valid-isset', $expression] ); } } $expr = new Expression($left['right']); $expr->setReadOnly(true); $expr->setNoisy(false); $resolvedExpr = $expr->compile($compilationContext); switch ($resolvedExpr->getType()) { case 'int': case 'long': case 'string': return $compilationContext->backend->arrayIsset( $variable, $resolvedExpr, $left['right'], $compilationContext ); case 'variable': case 'mixed': $indexVariable = $compilationContext->symbolTable->getVariableForRead( $resolvedExpr->getCode(), $compilationContext, $left['right'] ); return $compilationContext->backend->arrayIsset( $variable, $indexVariable, $left['right'], $compilationContext ); default: throw new CompilerException('[' . $left['right']['type'] . ']', $expression); } case 'property-access': case 'property-dynamic-access': $compilationContext->headersManager->add('kernel/object'); $exprVariable = new Expression($left['left']); $exprVariable->setReadOnly(true); $exprCompiledVariable = $exprVariable->compile($compilationContext); if ('variable' !== $exprCompiledVariable->getType()) { throw new CompilerException( 'Expression type: ' . $exprCompiledVariable->getType() . ' cannot be used as object', $left['left'] ); } $variable = $compilationContext->symbolTable->getVariableForRead( $exprCompiledVariable->getCode(), $compilationContext, $left['left'] ); if ('variable' !== $variable->getType()) { throw new CompilerException( 'Variable type: ' . $variable->getType() . ' cannot be used as object', $left['left'] ); } if ($variable->hasDifferentDynamicType(['undefined', 'object', 'null'])) { $compilationContext->logger->warning( 'Possible attempt to use non object in isset operator', ['non-valid-isset', $expression] ); } $variableCode = $compilationContext->backend->getVariableCode($variable); if ('property-access' === $left['type']) { return $compilationContext->backend->propertyIsset($variable, $left['right']['value']); } $expr = new Expression($left['right']); $expr->setReadOnly(true); $expr->setNoisy(false); $resolvedExpr = $expr->compile($compilationContext); switch ($resolvedExpr->getType()) { case 'variable': $indexVariable = $compilationContext->symbolTable->getVariableForRead( $resolvedExpr->getCode(), $compilationContext, $left['right'] ); switch ($indexVariable->getType()) { case 'variable': case 'mixed': case 'string': $indexVariableCode = $compilationContext->backend->getVariableCode($indexVariable); return new CompiledExpression( 'bool', 'zephir_isset_property_zval(' . $variableCode . ', ' . $indexVariableCode . ')', $left['right'] ); default: throw new CompilerException('[' . $indexVariable->getType() . ']', $expression); } default: throw new CompilerException('[' . $expression['left']['right']['type'] . ']', $expression); } case 'property-string-access': case 'static-property-access': return new CompiledExpression('bool', '(0 == 0)', $left); default: throw new CompilerException('This expression is not valid for "isset" operator', $expression); } }
Compiles an 'isset' operator. @param array $expression @param CompilationContext $compilationContext @return CompiledExpression @throws Exception @throws ReflectionException
compile
php
zephir-lang/zephir
src/Operators/Other/IssetOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Other/IssetOperator.php
MIT
public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression { $codePrinter = $compilationContext->codePrinter; /** * Resolves the symbol that expects the value */ $this->literalOnly = false; $symbolVariable = $this->getExpectedNonLiteral($compilationContext, $expression); if (!$symbolVariable->isVariable()) { throw new CompilerException('Objects can only be instantiated into dynamic variables', $expression); } if ($symbolVariable->isLocalOnly()) { throw new CompilerException('Cannot use non-heap variable to store new instance', $expression); } if ('return_value' !== $symbolVariable->getName()) { if ($symbolVariable->hasDifferentDynamicType(['unknown', 'undefined', 'object', 'null'])) { $compilationContext->logger->warning( 'Possible attempt to use non-object in "new" operator', ['non-valid-new', $expression] ); } } /** * Mark variables as dynamic objects */ $symbolVariable->setDynamicTypes('object'); $dynamic = false; if ('self' == $expression['class'] || 'static' == $expression['class']) { $className = $compilationContext->classDefinition->getCompleteName(); } else { $className = $expression['class']; $dynamic = $expression['dynamic']; if (!$dynamic) { $className = $compilationContext->getFullName($expression['class']); } } if (!$className) { throw new CompilerException('A class name is required to instantiate the object', $expression); } /** * stdclass doesn't have constructors. */ $lowerClassName = strtolower($className); $isStdClass = 'stdclass' === $lowerClassName || '\stdclass' === $lowerClassName; if ($isStdClass) { if (isset($expression['parameters']) && count($expression['parameters']) > 0) { throw new CompilerException('stdclass does not receive parameters in its constructor', $expression); } $compilationContext->backend->initObject($symbolVariable, null, $compilationContext); $symbolVariable->setClassTypes('stdclass'); } else { $classDefinition = false; if ($compilationContext->compiler->isClass($className)) { $classDefinition = $compilationContext->compiler->getClassDefinition($className); } /** * Classes inside the same extension */ if ($classDefinition) { $compilationContext->backend->initObject( $symbolVariable, $classDefinition->getClassEntry($compilationContext), $compilationContext ); $symbolVariable->setClassTypes($className); $symbolVariable->setAssociatedClass($classDefinition); } else { /** * Classes outside the extension */ if ($dynamic) { $classNameVariable = $compilationContext->symbolTable->getVariableForRead( $className, $compilationContext, $expression ); if ($classNameVariable->isNotVariableAndString()) { throw new CompilerException( 'Only dynamic/string variables can be used in new operator. ' . $classNameVariable->getName( ), $expression ); } /** * Use a safe string version of the variable to avoid segfaults */ $compilationContext->headersManager->add('kernel/object'); $safeSymbolVariable = $compilationContext->symbolTable->getTempVariable( 'variable', $compilationContext ); $safeSymbolVariable->setMustInitNull(true); $safeSymbolVariable->setIsInitialized(true, $compilationContext); $safeSymbolVariable->increaseUses(); $safeSymbol = $compilationContext->backend->getVariableCode($safeSymbolVariable); $classNameSymbol = $compilationContext->backend->getVariableCode($classNameVariable); $compilationContext->codePrinter->output( 'zephir_fetch_safe_class(' . $safeSymbol . ', ' . $classNameSymbol . ');' ); $classNameToFetch = 'Z_STRVAL_P(' . $safeSymbol . '), Z_STRLEN_P(' . $safeSymbol . ')'; $zendClassEntry = $compilationContext->cacheManager->getClassEntryCache()->get( $classNameToFetch, true, $compilationContext ); $classEntry = $zendClassEntry->getName(); $compilationContext->codePrinter->output('if(!' . $classEntry . ') {'); $compilationContext->codePrinter->increaseLevel(); $compilationContext->codePrinter->output('RETURN_MM_NULL();'); $compilationContext->codePrinter->decreaseLevel(); $compilationContext->codePrinter->output('}'); } else { if (!class_exists($className, false)) { $compilationContext->logger->warning( 'Class "' . $className . '" does not exist at compile time', ['nonexistent-class', $expression] ); $classNameToFetch = 'SL("' . Entry::escape($className) . '")'; $zendClassEntry = $compilationContext->cacheManager->getClassEntryCache()->get( $classNameToFetch, false, $compilationContext ); $classEntry = $zendClassEntry->getName(); } else { $reflectionClass = new ReflectionClass($className); if ($reflectionClass->isInterface()) { throw new CompilerException('Interfaces cannot be instantiated', $expression); } if (method_exists($reflectionClass, 'isTrait') && $reflectionClass->isTrait()) { throw new CompilerException('Traits cannot be instantiated', $expression); } $classEntry = (new Entry($expression['class'], $compilationContext))->get(); $symbolVariable->setAssociatedClass($reflectionClass); } $symbolVariable->setClassTypes($className); } $compilationContext->backend->initObject($symbolVariable, $classEntry, $compilationContext); } } /** * Mark variable initialized */ $symbolVariable->setIsInitialized(true, $compilationContext); /** * Don't check the constructor for stdclass instances */ if ($isStdClass) { return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); } /** * Call the constructor * For classes in the same extension we check if the class does implement a constructor * For external classes we always assume the class does implement a constructor. */ $callConstructor = false; if ($compilationContext->compiler->isClass($className)) { $classDefinition = $compilationContext->compiler->getClassDefinition($className); if ('class' != $classDefinition->getType()) { throw new CompilerException('Only classes can be instantiated', $expression); } $callConstructor = $classDefinition->hasMethod('__construct'); } else { if ($compilationContext->compiler->isBundledClass($className)) { $classDefinition = $compilationContext->compiler->getInternalClassDefinition($className); $callConstructor = $classDefinition->hasMethod('__construct'); } } /* TODO: use the MethodBuilder here */ if (isset($expression['parameters'])) { $callExpr = new Expression([ 'variable' => ['type' => 'variable', 'value' => $symbolVariable->getRealName()], 'name' => '__construct', 'parameters' => $expression['parameters'], 'call-type' => MethodCall::CALL_NORMAL, 'file' => $expression['file'], 'line' => $expression['line'], 'char' => $expression['char'], 'check' => $callConstructor, ]); } else { $callExpr = new Expression([ 'variable' => ['type' => 'variable', 'value' => $symbolVariable->getRealName()], 'name' => '__construct', 'call-type' => MethodCall::CALL_NORMAL, 'file' => $expression['file'], 'line' => $expression['line'], 'char' => $expression['char'], 'check' => $callConstructor, ]); } /** * If we are certain that there is a constructor we call it, otherwise we checked it at runtime. */ if ($callConstructor) { $methodCall = new MethodCall(); $callExpr->setExpectReturn(false); $methodCall->compile($callExpr, $compilationContext); } else { $compilationContext->headersManager->add('kernel/fcall'); /* TODO:, generate the code using builders */ $compilationContext->backend->checkConstructor($symbolVariable, $compilationContext); $codePrinter->increaseLevel(); $methodCall = new MethodCall(); $callExpr->setExpectReturn(false); $methodCall->compile($callExpr, $compilationContext); $codePrinter->decreaseLevel(); $codePrinter->output('}'); $codePrinter->outputBlankLine(); } return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
Creates a new instance. @param array $expression @param CompilationContext $compilationContext @return CompiledExpression @throws ReflectionException @throws Exception
compile
php
zephir-lang/zephir
src/Operators/Other/NewInstanceOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Other/NewInstanceOperator.php
MIT
public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression { if (!isset($expression['parameters'])) { throw new CompilerException("Invalid 'parameters' for new-type", $expression); } switch ($expression['internal-type']) { case 'array': $compilationContext->headersManager->add('kernel/array'); $functionName = 'create_array'; break; case 'string': $compilationContext->headersManager->add('kernel/string'); $functionName = 'create_string'; break; default: throw new CompilerException('Cannot build instance of type', $expression); } $builder = new FunctionCallBuilder( $functionName, $expression['parameters'], 1, $expression['file'], $expression['line'], $expression['char'] ); /** * Implicit type coercing. */ $castBuilder = new CastOperatorBuilder($expression['internal-type'], $builder); $expression = new Expression($castBuilder->get()); $expression->setReadOnly($this->readOnly); try { return $expression->compile($compilationContext); } catch (Exception $e) { throw new CompilerException($e->getMessage(), $expression, $e->getCode(), $e); } }
Executes the operator. @param array $expression @param CompilationContext $compilationContext @return CompiledExpression @throws CompilerException
compile
php
zephir-lang/zephir
src/Operators/Other/NewInstanceTypeOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Other/NewInstanceTypeOperator.php
MIT
public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression { if (!isset($expression['left'])) { throw new CompilerException("Invalid 'left' operand for 'irange' expression", $expression['left']); } if (!isset($expression['right'])) { throw new CompilerException("Invalid 'right' operand for 'irange' expression", $expression['right']); } $exprBuilder = BuilderFactory::getInstance(); /** * Implicit type coercing. */ $castBuilder = $exprBuilder->operators()->cast( 'array', $exprBuilder->statements() ->functionCall('range', [$expression['left'], $expression['right']]) ); $expression = new Expression($castBuilder->build()); $expression->setReadOnly($this->readOnly); return $expression->compile($compilationContext); }
@param array $expression @param CompilationContext $compilationContext @return CompiledExpression @throws Exception
compile
php
zephir-lang/zephir
src/Operators/Other/RangeExclusiveOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Other/RangeExclusiveOperator.php
MIT
public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression { if (!isset($expression['left'])) { throw new CompilerException("Invalid 'left' operand for 'irange' expression", $expression['left']); } if (!isset($expression['right'])) { throw new CompilerException("Invalid 'right' operand for 'irange' expression", $expression['right']); } $exprBuilder = Expression\Builder\BuilderFactory::getInstance(); /** * Implicit type coercing. */ $castBuilder = $exprBuilder->operators()->cast( Types::T_ARRAY, $exprBuilder->statements() ->functionCall('range', [$expression['left'], $expression['right']]) ); $expression = new Expression($castBuilder->build()); $expression->setReadOnly($this->readOnly); return $expression->compile($compilationContext); }
@param array $expression @param CompilationContext $compilationContext @return CompiledExpression @throws Exception
compile
php
zephir-lang/zephir
src/Operators/Other/RangeInclusiveOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Other/RangeInclusiveOperator.php
MIT
public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression { $expr = new Expression($expression['left']); $expr->setReadOnly(true); $expr->setExpectReturn(true); $exprPath = $expr->compile($compilationContext); if ('variable' === $exprPath->getType()) { $exprVariable = $compilationContext->symbolTable->getVariableForRead( $exprPath->getCode(), $compilationContext, $expression ); $exprVar = $compilationContext->backend->getVariableCode($exprVariable); if ('variable' === $exprVariable->getType()) { if ($exprVariable->hasDifferentDynamicType(['undefined', 'string'])) { $compilationContext->logger->warning( 'Possible attempt to use invalid type as path in "' . $this->operator . '" operator', [$this->warningName, $expression] ); } } } else { $exprVar = $compilationContext->symbolTable->getTempVariableForWrite( 'variable', $compilationContext, $expression ); $compilationContext->backend->assignString($exprVar, $exprPath->getCode(), $compilationContext); $exprVar = $compilationContext->backend->getVariableCode($exprVar); } $symbolVariable = false; if ($this->isExpecting()) { $symbolVariable = $compilationContext->symbolTable->getTempVariableForObserveOrNullify( 'variable', $compilationContext ); } $compilationContext->headersManager->add('kernel/memory'); $compilationContext->headersManager->add('kernel/require'); $codePrinter = $compilationContext->codePrinter; if ($symbolVariable) { $codePrinter->output('ZEPHIR_OBSERVE_OR_NULLIFY_PPZV(&' . $symbolVariable->getName() . ');'); $symbol = $compilationContext->backend->getVariableCode($symbolVariable); $codePrinter->output( 'if (' . $this->zephirMethodRet . '(' . $symbol . ', ' . $exprVar . ') == FAILURE) {' ); } else { $codePrinter->output('if (' . $this->zephirMethod . '(' . $exprVar . ') == FAILURE) {'); } $codePrinter->output("\t" . 'RETURN_MM_NULL();'); $codePrinter->output('}'); if ($symbolVariable) { return new CompiledExpression('variable', $symbolVariable->getName(), $expression); } return new CompiledExpression('null', null, $expression); }
@param array $expression @param CompilationContext $compilationContext @return CompiledExpression @throws Exception @throws ReflectionException
compile
php
zephir-lang/zephir
src/Operators/Other/RequireOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Other/RequireOperator.php
MIT
public function compile($expression, CompilationContext $compilationContext): CompiledExpression { /** * This variable is used to check if the compound and expression is evaluated as true or false: * Ensure that newly allocated variables are local-only (setReadOnly) */ $this->setReadOnly(false); $returnVariable = $this->getExpected($compilationContext, $expression, false); /* Make sure that passed variables (passed symbol variables) are promoted */ $returnVariable->setLocalOnly(false); if ('variable' != $returnVariable->getType() || 'return_value' == $returnVariable->getName()) { $returnVariable = $compilationContext->symbolTable->getTempVariableForWrite( 'variable', $compilationContext ); $this->checkVariableTemporal($returnVariable); } $ifBuilder = new IfStatementBuilder( new UnaryOperatorBuilder( 'not', $expression['left'] ), new StatementsBlockBuilder([ /** * Create an implicit 'let' operation to update the evaluated right operator */ new LetStatementBuilder([ 'assign-type' => 'variable', 'variable' => $returnVariable->getName(), 'operator' => 'assign', 'expr' => $expression['extra'], 'file' => $expression['file'], 'line' => $expression['line'], 'char' => $expression['char'], ], $expression['extra']), ]), new StatementsBlockBuilder([ /** * Create an implicit 'let' operation to update the evaluated right operator */ new LetStatementBuilder([ 'assign-type' => 'variable', 'variable' => $returnVariable->getName(), 'operator' => 'assign', 'expr' => $expression['left'], 'file' => $expression['file'], 'line' => $expression['line'], 'char' => $expression['char'], ], $expression['extra']), ]) ); $ifStatement = new IfStatement($ifBuilder->get()); $ifStatement->compile($compilationContext); return new CompiledExpression('variable', $returnVariable->getName(), $expression); }
Compile ternary operator. @param $expression @param CompilationContext $compilationContext @return CompiledExpression
compile
php
zephir-lang/zephir
src/Operators/Other/ShortTernaryOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Other/ShortTernaryOperator.php
MIT
public function compile($expression, CompilationContext $compilationContext): CompiledExpression { /** * This variable is used to check if the compound and expression is evaluated as true or false: * Ensure that newly allocated variables are local-only (setReadOnly) */ $this->setReadOnly(false); $returnVariable = $this->getExpected($compilationContext, $expression, false); /* Make sure that passed variables (passed symbol variables) are promoted */ $returnVariable->setLocalOnly(false); if ('variable' != $returnVariable->getType() || 'return_value' == $returnVariable->getName()) { $returnVariable = $compilationContext->symbolTable->getTempVariableForWrite( 'variable', $compilationContext ); $this->checkVariableTemporal($returnVariable); } $expr = new EvalExpression(); $condition = $expr->optimize($expression['left'], $compilationContext); $compilationContext->codePrinter->output('if (' . $condition . ') {'); $compilationContext->codePrinter->increaseLevel(); /** * Create an implicit 'let' operation to update the evaluated left operator. */ $statement = new LetStatement([ 'type' => 'let', 'assignments' => [ [ 'assign-type' => 'variable', 'variable' => $returnVariable->getName(), 'operator' => 'assign', 'expr' => $expression['right'], 'file' => $expression['file'], 'line' => $expression['line'], 'char' => $expression['char'], ], ], ]); $statement->compile($compilationContext); $compilationContext->codePrinter->decreaseLevel(); $compilationContext->codePrinter->output('} else {'); $compilationContext->codePrinter->increaseLevel(); /** * Create an implicit 'let' operation to update the evaluated left operator. */ $statement = new LetStatement([ 'type' => 'let', 'assignments' => [ [ 'assign-type' => 'variable', 'variable' => $returnVariable->getName(), 'operator' => 'assign', 'expr' => $expression['extra'], 'file' => $expression['file'], 'line' => $expression['line'], 'char' => $expression['char'], ], ], ]); $statement->compile($compilationContext); $compilationContext->codePrinter->decreaseLevel(); $compilationContext->codePrinter->output('}'); return new CompiledExpression('variable', $returnVariable->getName(), $expression); }
Compile ternary operator. @param $expression @param CompilationContext $compilationContext @return CompiledExpression
compile
php
zephir-lang/zephir
src/Operators/Other/TernaryOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Other/TernaryOperator.php
MIT
public function compile(array $expression, CompilationContext $compilationContext): CompiledExpression { $expr = new Expression($expression['right']); $expr->setReadOnly(true); $resolved = $expr->compile($compilationContext); if ('variable' !== $resolved->getType()) { throw new CompilerException('Type-Hints only can be applied to dynamic variables.', $expression); } $symbolVariable = $compilationContext->symbolTable->getVariableForRead( $resolved->getCode(), $compilationContext, $expression ); if (!$symbolVariable->isVariable()) { throw new CompilerException('Type-Hints only can be applied to dynamic variables.', $expression); } $symbolVariable->setDynamicTypes('object'); $symbolVariable->setClassTypes($compilationContext->getFullName($expression['left']['value'])); return $resolved; }
Performs type-hint compilation. @param array $expression @param CompilationContext $compilationContext @return CompiledExpression @throws Exception @throws ReflectionException
compile
php
zephir-lang/zephir
src/Operators/Other/TypeHintOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Other/TypeHintOperator.php
MIT
public function compile($expression, CompilationContext $compilationContext): CompiledExpression { if (!isset($expression['left'])) { throw new CompilerException("Invalid 'left' operand for 'typeof' expression", $expression['left']); } $functionCall = BuilderFactory::getInstance()->statements()->functionCall('gettype', [$expression['left']]); $expression = new Expression($functionCall->build()); return $expression->compile($compilationContext); }
@param $expression @param CompilationContext $compilationContext @return CompiledExpression @throws Exception @throws ReflectionException
compile
php
zephir-lang/zephir
src/Operators/Other/TypeOfOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Other/TypeOfOperator.php
MIT
public function compile($expression, CompilationContext $compilationContext): CompiledExpression { if (!isset($expression['left'])) { throw new CompilerException( "Invalid 'left' operand for '" . $this->operator . "' expression", $expression['left'] ); } $leftExpr = new Expression($expression['left']); $leftExpr->setReadOnly(true); $left = $leftExpr->compile($compilationContext); if ('bool' === $left->getType()) { return new CompiledExpression( 'bool', $this->zephirMethod . '(' . $left->getCode() . ')', $expression ); } if ('variable' === $left->getType()) { $variable = $compilationContext->symbolTable->getVariableForRead( $left->getCode(), $compilationContext, $expression['left'] ); switch ($variable->getType()) { case 'bool': return new CompiledExpression( 'bool', $this->zephirMethod . '(' . $variable->getName() . ')', $expression ); default: $compilationContext->headersManager->add('kernel/operators'); $symbol = $compilationContext->backend->getVariableCode($variable); return new CompiledExpression('bool', 'UNEXPECTED(zephir_is_true(' . $symbol . '))', $expression); } } throw new CompilerException( "Cannot use expression type: '" . $left->getType() . "' in '" . $this->operator . "' operator", $expression['left'] ); }
Compile likely/unlikely operator @param array $expression @param CompilationContext $compilationContext @return CompiledExpression @throws Exception @throws ReflectionException
compile
php
zephir-lang/zephir
src/Operators/Other/UnlikelyOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Other/UnlikelyOperator.php
MIT
public function compile($expression, CompilationContext $compilationContext): CompiledExpression { $this->checkLeft($expression); $leftExpr = new Expression($expression['left']); $leftExpr->setReadOnly($this->readOnly); $left = $leftExpr->compile($compilationContext); switch ($left->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'double': return new CompiledExpression($left->getType(), '-' . $left->getCode(), $expression); case 'variable': $variable = $compilationContext->symbolTable->getVariable($left->getCode()); switch ($variable->getType()) { case 'int': case 'uint': case 'long': case 'ulong': case 'double': return new CompiledExpression($variable->getType(), '-' . $variable->getName(), $expression); case 'variable': $compilationContext->headersManager->add('kernel/operators'); $compilationContext->codePrinter->output( 'zephir_negate(' . $compilationContext->backend->getVariableCode($variable) . ');' ); return new CompiledExpression('variable', $variable->getName(), $expression); default: throw new CompilerException( "Cannot operate minus with variable of '" . $left->getType() . "' type" ); } default: throw new CompilerException("Cannot operate minus with '" . $left->getType() . "' type"); } }
Compile expression. @param $expression @param CompilationContext $compilationContext @return CompiledExpression @throws Exception @throws ReflectionException
compile
php
zephir-lang/zephir
src/Operators/Unary/MinusOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Unary/MinusOperator.php
MIT
public function compile($expression, CompilationContext $compilationContext): CompiledExpression { $this->checkLeft($expression); $leftExpr = new Expression($expression['left']); $leftExpr->setReadOnly($this->readOnly); $left = $leftExpr->compile($compilationContext); switch ($left->getType()) { case 'bool': case 'int': case 'uint': case 'long': case 'ulong': return new CompiledExpression('bool', '!(' . $left->getCode() . ')', $expression); case 'variable': $variable = $compilationContext->symbolTable->getVariableForRead( $left->getCode(), $compilationContext, $expression['left'] ); switch ($variable->getType()) { case 'bool': case 'int': case 'uint': case 'long': return new CompiledExpression('bool', '!' . $variable->getName(), $expression); case 'variable': case 'mixed': $compilationContext->headersManager->add('kernel/operators'); $symbol = $compilationContext->backend->getVariableCode($variable); return new CompiledExpression('bool', '!zephir_is_true(' . $symbol . ')', $expression); default: throw new CompilerException('Unknown type: ' . $variable->getType(), $expression); } default: throw new CompilerException('Unknown type: ' . $left->getType(), $expression); } }
@param $expression @param CompilationContext $compilationContext @return CompiledExpression @throws Exception @throws ReflectionException
compile
php
zephir-lang/zephir
src/Operators/Unary/NotOperator.php
https://github.com/zephir-lang/zephir/blob/master/src/Operators/Unary/NotOperator.php
MIT
public function getEvalVariable() { return end($this->usedVariables); }
Returns the variable evaluated by the EvalExpression. @return Variable
getEvalVariable
php
zephir-lang/zephir
src/Optimizers/EvalExpression.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/EvalExpression.php
MIT
public function isUnreachable(): ?bool { return $this->unreachable; }
Checks if the evaluation produce unreachable code. @return bool|null
isUnreachable
php
zephir-lang/zephir
src/Optimizers/EvalExpression.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/EvalExpression.php
MIT
public function isUnreachableElse(): ?bool { return $this->unreachableElse; }
Checks if the evaluation not produce unreachable code. @return bool|null
isUnreachableElse
php
zephir-lang/zephir
src/Optimizers/EvalExpression.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/EvalExpression.php
MIT
public function optimize($exprRaw, CompilationContext $compilationContext) { $conditions = $this->optimizeNot($exprRaw, $compilationContext); if (null !== $conditions) { return $conditions; } /** * Discard first level parentheses */ if ('list' === $exprRaw['type']) { $expr = new Expression($exprRaw['left']); } else { $expr = new Expression($exprRaw); } $expr->setReadOnly(true); $expr->setEvalMode(true); $compiledExpression = $expr->compile($compilationContext); /** * Possible corrupted expression? */ if (!is_object($compiledExpression)) { throw new CompilerException('Corrupted expression: ' . $exprRaw['type'], $exprRaw); } /** * Generate the condition according to the value returned by the evaluated expression */ switch ($compiledExpression->getType()) { case 'null': $this->unreachable = true; return '0'; case 'int': case 'uint': case 'long': case 'ulong': case 'double': $code = $compiledExpression->getCode(); if (is_numeric($code)) { if ('1' == $code) { $this->unreachableElse = true; } else { $this->unreachable = true; } } return $code; case 'char': case 'uchar': return $compiledExpression->getCode(); case 'bool': $code = $compiledExpression->getBooleanCode(); if ('1' == $code) { $this->unreachableElse = true; } else { if ('0' == $code) { $this->unreachable = true; } } return $code; case 'variable': $variableRight = $compilationContext->symbolTable->getVariableForRead( $compiledExpression->getCode(), $compilationContext, $exprRaw ); $possibleValue = $variableRight->getPossibleValue(); if ($possibleValue instanceof LiteralCompiledExpression) { $possibleValueBranch = $variableRight->getPossibleValueBranch(); if ($possibleValueBranch instanceof Branch) { /** * Check if the possible value was assigned in the root branch */ if (Branch::TYPE_ROOT == $possibleValueBranch->getType()) { switch ($possibleValue->getType()) { case 'null': $this->unreachable = true; break; case 'bool': if ('0' == $possibleValue->getBooleanCode()) { $this->unreachable = true; } else { $this->unreachableElse = true; } break; case 'int': if (!(int)$possibleValue->getCode()) { $this->unreachable = true; } else { $this->unreachableElse = true; } break; case 'double': if (!(float)$possibleValue->getCode()) { $this->unreachable = true; } else { $this->unreachableElse = true; } break; } } } } $this->usedVariables[] = $variableRight->getName(); /** * Evaluate the variable */ switch ($variableRight->getType()) { case 'int': case 'uint': case 'char': case 'uchar': case 'long': case 'bool': case 'double': case 'ulong': return $variableRight->getName(); case 'string': return '!(' . $compilationContext->backend->ifVariableValueUndefined( $variableRight, $compilationContext, true, false ) . ')'; case 'variable': $compilationContext->headersManager->add('kernel/operators'); $variableRightCode = $compilationContext->backend->getVariableCode($variableRight); return 'zephir_is_true(' . $variableRightCode . ')'; default: throw new CompilerException( "Variable can't be evaluated " . $variableRight->getType(), $exprRaw ); } break; default: throw new CompilerException( 'Expression ' . $compiledExpression->getType() . " can't be evaluated", $exprRaw ); } }
Optimizes expressions. @param $exprRaw @param CompilationContext $compilationContext @return bool|string @throws Exception
optimize
php
zephir-lang/zephir
src/Optimizers/EvalExpression.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/EvalExpression.php
MIT
public function optimizeNot(array $expr, CompilationContext $compilationContext): ?string { /** * Compile the expression negating the evaluated expression */ if ('not' == $expr['type']) { $conditions = $this->optimize($expr['left'], $compilationContext); if (false !== $conditions) { if (null !== $this->unreachable) { $this->unreachable = !$this->unreachable; } if (null !== $this->unreachableElse) { $this->unreachableElse = !$this->unreachableElse; } return '!(' . $conditions . ')'; } } return null; }
Skips the not operator by recursively optimizing the expression at its right. @param array $expr @param CompilationContext $compilationContext @return string|null @throws Exception
optimizeNot
php
zephir-lang/zephir
src/Optimizers/EvalExpression.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/EvalExpression.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context): ?CompiledExpression { if (!isset($expression['parameters']) || count($expression['parameters']) !== 1) { return null; } $resolvedParam = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression)[0]; if ('IS_BOOL' === $this->getType()) { $condition = sprintf( '(Z_TYPE_P(%s) == IS_TRUE || Z_TYPE_P(%s) == IS_FALSE)', $resolvedParam, $resolvedParam, ); } else { $condition = 'Z_TYPE_P(' . $resolvedParam . ') == ' . $this->getType(); } return new CompiledExpression('bool', $condition, $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return CompiledExpression|null @throws Exception
optimize
php
zephir-lang/zephir
src/Optimizers/IsTypeOptimizerAbstract.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/IsTypeOptimizerAbstract.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if (count($expression['parameters']) > 1) { return false; } /** * Resolve parameters as vars */ $call->getResolvedParams($expression['parameters'], $context, $expression); /** * Get CompiledExpression(s) for resolved var(s). */ $resolvedParams = $call->getResolvedParamsAsExpr( $expression['parameters'], $context, $expression ); $compiledExpression = $resolvedParams[0]; switch ($compiledExpression->getType()) { case 'int': case 'float': case 'long': case 'ulong': case 'double': $context->headersManager->add('math'); return $this->passNativeFCall($compiledExpression, $expression); break; case 'variable': $variable = $context->symbolTable->getVariable($compiledExpression->getCode()); switch ($variable->getType()) { case 'int': case 'float': case 'long': case 'ulong': case 'double': $context->headersManager->add('math'); return $this->passNativeFCall($compiledExpression, $expression); break; case 'variable': $context->headersManager->add('kernel/math'); return new CompiledExpression( 'double', 'zephir_' . $this->zephirMethod . '(' . $context->backend->getVariableCode($variable) . ')', $expression ); } } return false; }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/MathOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/MathOptimizer.php
MIT
protected function passNativeFCall($compiledExpression, $expression) { return new CompiledExpression( 'double', $this->zephirMethod . '(' . $compiledExpression->getCode() . ')', $expression ); }
@param CompiledExpression $compiledExpression @param array $expression @return CompiledExpression
passNativeFCall
php
zephir-lang/zephir
src/Optimizers/MathOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/MathOptimizer.php
MIT
protected function checkInitSymbolVariable( Call $call, ?Variable $symbolVariable, CompilationContext $context ): void { if ($call->mustInitSymbolVariable()) { $symbolVariable->initVariant($context); } }
@param Call $call @param Variable|null $symbolVariable @param CompilationContext $context @return void
checkInitSymbolVariable
php
zephir-lang/zephir
src/Optimizers/OptimizerAbstract.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/OptimizerAbstract.php
MIT
protected function processStringOptimizer(Call $call, CompilationContext $context, array $expression): array { /** * Process the expected symbol to be returned */ $call->processExpectedReturn($context); $symbolVariable = $call->getSymbolVariable(true, $context); $this->checkNotVariableString($symbolVariable, $expression); $context->headersManager->add('kernel/string'); $symbolVariable->setDynamicTypes('string'); $resolvedParams = $call->getReadOnlyResolvedParams( $expression['parameters'], $context, $expression ); $this->checkInitSymbolVariable($call, $symbolVariable, $context); $symbol = $context->backend->getVariableCode($symbolVariable); return [$symbolVariable, $resolvedParams, $symbol]; }
@param Call $call @param CompilationContext $context @param array $expression @return array @throws Exception
processStringOptimizer
php
zephir-lang/zephir
src/Optimizers/OptimizerAbstract.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/OptimizerAbstract.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if (count($expression['parameters']) > 1) { return false; } [$symbolVariable, $resolvedParams, $symbol] = $this->processStringOptimizer( $call, $context, $expression ); $context->codePrinter->output('zephir_addslashes(' . $symbol . ', ' . $resolvedParams[0] . ');'); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return false|CompiledExpression @throws Exception
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/AddslashesOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/AddslashesOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if (2 != count($expression['parameters'])) { return false; } $context->headersManager->add('kernel/array'); $resolvedParams = $call->getReadOnlyResolvedParams( $expression['parameters'], $context, $expression ); // Note: the first parameter is key in php array_key_exists return new CompiledExpression( 'bool', $this->getCode($resolvedParams), $expression ); }
@param array $expression @param Call $call @param CompilationContext $context @return false|CompiledExpression @throws Exception
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/ArrayKeyExistsOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/ArrayKeyExistsOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if ($this->parameterCount !== count($expression['parameters'])) { return false; } /* * Process the expected symbol to be returned */ $call->processExpectedReturn($context); $symbolVariable = $call->getSymbolVariable(true, $context); $this->checkNotVariable($symbolVariable, $expression); $context->headersManager->add('kernel/array'); $symbolVariable->setDynamicTypes('array'); $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); $this->checkInitSymbolVariable($call, $symbolVariable, $context); $symbol = $context->backend->getVariableCode($symbolVariable); $context->codePrinter->output($this->getOutput($symbol, $resolvedParams)); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @return false|CompiledExpression @throws Exception
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/ArrayKeysOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/ArrayKeysOptimizer.php
MIT
protected function getOutput(string $symbol, $resolvedParams): string { return sprintf( 'zephir_array_keys(%s, %s);', $symbol, $resolvedParams[0] ); }
@param string $symbol @param array $resolvedParams @return string
getOutput
php
zephir-lang/zephir
src/Optimizers/FunctionCall/ArrayKeysOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/ArrayKeysOptimizer.php
MIT
protected function getOutput(string $symbol, $resolvedParams): string { return sprintf( 'zephir_fast_array_merge(%s, %s, %s);', $symbol, $resolvedParams[0], $resolvedParams[1] ); }
@param string $symbol @param array $resolvedParams @return string
getOutput
php
zephir-lang/zephir
src/Optimizers/FunctionCall/ArrayMergeOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/ArrayMergeOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if (1 != count($expression['parameters'])) { return false; } $context->headersManager->add('kernel/file'); /* * Process the expected symbol to be returned */ $call->processExpectedReturn($context); $symbolVariable = $call->getSymbolVariable(true, $context); $this->checkNotVariableString($symbolVariable, $expression); $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); $this->checkInitSymbolVariable($call, $symbolVariable, $context); $symbol = $context->backend->getVariableCode($symbolVariable); $context->codePrinter->output( $this->zephirMethod . '(' . $symbol . ', ' . $resolvedParams[0] . ');' ); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @return false|CompiledExpression @throws Exception
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/BasenameOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/BasenameOptimizer.php
MIT
protected function getOutput(string $symbol, array $resolvedParams): string { return $this->zephirMethod . '(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ');'; }
@param string $symbol @param array $resolvedParams @return string
getOutput
php
zephir-lang/zephir
src/Optimizers/FunctionCall/CallUserFuncArrayOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/CallUserFuncArrayOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if ($this->parameterCount !== count($expression['parameters'])) { return false; } /* * Process the expected symbol to be returned */ $call->processExpectedReturn($context); $symbolVariable = $call->getSymbolVariable(true, $context); $this->checkNotVariable($symbolVariable, $expression); if (!$symbolVariable) { $symbolVariable = $context->symbolTable->addTemp('variable', $context); $symbolVariable->initVariant($context); } /* * Add the last call status to the current symbol table */ $call->addCallStatusFlag($context); $resolvedParams = $call->getReadOnlyResolvedParams( $expression['parameters'], $context, $expression ); $context->headersManager->add('kernel/fcall'); /* * Add the last call status to the current symbol table */ $call->addCallStatusFlag($context); $this->checkInitSymbolVariable($call, $symbolVariable, $context); $symbol = $context->backend->getVariableCode($symbolVariable); $context->codePrinter->output($this->getOutput($symbol, $resolvedParams)); $call->addCallStatusOrJump($context); return new CompiledExpression('variable', $symbolVariable->getName(), $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/CallUserFuncOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/CallUserFuncOptimizer.php
MIT
protected function getOutput(string $symbol, array $resolvedParams): string { return $this->zephirMethod . '(' . $symbol . ', ' . $resolvedParams[0] . ');'; }
@param string $symbol @param array $resolvedParams @return string
getOutput
php
zephir-lang/zephir
src/Optimizers/FunctionCall/CallUserFuncOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/CallUserFuncOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } $this->checkParameters($expression['parameters']); $charlist = 'NULL '; if (2 == count($expression['parameters'])) { if ('null' == $expression['parameters'][1]['parameter']['type']) { unset($expression['parameters'][1]); } } /* * Process the expected symbol to be returned */ $call->processExpectedReturn($context); $symbolVariable = $call->getSymbolVariable(true, $context); $this->checkNotVariableString($symbolVariable, $expression); $this->symbolVariablePre($call, $symbolVariable, $context); $context->headersManager->add('kernel/string'); $symbolVariable->setDynamicTypes('string'); $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); if (isset($resolvedParams[1])) { $charlist = $resolvedParams[1]; } $this->symbolVariablePost($call, $symbolVariable, $context); $symbol = $context->backend->getVariableCode($symbolVariable); $context->codePrinter->output( $this->zephirMethod . '(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $charlist . $this->trimWhere . ');' ); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/CamelizeOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/CamelizeOptimizer.php
MIT
protected function symbolVariablePost( Call $call, ?Variable $symbolVariable, CompilationContext $context ): void { // empty }
@param Call $call @param Variable|null $symbolVariable @param CompilationContext $context @return void
symbolVariablePost
php
zephir-lang/zephir
src/Optimizers/FunctionCall/CamelizeOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/CamelizeOptimizer.php
MIT
protected function symbolVariablePre( Call $call, ?Variable $symbolVariable, CompilationContext $context ): void { $this->checkInitSymbolVariable($call, $symbolVariable, $context); }
@param Call $call @param Variable|null $symbolVariable @param CompilationContext $context @return void
symbolVariablePre
php
zephir-lang/zephir
src/Optimizers/FunctionCall/CamelizeOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/CamelizeOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if (1 != count($expression['parameters'])) { return false; } $context->headersManager->add('kernel/math'); $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); return new CompiledExpression('double', 'zephir_ceil(' . $resolvedParams[0] . ')', $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/CeilOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/CeilOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if (count($expression['parameters']) < 1) { throw new CompilerException("'class_exists' require one or two parameters"); } $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); /* * Process autoload */ if (2 == count($resolvedParams)) { $context->headersManager->add('kernel/operators'); $autoload = 'zephir_is_true(' . $resolvedParams[1] . ') '; } else { $autoload = '1'; } $context->headersManager->add('kernel/object'); return new CompiledExpression( 'bool', 'zephir_class_exists(' . $resolvedParams[0] . ', ' . $autoload . ')', $expression ); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/ClassExistsOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/ClassExistsOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if (2 != count($expression['parameters'])) { return false; } $context->headersManager->add('kernel/file'); /* * Process the expected symbol to be returned */ $call->processExpectedReturn($context); $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); return new CompiledExpression( 'bool', 'zephir_compare_mtime(' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ')', $expression ); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/CompareMtimeOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/CompareMtimeOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if (1 != count($expression['parameters'])) { return false; } $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); return new CompiledExpression('int', 'zephir_fast_count_int(' . $resolvedParams[0] . ')', $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/CountOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/CountOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (isset($expression['parameters'])) { if (count($expression['parameters']) > 1) { throw new CompilerException('This function only requires one parameter', $expression); } } /* * Process the expected symbol to be returned */ $call->processExpectedReturn($context); $symbolVariable = $call->getSymbolVariable(true, $context); $this->checkNotVariable($symbolVariable, $expression); /* * Add the last call status to the current symbol table */ $call->addCallStatusFlag($context); $context->headersManager->add('kernel/array'); $symbolVariable->setDynamicTypes('array'); if (isset($expression['parameters'])) { $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); } else { $resolvedParams = null; } $this->checkInitSymbolVariable($call, $symbolVariable, $context); $symbol = $context->backend->getVariableCode($symbolVariable); if ($resolvedParams) { $context->codePrinter->output( 'zephir_create_array(' . $symbol . ', zephir_get_intval(' . $resolvedParams[0] . '), 1);' ); } else { $context->codePrinter->output('zephir_create_array(' . $symbol . ', 0, 1);'); } return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/CreateArrayOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/CreateArrayOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { throw new CompilerException('This function requires parameters', $expression); } if ($this->parameterCount !== count($expression['parameters'])) { throw new CompilerException( $this->exceptionMessage, $expression ); } /* * Process the expected symbol to be returned */ $call->processExpectedReturn($context); $symbolVariable = $call->getSymbolVariable(true, $context); $this->checkNotVariable($symbolVariable, $expression); /* * Add the last call status to the current symbol table */ $call->addCallStatusFlag($context); $context->headersManager->add('kernel/object'); $symbolVariable->setDynamicTypes('object'); $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); $this->checkInitSymbolVariable($call, $symbolVariable, $context); /* * Add the last call status to the current symbol table */ $call->addCallStatusFlag($context); $symbol = $context->backend->getVariableCode($symbolVariable); $context->codePrinter->output($this->getOutput($symbol, $resolvedParams)); $this->getTempParameters($call, $context); $call->addCallStatusOrJump($context); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/CreateInstanceOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/CreateInstanceOptimizer.php
MIT
protected function getOutput(string $symbol, array $resolvedParams): string { return 'ZEPHIR_LAST_CALL_STATUS = ' . 'zephir_create_instance(' . $symbol . ', ' . $resolvedParams[0] . ');'; }
@param string $symbol @param array $resolvedParams @return string
getOutput
php
zephir-lang/zephir
src/Optimizers/FunctionCall/CreateInstanceOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/CreateInstanceOptimizer.php
MIT
protected function getTempParameters(Call $call, CompilationContext $context): void { $call->checkTempParameters($context); }
@param Call $call @param CompilationContext $context @return void
getTempParameters
php
zephir-lang/zephir
src/Optimizers/FunctionCall/CreateInstanceOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/CreateInstanceOptimizer.php
MIT
protected function getOutput(string $symbol, array $resolvedParams): string { return 'ZEPHIR_LAST_CALL_STATUS = ' . 'zephir_create_instance_params(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ');'; }
@param string $symbol @param array $resolvedParams @return string
getOutput
php
zephir-lang/zephir
src/Optimizers/FunctionCall/CreateInstanceParamsOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/CreateInstanceParamsOptimizer.php
MIT
protected function getTempParameters(Call $call, CompilationContext $context): void { // Not needed here }
@param Call $call @param CompilationContext $context @return void
getTempParameters
php
zephir-lang/zephir
src/Optimizers/FunctionCall/CreateInstanceParamsOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/CreateInstanceParamsOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (isset($expression['parameters'])) { if (0 != count($expression['parameters'])) { throw new CompilerException("This function doesn't require parameters", $expression); } } /** * Process the expected symbol to be returned */ $call->processExpectedReturn($context); $symbolVariable = $call->getSymbolVariable(true, $context); $this->checkNotVariable($symbolVariable, $expression); $this->checkInitSymbolVariable($call, $symbolVariable, $context); // TODO: Still needed? $context->symbolTable->mustGrownStack(true); $context->codePrinter->output('ZEPHIR_CREATE_SYMBOL_TABLE();'); $context->codePrinter->output(''); return new CompiledExpression('null', null, $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/CreateSymbolTableOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/CreateSymbolTableOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if (1 != count($expression['parameters'])) { return false; } $context->headersManager->add('kernel/operators'); $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); return new CompiledExpression('double', 'zephir_get_doubleval(' . $resolvedParams[0] . ')', $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/DoublevalOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/DoublevalOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if (2 != count($expression['parameters']) && 3 != count($expression['parameters'])) { return false; } if ('string' == $expression['parameters'][1]['parameter']['type']) { $str = $expression['parameters'][1]['parameter']['value']; unset($expression['parameters'][1]); } $resolvedParams = $call->getReadOnlyResolvedParams( $expression['parameters'], $context, $expression ); $context->headersManager->add('kernel/string'); if (isset($str)) { return new CompiledExpression( 'bool', $this->zephirMethodStr . '(' . $resolvedParams[0] . ', SL("' . $str . '"))', $expression ); } if (2 == count($expression['parameters'])) { return new CompiledExpression( 'bool', $this->zephirMethod . '(' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ', NULL)', $expression ); } else { return new CompiledExpression( 'bool', $this->zephirMethod . '(' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ', ' . $resolvedParams[2] . ')', $expression ); } }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/EndsWithOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/EndsWithOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters']) || count($expression['parameters']) > 1) { return false; } /* * Process the expected symbol to be returned */ $call->processExpectedReturn($context); $symbolVariable = $call->getSymbolVariable(true, $context); $this->checkNotVariableString($symbolVariable, $expression); $context->headersManager->add('kernel/fcall'); $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); $this->checkInitSymbolVariable($call, $symbolVariable, $context); $evalContext = str_replace( [getcwd() . '\\', getcwd() . '/'], '', $expression['file'] . ':' . $expression['line'] ); $symbol = $context->backend->getVariableCode($symbolVariable); $context->codePrinter->output( sprintf('zephir_eval_php(%s, %s, "%s");', $resolvedParams[0], $symbol, $evalContext) ); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
{@inheritdoc} @param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @throws CompilerException @throws Exception
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/EvalOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/EvalOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (isset($expression['parameters']) && count($expression['parameters']) > 1) { return false; } $context->headersManager->add('kernel/exit'); if (isset($expression['parameters'])) { // TODO: protect resolvedParams[0] from restore } if (!isset($expression['parameters'])) { // $context->codePrinter->output('ZEPHIR_MM_RESTORE();'); $context->codePrinter->output('zephir_exit_empty();'); } else { $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); // $context->codePrinter->output('ZEPHIR_MM_RESTORE();'); $context->codePrinter->output('zephir_exit(' . $resolvedParams[0] . ');'); } return new CompiledExpression('void ', '', $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/ExitOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/ExitOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if (count($expression['parameters']) < 2) { throw new CompilerException("'explode' require two parameter"); } /* * Process the expected symbol to be returned */ $call->processExpectedReturn($context); $symbolVariable = $call->getSymbolVariable(true, $context); $this->checkNotVariableString($symbolVariable, $expression); /** * Process limit. */ $limit = 'LONG_MAX'; $limitOffset = 2; if (3 == count($expression['parameters']) && 'int' == $expression['parameters'][2]['parameter']['type']) { $limit = $expression['parameters'][2]['parameter']['value'] . ' '; unset($expression['parameters'][2]); } if ('string' == $expression['parameters'][0]['parameter']['type']) { $str = Name::addSlashes($expression['parameters'][0]['parameter']['value']); unset($expression['parameters'][0]); if (2 == count($expression['parameters'])) { $limitOffset = 1; } } $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); if (isset($resolvedParams[$limitOffset])) { $context->headersManager->add('kernel/operators'); $limit = 'zephir_get_intval(' . $resolvedParams[$limitOffset] . ') '; } $context->headersManager->add('kernel/string'); $symbolVariable->setDynamicTypes('array'); $this->checkInitSymbolVariable($call, $symbolVariable, $context); $symbol = $context->backend->getVariableCode($symbolVariable); if (isset($str)) { $context->codePrinter->output( 'zephir_fast_explode_str(' . $symbol . ', SL("' . $str . '"), ' . $resolvedParams[0] . ', ' . $limit . ');' ); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); } $context->codePrinter->output( 'zephir_fast_explode(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ', ' . $limit . ');' ); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/ExplodeOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/ExplodeOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if (1 != count($expression['parameters'])) { return false; } $context->headersManager->add('kernel/file'); $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); return new CompiledExpression('bool', 'zephir_fclose(' . $resolvedParams[0] . ')', $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/FcloseOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/FcloseOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if (1 != count($expression['parameters'])) { return false; } $context->headersManager->add('kernel/file'); $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); return new CompiledExpression('bool', 'zephir_feof(' . $resolvedParams[0] . ')', $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/FeofOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/FeofOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if (1 != count($expression['parameters'])) { return false; } $context->headersManager->add('kernel/file'); $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); return new CompiledExpression( 'bool', '(zephir_file_exists(' . $resolvedParams[0] . ') == SUCCESS)', $expression ); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/FileExistsOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/FileExistsOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if (1 != count($expression['parameters'])) { return false; } $context->headersManager->add('kernel/file'); /* * Process the expected symbol to be returned */ $call->processExpectedReturn($context); $symbolVariable = $call->getSymbolVariable(true, $context); $this->checkNotVariableString($symbolVariable, $expression); $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); $this->checkInitSymbolVariable($call, $symbolVariable, $context); if ($symbolVariable) { $symbol = $context->backend->getVariableCode($symbolVariable); $context->codePrinter->output('zephir_file_get_contents(' . $symbol . ', ' . $resolvedParams[0] . ');'); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); } else { $context->codePrinter->output('zephir_file_get_contents(NULL, ' . $resolvedParams[0] . ');'); } return new CompiledExpression('null', 'null', $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/FileGetContentsOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/FileGetContentsOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if (2 != count($expression['parameters'])) { return false; } $context->headersManager->add('kernel/file'); /* * Process the expected symbol to be returned */ $call->processExpectedReturn($context); $symbolVariable = $call->getSymbolVariable(); $this->checkNotVariableString($symbolVariable, $expression); $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); if ($symbolVariable) { $symbol = $context->backend->getVariableCode($symbolVariable); if ($call->mustInitSymbolVariable()) { $symbolVariable->initVariant($context); } $context->codePrinter->output( 'zephir_file_put_contents(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ');' ); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); } else { $context->codePrinter->output( 'zephir_file_put_contents(NULL, ' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ');' ); } return new CompiledExpression('null', 'null', $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/FilePutContentsOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/FilePutContentsOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if (1 != count($expression['parameters'])) { return false; } $context->headersManager->add('kernel/math'); $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); return new CompiledExpression('double', 'zephir_floor(' . $resolvedParams[0] . ')', $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/FloorOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/FloorOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters']) || 1 != count($expression['parameters'])) { throw new CompilerException( sprintf( 'func_get_arg() expects at exactly 1 parameter, %d given', isset($expression['parameters']) ? count($expression['parameters']) : 0 ), $expression ); } /* * Process the expected symbol to be returned */ $call->processExpectedReturn($context); $symbolVariable = $call->getSymbolVariable(true, $context); $this->checkNotVariableString($symbolVariable, $expression); $this->checkInitSymbolVariable($call, $symbolVariable, $context); $symbol = $context->backend->getVariableCode($symbolVariable); $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); // zephir_get_intval $context->headersManager->add('kernel/operators'); // zephir_get_arg $context->headersManager->add('kernel/main'); $context->codePrinter->output( sprintf('zephir_get_arg(%s, zephir_get_intval(%s));', $symbol, $resolvedParams[0]) ); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
{@inheritdoc} @param array $expression @param Call $call @param CompilationContext $context @return CompiledExpression @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/FuncGetArgOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/FuncGetArgOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { /* * Process the expected symbol to be returned */ $call->processExpectedReturn($context); $symbolVariable = $call->getSymbolVariable(true, $context); $this->checkNotVariableString($symbolVariable, $expression); $this->checkInitSymbolVariable($call, $symbolVariable, $context); $symbol = $context->backend->getVariableCode($symbolVariable); $context->headersManager->add('kernel/main'); $context->codePrinter->output('zephir_get_args(' . $symbol . ');'); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/FuncGetArgsOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/FuncGetArgsOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { return new CompiledExpression('int', 'ZEND_NUM_ARGS()', $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/FuncNumArgsOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/FuncNumArgsOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters']) || 1 !== count($expression['parameters'])) { return false; } if ('string' === $expression['parameters'][0]['parameter']['type']) { $str = Name::addSlashes($expression['parameters'][0]['parameter']['value']); unset($expression['parameters'][0]); } $context->headersManager->add('kernel/object'); $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); if (isset($str)) { return new CompiledExpression( 'bool', '(zephir_function_exists_ex(ZEND_STRL("' . strtolower($str) . '")) == SUCCESS)', $expression ); } return new CompiledExpression( 'bool', '(zephir_function_exists(' . $resolvedParams[0] . ') == SUCCESS)', $expression ); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/FunctionExistsOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/FunctionExistsOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if (2 != count($expression['parameters'])) { return false; } $context->headersManager->add('kernel/file'); /* * Process the expected symbol to be returned */ $call->processExpectedReturn($context); $symbolVariable = $call->getSymbolVariable(); $this->checkNotVariableString($symbolVariable, $expression); $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); if ($symbolVariable) { if ($call->mustInitSymbolVariable()) { $symbolVariable->initVariant($context); } $symbol = $context->backend->getVariableCode($symbolVariable); $context->codePrinter->output( 'zephir_fwrite(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ');' ); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); } else { $context->codePrinter->output( 'zephir_fwrite(NULL, ' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ');' ); } return new CompiledExpression('null', 'null', $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/FwriteOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/FwriteOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (isset($expression['parameters'])) { $numberParameters = count($expression['parameters']); if (0 != $numberParameters) { return false; } } /* * Process the expected symbol to be returned */ $call->processExpectedReturn($context); $symbolVariable = $call->getSymbolVariable(true, $context); $this->checkNotVariableString($symbolVariable, $expression); $this->checkInitSymbolVariable($call, $symbolVariable, $context); $context->headersManager->add('kernel/object'); $symbolVariable->setDynamicTypes('string'); $symbol = $context->backend->getVariableCode($symbolVariable); $context->codePrinter->output('zephir_get_called_class(' . $symbol . ');'); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/GetCalledClassOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/GetCalledClassOptimizer.php
MIT
protected function getOutput( CompilationContext $context, string $symbol, array $resolvedParams ): void { $output = 'zephir_get_class_ns(' . $symbol . ', ' . $resolvedParams[0]; if (!isset($resolvedParams[1])) { $output .= ', 0);'; } else { $output .= ', ' . $resolvedParams[1] . ');'; } $context->codePrinter->output($output); }
@param CompilationContext $context @param string $symbol @param array $resolvedParams @return void
getOutput
php
zephir-lang/zephir
src/Optimizers/FunctionCall/GetClassNsOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/GetClassNsOptimizer.php
MIT
protected function getOutput( CompilationContext $context, string $symbol, array $resolvedParams ): void { $lower = $this->lower ? 1 : 0; $context->codePrinter->output( 'zephir_get_class(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $lower . ');' ); }
@param CompilationContext $context @param string $symbol @param array $resolvedParams @return void
getOutput
php
zephir-lang/zephir
src/Optimizers/FunctionCall/GetClassOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/GetClassOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { $call->processExpectedReturn($context); $symbolVariable = $call->getSymbolVariable(true, $context); $this->checkNotVariableString($symbolVariable, $expression); $this->checkInitSymbolVariable($call, $symbolVariable, $context); $symbol = $context->backend->getVariableCode($symbolVariable); $context->headersManager->add('kernel/variables'); $context->codePrinter->output('zephir_get_defined_vars(' . $symbol . ');'); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/GetDefinedVarsOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/GetDefinedVarsOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if (1 != count($expression['parameters'])) { throw new CompilerException("'get_class_ns' only accepts one parameter"); } /* * Process the expected symbol to be returned */ $call->processExpectedReturn($context); $symbolVariable = $call->getSymbolVariable(true, $context); $this->checkNotVariableString($symbolVariable, $expression); $context->headersManager->add('kernel/object'); $symbolVariable->setDynamicTypes('string'); $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); $this->checkInitSymbolVariable($call, $symbolVariable, $context); $symbol = $context->backend->getVariableCode($symbolVariable); $context->codePrinter->output('zephir_get_ns_class(' . $symbol . ', ' . $resolvedParams[0] . ', 0);'); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/GetNsClassOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/GetNsClassOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters'])) { return false; } if (1 != count($expression['parameters'])) { return false; } /* * Process the expected symbol to be returned */ $call->processExpectedReturn($context); $symbolVariable = $call->getSymbolVariable(true, $context); $this->checkNotVariableString($symbolVariable, $expression); $symbolVariable->setDynamicTypes('string'); $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); $this->checkInitSymbolVariable($call, $symbolVariable, $context); $symbol = $context->backend->getVariableCode($symbolVariable); $context->codePrinter->output('zephir_gettype(' . $symbol . ', ' . $resolvedParams[0] . ');'); return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/GettypeOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/GettypeOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context): ?CompiledExpression { if (!isset($expression['parameters'])) { return null; } if (1 !== count($expression['parameters'])) { throw new CompilerException("'globals_get' only accepts one parameter", $expression); } if ('string' !== $expression['parameters'][0]['parameter']['type']) { throw new CompilerException("A string parameter is required for 'globals_get'", $expression); } $globalName = $expression['parameters'][0]['parameter']['value']; if (!$context->compiler->isExtensionGlobal($globalName)) { throw new CompilerException( "Global '" . $globalName . "' cannot be read because it isn't defined", $expression ); } $type = $context->compiler->getExtensionGlobal($globalName)['type']; if (str_contains($globalName, '.')) { $parts = explode('.', $globalName); return new CompiledExpression($type, 'ZEPHIR_GLOBAL(' . $parts[0] . ').' . $parts[1], $expression); } return new CompiledExpression($type, 'ZEPHIR_GLOBAL(' . $globalName . ')', $expression); }
@param array $expression @param Call $call @param CompilationContext $context @return CompiledExpression|null @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/GlobalsGetOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/GlobalsGetOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context): CompiledExpression { if (!isset($expression['parameters'])) { throw new CompilerException("'globals_set' requires two parameters", $expression); } if (2 !== count($expression['parameters'])) { throw new CompilerException("'globals_set' only accepts two parameters", $expression); } if ('string' !== $expression['parameters'][0]['parameter']['type']) { throw new CompilerException("A string parameter is required for 'globals_set'", $expression); } $globalName = $expression['parameters'][0]['parameter']['value']; if (!$context->compiler->isExtensionGlobal($globalName)) { throw new CompilerException( "Global variable '{$globalName}' cannot be written because it wasn't defined", $expression ); } unset($expression['parameters'][0]); try { $globalDefinition = $context->compiler->getExtensionGlobal($globalName); $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); if (!isset($resolvedParams[0]) || empty($resolvedParams[0]) || !is_string($resolvedParams[0])) { throw new CompilerException( "Unable to reslove value for '{$globalName}' global variable.", $expression ); } if (!isset($globalDefinition['type'])) { throw new CompilerException( "Unable to determine type for '{$globalName}' global variable.", $expression ); } $internalAccessor = $this->resolveInternalAccessor($globalName); $internalValue = $this->resolveInternalValue( $globalDefinition, $expression, $globalName, $resolvedParams[0] ); $context->codePrinter->output("{$internalAccessor} = {$internalValue};"); return new CompiledExpression('null', null, $expression); } catch (Exception $e) { throw new CompilerException($e->getMessage(), $expression); } }
@param array $expression @param Call $call @param CompilationContext $context @return CompiledExpression @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/GlobalsSetOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/GlobalsSetOptimizer.php
MIT
private function resolveInternalValue(array $definition, array $expression, string $name, string $value): string { $type = $definition['type']; return match ($type) { Types::T_BOOLEAN, Types::T_BOOL => strtr('zend_is_true(:v)', [':v' => $value]), Types::T_INT, Types::T_UINT, Types::T_INTEGER, Types::T_LONG, Types::T_ULONG => strtr('zval_get_long(:v)', [':v' => $value]), Types::T_STRING => strtr('ZSTR_VAL(zval_get_string(:v))', [':v' => $value]), Types::T_CHAR, Types::T_UCHAR => strtr( '(Z_TYPE_P(:v) == IS_STRING ? (Z_STRLEN_P(:v) ? Z_STRVAL_P(:v)[0] : NULL) : zval_get_long(:v))', [':v' => $value] ), Types::T_DOUBLE, Types::T_FLOAT => strtr('zval_get_double(:v)', [':v' => $value]), default => throw new CompilerException( "Unknown type '$type' to setting global variable '$name'.", $expression ), }; }
TODO: Add 'hash' support @param array $definition @param array $expression @param string $name @param string $value @return string
resolveInternalValue
php
zephir-lang/zephir
src/Optimizers/FunctionCall/GlobalsSetOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/GlobalsSetOptimizer.php
MIT
public function optimize(array $expression, Call $call, CompilationContext $context) { if (!isset($expression['parameters']) || 2 != count($expression['parameters'])) { throw new CompilerException("'hash_equals' requires two parameters", $expression); } /* * Process the expected symbol to be returned */ $call->processExpectedReturn($context); $symbolVariable = $call->getSymbolVariable(true, $context); $this->checkNotVariableString($symbolVariable, $expression); $context->headersManager->add('kernel/string'); /** * Process parameters. */ $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression); $this->checkInitSymbolVariable($call, $symbolVariable, $context); return new CompiledExpression( 'bool', 'zephir_hash_equals(' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ')', $expression ); }
@param array $expression @param Call $call @param CompilationContext $context @return bool|CompiledExpression|mixed @throws CompilerException
optimize
php
zephir-lang/zephir
src/Optimizers/FunctionCall/HashEqualsOptimizer.php
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/HashEqualsOptimizer.php
MIT