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 optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters'])) {
return false;
}
if (2 != count($expression['parameters'])) {
return false;
}
/*
* Process the expected symbol to be returned
*/
$call->processExpectedReturn($context);
$symbolVariable = $call->getSymbolVariable(true, $context);
$this->checkNotVariableString($symbolVariable, $expression);
if ('string' == $expression['parameters'][0]['parameter']['type']) {
$str = Name::addSlashes($expression['parameters'][0]['parameter']['value']);
unset($expression['parameters'][0]);
}
$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);
$context->headersManager->add('kernel/string');
$symbolVariable->setDynamicTypes('string');
$this->checkInitSymbolVariable($call, $symbolVariable, $context);
$symbol = $context->backend->getVariableCode($symbolVariable);
if (isset($str)) {
$context->codePrinter->output(
'zephir_fast_join_str(' . $symbol . ', SL("' . $str . '"), ' . $resolvedParams[0] . ');'
);
return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression);
}
$context->codePrinter->output(
'zephir_fast_join(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ');'
);
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/ImplodeOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/ImplodeOptimizer.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");
}
/**
* Process autoload.
*/
$autoload = '1 ';
if (
2 == count($expression['parameters']) &&
(
'int' == $expression['parameters'][1]['parameter']['type'] ||
'bool' == $expression['parameters'][1]['parameter']['type']
)
) {
$autoload = $expression['parameters'][1]['parameter']['value'] . ' ';
unset($expression['parameters'][1]);
}
$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);
if (2 == count($resolvedParams)) {
$context->headersManager->add('kernel/operators');
$autoload = 'zephir_is_true(' . $resolvedParams[1] . ') ';
}
$context->headersManager->add('kernel/object');
return new CompiledExpression(
'bool',
'zephir_interface_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/InterfaceExistsOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/InterfaceExistsOptimizer.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('long', 'zephir_get_intval(' . $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/IntvalOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/IntvalOptimizer.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('bool', 'zephir_is_callable(' . $resolvedParams[0] . ')', $expression);
}
|
@param array $expression
@param Call $call
@param CompilationContext $context
@return bool|CompiledExpression|mixed
|
optimize
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/IsCallableOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/IsCallableOptimizer.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('bool', 'zephir_is_numeric(' . $resolvedParams[0] . ')', $expression);
}
|
@param array $expression
@param Call $call
@param CompilationContext $context
@return bool|CompiledExpression|mixed
|
optimize
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/IsNumericOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/IsNumericOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters'])) {
throw new CompilerException('This function requires parameters', $expression);
}
if (1 != count($expression['parameters'])) {
throw new CompilerException('This function only requires one parameter', $expression);
}
$variableType = $expression['parameters'][0]['parameter']['type'];
if (!isset($this->allowedTypes[$variableType])) {
throw new CompilerException(
"This function requires a scalar types parameter, $variableType given",
$expression
);
}
preg_match(
'/^(?<major>\d+)(?:\.(?<minor>!?\d+))?(?:\.(?<patch>!?\d+))?(?:[^Ee0-9.]+.*)?$/',
$expression['parameters'][0]['parameter']['value'],
$matches
);
if (!count($matches)) {
throw new CompilerException('Could not parse PHP version', $expression);
}
$minorVersion = 0;
$releaseVersion = 0;
$majorVersion = $matches['major'] * 10000;
if (isset($matches['minor'])) {
$minorVersion = $matches['minor'] * 100;
}
if (isset($matches['patch'])) {
$releaseVersion = $matches['patch'];
}
$versionId = (int)($majorVersion + $minorVersion + $releaseVersion);
return new CompiledExpression('bool', 'zephir_is_php_version(' . $versionId . ')', $expression);
}
|
@param array $expression
@param Call $call
@param CompilationContext $context
@return CompiledExpression
|
optimize
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/IsPhpVersionOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/IsPhpVersionOptimizer.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/object');
try {
$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);
return new CompiledExpression('bool', 'zephir_is_private_prop(' . $resolvedParams[0] . ')', $expression);
} catch (Exception $e) {
throw new CompilerException($e->getMessage(), $expression);
}
}
|
@param array $expression
@param Call $call
@param CompilationContext $context
@return bool|CompiledExpression
@throws CompilerException
|
optimize
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/IsPrivatePropertyOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/IsPrivatePropertyOptimizer.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('bool', 'zephir_is_scalar(' . $resolvedParams[0] . ')', $expression);
}
|
@param array $expression
@param Call $call
@param CompilationContext $context
@return bool|CompiledExpression|mixed
|
optimize
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/IsScalarOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/IsScalarOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters'])) {
return false;
}
/*
* Process the expected symbol to be returned
*/
$call->processExpectedReturn($context);
$symbolVariable = $call->getSymbolVariable();
$this->checkNotVariable($symbolVariable, $expression);
if (!$symbolVariable) {
$symbolVariable = $context->symbolTable->addTemp('variable', $context);
$symbolVariable->initVariant($context);
}
$context->headersManager->add('kernel/string');
$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);
/*
* Process encode options
*/
if (count($resolvedParams) >= 2) {
$context->headersManager->add('kernel/operators');
$options = 'zephir_get_intval(' . $resolvedParams[1] . ') ';
} else {
$options = '0 ';
}
$this->checkInitSymbolVariable($call, $symbolVariable, $context);
$symbol = $context->backend->getVariableCode($symbolVariable);
$context->codePrinter->output(
'zephir_json_decode(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $options . ');'
);
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/JsonDecodeOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/JsonDecodeOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($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/string');
$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);
/*
* Process encode options
*/
if (count($resolvedParams) >= 2) {
$context->headersManager->add('kernel/operators');
$options = 'zephir_get_intval(' . $resolvedParams[1] . ') ';
} else {
$options = '0 ';
}
$this->checkInitSymbolVariable($call, $symbolVariable, $context);
$symbol = $context->backend->getVariableCode($symbolVariable);
$context->codePrinter->output(
'zephir_json_encode(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $options . ');'
);
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/JsonEncodeOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/JsonEncodeOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters']) || 2 != count($expression['parameters'])) {
throw new CompilerException(
sprintf(
"'%s' requires two parameters",
$this->zephirMethod
),
$expression
);
}
$resolvedParams = $call->getReadOnlyResolvedParams(
$expression['parameters'],
$context,
$expression
);
$context->headersManager->add('kernel/math');
return new CompiledExpression(
'double',
'zephir_' . $this->zephirMethod . '(' . $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/LdexpOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/LdexpOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters'])) {
return false;
}
if (2 != count($expression['parameters'])) {
return false;
}
if ('string' == $expression['parameters'][1]['parameter']['type']) {
$str = Name::addSlashes($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',
'zephir_memnstr_str(' . $resolvedParams[0] . ', SL("' . $str . '"), "' . Compiler::getShortUserPath(
$expression['file']
) . '", ' . $expression['line'] . ')',
$expression
);
}
return new CompiledExpression(
'bool',
'zephir_memnstr(' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ', "' . Compiler::getShortUserPath(
$expression['file']
) . '", ' . $expression['line'] . ')',
$expression
);
}
|
@param array $expression
@param Call $call
@param CompilationContext $context
@return bool|CompiledExpression|mixed
|
optimize
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/MemstrOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/MemstrOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters'])) {
return false;
}
if (2 != count($expression['parameters'])) {
return false;
}
/*
* Process the expected symbol to be returned
*/
$call->processExpectedReturn($context);
$context->headersManager->add('kernel/array');
$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);
$context->codePrinter->output('zephir_merge_append(' . $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/MergeAppendOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/MergeAppendOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters'])) {
return false;
}
if (2 != count($expression['parameters'])) {
return false;
}
if ('string' == $expression['parameters'][1]['parameter']['type']) {
$str = Name::addSlashes($expression['parameters'][1]['parameter']['value']);
unset($expression['parameters'][1]);
}
$context->headersManager->add('kernel/object');
$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);
if (isset($str)) {
return new CompiledExpression(
'bool',
'(zephir_method_exists_ex(' . $resolvedParams[0] . ', ZEND_STRL("' . strtolower(
$str
) . '")) == SUCCESS)',
$expression
);
}
return new CompiledExpression(
'bool',
'(zephir_method_exists(' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ') == SUCCESS)',
$expression
);
}
|
@param array $expression
@param Call $call
@param CompilationContext $context
@return bool|CompiledExpression|mixed
|
optimize
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/MethodExistsOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/MethodExistsOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
/* microtime has one optional parameter (get_as_float) */
if (isset($expression['parameters']) && count($expression['parameters']) > 2) {
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/time');
$symbol = $context->backend->getVariableCode($symbolVariable);
if (!isset($expression['parameters'])) {
$symbolVariable->setDynamicTypes('string');
if ($call->mustInitSymbolVariable()) {
$symbolVariable->initVariant($context);
}
$context->codePrinter->output('zephir_microtime(' . $symbol . ', NULL);');
} else {
$symbolVariable->setDynamicTypes('double');
$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);
if ($call->mustInitSymbolVariable()) {
$symbolVariable->initVariant($context);
}
$context->codePrinter->output('zephir_microtime(' . $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/MicrotimeOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/MicrotimeOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters'])) {
return false;
}
if (2 != count($expression['parameters'])) {
return false;
}
}
|
@param array $expression
@param Call $call
@param CompilationContext $context
@return bool|CompiledExpression|mixed
@throws CompilerException
|
optimize
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/PowOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/PowOptimizer.php
|
MIT
|
protected function setHeaders(CompilationContext $context, ?Variable $symbolVariable): void
{
$context->headersManager->add('kernel/operators');
$symbolVariable->setDynamicTypes('variable');
}
|
@param CompilationContext $context
@param Variable|null $symbolVariable
@return void
|
setHeaders
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/PowOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/PowOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters']) || count($expression['parameters']) < 2) {
throw new CompilerException(
sprintf(
'preg_match() expects at least 2 parameters, %d given',
isset($expression['parameters']) ? count($expression['parameters']) : 0
),
$expression
);
}
if (count($expression['parameters']) > 5) {
throw new CompilerException(
sprintf(
'preg_match() expects at most 5 parameters, %d given',
count($expression['parameters'])
),
$expression
);
}
$matches = $this->createMatches($expression, $context);
[$flags, $offset] = $this->processOptionals($expression, $call, $context);
/*
* Process the expected symbol to be returned
*/
$call->processExpectedReturn($context);
$symbolVariable = $call->getSymbolVariable(true, $context);
$this->checkNotVariable($symbolVariable, $expression);
// zephir_preg_match
$context->headersManager->add('kernel/string');
try {
$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);
if ($call->mustInitSymbolVariable()) {
$symbolVariable->initVariant($context);
}
$symbol = $context->backend->getVariableCode($symbolVariable);
$matchesSymbol = $context->backend->getVariableCode($matches);
$context->codePrinter->output(
sprintf(
'zephir_preg_match(%s, %s, %s, %s, %d, %s, %s);',
$symbol,
$resolvedParams[0],
$resolvedParams[1],
$matchesSymbol,
static::GLOBAL_MATCH,
$flags,
$offset
)
);
return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression);
} catch (Exception $e) {
throw new CompilerException($e->getMessage(), $expression, $e->getCode(), $e);
}
}
|
{@inheritdoc}
@param array $expression
@param Call $call
@param CompilationContext $context
@return CompiledExpression
@throws CompilerException
|
optimize
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/PregMatchOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/PregMatchOptimizer.php
|
MIT
|
private function createMatches(array $expression, CompilationContext $context)
{
if (isset($expression['parameters'][2])) {
$type = $expression['parameters'][2]['parameter']['type'];
if ('variable' !== $type) {
throw new CompilerException('Only variables can be passed by reference', $expression);
}
$name = $expression['parameters'][2]['parameter']['value'];
if (!$variable = $context->symbolTable->getVariable($name)) {
throw new CompilerException(
sprintf("Cannot mutate variable '%s' because it wasn't defined", $name),
$expression
);
}
if (!in_array($variable->getType(), ['variable', 'array'], true)) {
throw new CompilerException(
sprintf(
"The '%s' variable must be either a variable or an array, got %s",
$name,
$variable->getType()
),
$expression
);
}
if (false == $variable->isInitialized()) {
$variable->initVariant($context);
$variable->setIsInitialized(true, $context);
}
} else {
$variable = $context->symbolTable->addTemp('variable', $context);
$variable->initVariant($context);
}
$variable->setDynamicTypes('array');
return $variable;
}
|
Process the matches result.
preg_match(pattern, subject, matches)
@param array $expression
@param CompilationContext $context
@return Variable
|
createMatches
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/PregMatchOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/PregMatchOptimizer.php
|
MIT
|
private function processOptionals(array &$expression, Call $call, CompilationContext $context)
{
$flags = null;
$offset = null;
$offsetParamOffset = 4;
if (isset($expression['parameters'][4]) && 'int' === $expression['parameters'][4]['parameter']['type']) {
$offset = $expression['parameters'][4]['parameter']['value'] . ' ';
unset($expression['parameters'][4]);
}
if (isset($expression['parameters'][3]) && 'int' === $expression['parameters'][3]['parameter']['type']) {
$flags = $expression['parameters'][3]['parameter']['value'] . ' ';
$offsetParamOffset = 3;
unset($expression['parameters'][3]);
}
try {
$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);
if (null === $offset && isset($resolvedParams[$offsetParamOffset])) {
$context->headersManager->add('kernel/operators');
$offset = 'zephir_get_intval(' . $resolvedParams[$offsetParamOffset] . ') ';
}
if (null === $flags && isset($resolvedParams[3])) {
$context->headersManager->add('kernel/operators');
$flags = 'zephir_get_intval(' . $resolvedParams[3] . ') ';
}
} catch (Exception $e) {
throw new CompilerException($e->getMessage(), $expression, $e->getCode(), $e);
}
if (null === $flags) {
$flags = '0 ';
}
if (null === $offset) {
$offset = '0 ';
}
return [$flags, $offset];
}
|
Process optional parameters.
preg_match(pattern, subject, matches, flags, offset)
@param array $expression
@param Call $call
@param CompilationContext $context
@return array
@throws CompilerException
|
processOptionals
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/PregMatchOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/PregMatchOptimizer.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(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(
'zephir_prepare_virtual_path(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ');'
);
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/PrepareVirtualPathOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/PrepareVirtualPathOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters'])) {
return false;
}
if (count($expression['parameters']) > 4) {
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/math');
$symbolVariable->setDynamicTypes('double');
$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);
$this->checkInitSymbolVariable($call, $symbolVariable, $context);
/* Todo: move var return type -> double as with round, floor */
$symbol = $context->backend->getVariableCode($symbolVariable);
switch (count($expression['parameters'])) {
/*
* Only float $val
*/
case 1:
$context->codePrinter->output(
'zephir_round(' . $symbol . ', ' . $resolvedParams[0] . ', NULL, NULL);'
);
break;
/*
* float $val, int $mode
*/
case 2:
$context->codePrinter->output(
'zephir_round(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ', NULL);'
);
break;
default:
$context->codePrinter->output(
'zephir_round(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ', ' . $resolvedParams[2] . ');'
);
break;
}
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/RoundOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/RoundOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters'])) {
return false;
}
if (count($expression['parameters']) > 1) {
return false;
}
/*
* Process the expected symbol to be returned
*/
$call->processExpectedReturn($context);
$symbolVariable = $call->getSymbolVariable();
$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);
$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
@throws CompilerException
|
optimize
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/StripslashesOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/StripslashesOptimizer.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/string');
$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);
return new CompiledExpression('int', 'zephir_fast_strlen_ev(' . $resolvedParams[0] . ')', $expression);
}
|
@param array $expression
@param Call $call
@param CompilationContext $context
@return bool|CompiledExpression|mixed
|
optimize
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/StrlenOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/StrlenOptimizer.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("'strpos' require two or three parameters");
}
/**
* Process offset.
*/
$offset = '0 ';
if (count($expression['parameters']) >= 3 && 'int' == $expression['parameters'][2]['parameter']['type']) {
$offset = $expression['parameters'][2]['parameter']['value'] . ' ';
unset($expression['parameters'][2]);
}
$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);
if (count($resolvedParams) >= 3) {
$context->headersManager->add('kernel/operators');
$offset = 'zephir_get_intval(' . $resolvedParams[2] . ') ';
}
/*
* 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('int');
$this->checkInitSymbolVariable($call, $symbolVariable, $context);
$symbol = $context->backend->getVariableCode($symbolVariable);
$context->codePrinter->output(
'zephir_fast_strpos(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ', ' . $offset . ');'
);
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/StrposOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/StrposOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters'])) {
return false;
}
if (3 != count($expression['parameters'])) {
if (4 == count($expression['parameters'])) {
return false;
}
throw new CompilerException("'str_replace' only accepts three parameter", $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');
$symbolVariable->setDynamicTypes('string');
$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);
if ($call->mustInitSymbolVariable()) {
if ('return_value' == $symbolVariable->getName()) {
$symbolVariable = $context->symbolTable->getTempVariableForWrite('variable', $context);
} else {
$symbolVariable->initVariant($context);
}
}
$symbol = $context->backend->getVariableCode($symbolVariable);
$context->codePrinter->output(
'zephir_fast_str_replace(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ', ' . $resolvedParams[2] . ');'
);
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/StrReplaceOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/StrReplaceOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters'])) {
return false;
}
if (1 != count($expression['parameters'])) {
return false;
}
if (1 == count($expression['parameters'])) {
return new CompiledExpression('bool', 'false', $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/FunctionCall/StrtokOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/StrtokOptimizer.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);
$context->headersManager->add('kernel/string');
$symbolVariable->setDynamicTypes($this->type);
$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
@throws CompilerException
|
optimize
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/StrtoupperOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/StrtoupperOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters'])) {
return false;
}
if (count($expression['parameters']) < 2 || count($expression['parameters']) > 3) {
throw new CompilerException("'substr' require two or three parameters");
}
/**
* Process parameters.
*/
$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);
$params = [];
$flags = '0';
for ($param = 1; $param <= 2; ++$param) {
if (!isset($expression['parameters'][$param])) {
$params[] = '0';
if (2 == $param) {
$flags = 'ZEPHIR_SUBSTR_NO_LENGTH';
}
continue;
}
if ('int' == $expression['parameters'][$param]['parameter']['type']) {
$params[] = $expression['parameters'][$param]['parameter']['value'] . ' ';
} else {
$context->headersManager->add('kernel/operators');
$params[] = 'zephir_get_intval(' . $resolvedParams[$param] . ')';
}
}
/*
* 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');
$this->checkInitSymbolVariable($call, $symbolVariable, $context);
$symbol = $context->backend->getVariableCode($symbolVariable);
$context->codePrinter->output(
'zephir_substr(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $params[0] . ', ' . $params[1] . ', ' . $flags . ');'
);
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/SubstrOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/SubstrOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (isset($expression['parameters'])) {
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/time');
$symbolVariable->setDynamicTypes('long');
$this->checkInitSymbolVariable($call, $symbolVariable, $context);
$symbol = $context->backend->getVariableCode($symbolVariable);
$context->codePrinter->output('zephir_time(' . $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/TimeOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/TimeOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters'])) {
return false;
}
if (1 != count($expression['parameters'])) {
return false;
}
[$symbolVariable, $resolvedParams, $symbol] = $this->processStringOptimizer(
$call,
$context,
$expression
);
$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 false|CompiledExpression
@throws Exception
|
optimize
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/UcfirstOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/UcfirstOptimizer.php
|
MIT
|
protected function symbolVariablePost(
Call $call,
?Variable $symbolVariable,
CompilationContext $context
): void {
$this->checkInitSymbolVariable($call, $symbolVariable, $context);
}
|
@param Call $call
@param Variable|null $symbolVariable
@param CompilationContext $context
@return void
|
symbolVariablePost
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/UncamelizeOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/UncamelizeOptimizer.php
|
MIT
|
protected function symbolVariablePre(
Call $call,
?Variable $symbolVariable,
CompilationContext $context
): void {
// empty
}
|
@param Call $call
@param Variable|null $symbolVariable
@param CompilationContext $context
@return void
|
symbolVariablePre
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/UncamelizeOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/UncamelizeOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters'])) {
return false;
}
if (2 != count($expression['parameters'])) {
throw new CompilerException("'unique_key' only accepts two parameter", $expression);
}
return $this->processOptimizer($call, $context, $expression);
}
|
@param array $expression
@param Call $call
@param CompilationContext $context
@return bool|CompiledExpression|mixed
@throws CompilerException
|
optimize
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/UniqueKeyOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/UniqueKeyOptimizer.php
|
MIT
|
protected function processOptimizer(Call $call, CompilationContext $context, array $expression): CompiledExpression
{
/**
* Process the expected symbol to be returned
*/
$call->processExpectedReturn($context);
$symbolVariable = $call->getSymbolVariable(true, $context);
$this->checkNotVariableString($symbolVariable, $expression);
$this->setHeaders($context, $symbolVariable);
$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] . ', ' . $resolvedParams[1] . ');'
);
return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression);
}
|
@param Call $call
@param CompilationContext $context
@param array $expression
@return CompiledExpression
@throws Exception
|
processOptimizer
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/UniqueKeyOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/UniqueKeyOptimizer.php
|
MIT
|
protected function setHeaders(CompilationContext $context, ?Variable $symbolVariable): void
{
$context->headersManager->add('kernel/string');
$symbolVariable->setDynamicTypes('string');
}
|
@param CompilationContext $context
@param Variable|null $symbolVariable
@return void
|
setHeaders
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/UniqueKeyOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/UniqueKeyOptimizer.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("'unique_path_key' only accepts one parameter", $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);
$context->headersManager->add('kernel/file');
$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);
$symbol = $context->backend->getVariableCode($symbolVariable);
$context->codePrinter->output('zephir_unique_path_key(' . $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/UniquePathKeyOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/UniquePathKeyOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters'])) {
return false;
}
$context->headersManager->add('kernel/variables');
$resolvedParams = $call->getResolvedParamsAsExpr($expression['parameters'], $context, $expression);
foreach ($resolvedParams as $resolvedParam) {
$variable = $context->symbolTable->getVariable($resolvedParam->getCode());
if (!$variable || !$variable->isVariable()) {
/*
* Complex expressions require a temporary variable
*/
$type = match ($resolvedParam->getType()) {
Types::T_ARRAY => 'array',
default => 'variable',
};
$variable = $context->symbolTable->addTemp($type, $context);
$variable->initVariant($context);
$statement = new LetStatement([
'type' => 'let',
'assignments' => [
[
'assign-type' => $type,
'variable' => $variable->getName(),
'operator' => 'assign',
'expr' => [
'type' => $resolvedParam->getType(),
'value' => $resolvedParam->getCode(),
'file' => $expression['file'],
'line' => $expression['line'],
'char' => $expression['char'],
],
'file' => $expression['file'],
'line' => $expression['line'],
'char' => $expression['char'],
],
],
]);
$statement->compile($context);
} else {
/**
* This mark the variable as used.
*/
$variable = $context->symbolTable->getVariableForRead($resolvedParam->getCode(), $context, $expression);
}
$symbol = $context->backend->getVariableCode($variable);
$context->codePrinter->output('zephir_var_dump(' . $symbol . ');');
}
return new CompiledExpression('null', 'null', $expression);
}
|
@param array $expression
@param Call $call
@param CompilationContext $context
@return bool|CompiledExpression|mixed
|
optimize
|
php
|
zephir-lang/zephir
|
src/Optimizers/FunctionCall/VarDumpOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/VarDumpOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters'])) {
return false;
}
/*
* Process the expected symbol to be returned
*/
$call->processExpectedReturn($context);
$symbolVariable = $call->getSymbolVariable();
$this->checkNotVariable($symbolVariable, $expression);
$context->headersManager->add('kernel/variables');
$resolvedParams = $call->getResolvedParamsAsExpr($expression['parameters'], $context, $expression);
$resolvedParam = $resolvedParams[0];
if (!$symbolVariable || !$symbolVariable->isVariable()) {
/*
* Complex expressions require a temporary variable
*/
$type = match ($resolvedParam->getType()) {
Types::T_ARRAY => 'array',
default => 'variable',
};
$variable = $context->symbolTable->addTemp($type, $context);
$variable->initVariant($context);
$statement = new LetStatement([
'type' => 'let',
'assignments' => [
[
'assign-type' => $type,
'variable' => $variable->getName(),
'operator' => 'assign',
'expr' => [
'type' => $resolvedParam->getType(),
'value' => $resolvedParam->getCode(),
'file' => $expression['file'],
'line' => $expression['line'],
'char' => $expression['char'],
],
'file' => $expression['file'],
'line' => $expression['line'],
'char' => $expression['char'],
],
],
]);
$statement->compile($context);
} else {
/**
* This mark the variable as used.
*/
$variable = $context->symbolTable->getVariableForRead($resolvedParam->getCode(), $context, $expression);
}
$variableSymbol = $context->backend->getVariableCode($variable);
/*
* let a = var_export(val);
*/
if ($symbolVariable) {
if ('return_value' == $symbolVariable->getName()) {
$symbolVariable = $context->symbolTable->getTempVariableForWrite('variable', $context);
} else {
$symbolVariable->initVariant($context);
}
$symbol = $context->backend->getVariableCode($symbolVariable);
$context->codePrinter->output('zephir_var_export_ex(' . $symbol . ', ' . $variableSymbol . ');');
return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression);
}
$context->codePrinter->output('zephir_var_export(' . $variableSymbol . ');');
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/VarExportOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/VarExportOptimizer.php
|
MIT
|
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters'])) {
return false;
}
if (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/string');
$symbolVariable->setDynamicTypes('string');
$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);
$this->checkInitSymbolVariable($call, $symbolVariable, $context);
$symbol = $context->backend->getVariableCode($symbolVariable);
$context->codePrinter->output('zephir_string_to_hex(' . $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/ZephirStringToHexOptimizer.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Optimizers/FunctionCall/ZephirStringToHexOptimizer.php
|
MIT
|
public function isAvailable(): bool
{
return $this->parser->isAvailable()
&& version_compare(self::MINIMUM_PARSER_VERSION, $this->parser->getVersion(), '<=');
}
|
Check if Zephir Parser available.
@return bool
|
isAvailable
|
php
|
zephir-lang/zephir
|
src/Parser/Manager.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Parser/Manager.php
|
MIT
|
public function getVersion(): ?string
{
return phpversion('zephir_parser') ?: null;
}
|
Gets Zephir Parser version.
@return string|null
|
getVersion
|
php
|
zephir-lang/zephir
|
src/Parser/Parser.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Parser/Parser.php
|
MIT
|
public function isAvailable(): bool
{
return function_exists('zephir_parse_file');
}
|
Check if Zephir Parser available.
@return bool
|
isAvailable
|
php
|
zephir-lang/zephir
|
src/Parser/Parser.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Parser/Parser.php
|
MIT
|
public function parse(string $filePath): array
{
if (!$this->isAvailable()) {
throw new IllegalStateException("Zephir Parser extension couldn't be loaded.");
}
if (!is_file($filePath) || !is_readable($filePath)) {
throw new InvalidArgumentException('Unable to locate source file to parse.');
}
$content = file_get_contents($filePath);
if (false === $content) {
throw new ParseException('Unable to read source file to parse.');
}
return zephir_parse_file($content, $filePath);
}
|
Parses a file and returning an intermediate representation (IR).
@param string $filePath Absolute path to the *.zep file
@return array
@throws InvalidArgumentException
@throws ParseException
@throws IllegalStateException
|
parse
|
php
|
zephir-lang/zephir
|
src/Parser/Parser.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Parser/Parser.php
|
MIT
|
public function getNumberOfFunctionCalls(string $funcName): int
{
return $this->functionCalls[$funcName] ?? 0;
}
|
Returns the number of calls a function had.
@param string $funcName
@return int
|
getNumberOfFunctionCalls
|
php
|
zephir-lang/zephir
|
src/Passes/CallGathererPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/CallGathererPass.php
|
MIT
|
public function getNumberOfMethodCalls(string $className, string $methodName): int
{
return $this->methodCalls[$className][$methodName] ?? 0;
}
|
Returns the number of calls a function had.
@param string $className
@param string $methodName
@return int
|
getNumberOfMethodCalls
|
php
|
zephir-lang/zephir
|
src/Passes/CallGathererPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/CallGathererPass.php
|
MIT
|
public function pass(StatementsBlock $block): void
{
$this->passStatementBlock($block->getStatements());
}
|
Do the compilation pass.
@param StatementsBlock $block
|
pass
|
php
|
zephir-lang/zephir
|
src/Passes/CallGathererPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/CallGathererPass.php
|
MIT
|
public function passLetStatement(array $statement): void
{
foreach ($statement['assignments'] as $assignment) {
if (isset($assignment['expr'])) {
$this->passExpression($assignment['expr']);
}
}
}
|
Pass let statements.
@param array $statement
|
passLetStatement
|
php
|
zephir-lang/zephir
|
src/Passes/CallGathererPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/CallGathererPass.php
|
MIT
|
public function passNew(array $expression): void
{
if (!$expression['dynamic']) {
$className = $this->compilationContext->getFullName($expression['class']);
if (!isset($this->methodCalls[$className]['__construct'])) {
$this->methodCalls[$className]['__construct'] = 1;
} else {
++$this->methodCalls[$className]['__construct'];
}
}
if (isset($expression['parameters'])) {
foreach ($expression['parameters'] as $parameter) {
$this->passExpression($parameter['parameter']);
}
}
}
|
Pass "new" expressions.
@param array $expression
|
passNew
|
php
|
zephir-lang/zephir
|
src/Passes/CallGathererPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/CallGathererPass.php
|
MIT
|
public function getLastCallLine(): int
{
return $this->lastCallLine;
}
|
Returns the line where the latest call in the method was made.
@return int
|
getLastCallLine
|
php
|
zephir-lang/zephir
|
src/Passes/LocalContextPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/LocalContextPass.php
|
MIT
|
public function getLastUnsetLine(): int
{
return $this->lastUnsetLine;
}
|
Returns the line where the latest unset operation was made.
@return int
|
getLastUnsetLine
|
php
|
zephir-lang/zephir
|
src/Passes/LocalContextPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/LocalContextPass.php
|
MIT
|
public function getNumberOfMutations(string $variable): int
{
return $this->mutations[$variable] ?? 0;
}
|
Returns the number of assignment instructions that mutated a variable.
@param string $variable
@return int
|
getNumberOfMutations
|
php
|
zephir-lang/zephir
|
src/Passes/LocalContextPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/LocalContextPass.php
|
MIT
|
public function increaseMutations(string $variable): void
{
if (isset($this->mutations[$variable])) {
++$this->mutations[$variable];
} else {
$this->mutations[$variable] = 1;
}
}
|
Increase the number of mutations a variable has inside a statement block.
@param string $variable
|
increaseMutations
|
php
|
zephir-lang/zephir
|
src/Passes/LocalContextPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/LocalContextPass.php
|
MIT
|
public function markLastUse(string $variable, array $node): void
{
if (isset($node['line'])) {
$this->uses[$variable] = $node['line'];
}
}
|
Marks the latest use/read of a variable.
@param string $variable
@param array $node
|
markLastUse
|
php
|
zephir-lang/zephir
|
src/Passes/LocalContextPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/LocalContextPass.php
|
MIT
|
public function markVariableNoLocal(string $variable): void
{
if (isset($this->variables[$variable])) {
$this->variables[$variable] = false;
}
}
|
Marks a variable to mandatory be stored in the heap.
@param string $variable
|
markVariableNoLocal
|
php
|
zephir-lang/zephir
|
src/Passes/LocalContextPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/LocalContextPass.php
|
MIT
|
public function pass(StatementsBlock $block): void
{
$this->passStatementBlock($block->getStatements());
}
|
Do the compilation pass.
@param StatementsBlock $block
|
pass
|
php
|
zephir-lang/zephir
|
src/Passes/LocalContextPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/LocalContextPass.php
|
MIT
|
public function shouldBeLocal(string $variable): bool
{
return !empty($this->variables[$variable]);
}
|
Asks the local context information whether a variable can be stored in the stack instead of the heap.
@param string $variable
@return bool
|
shouldBeLocal
|
php
|
zephir-lang/zephir
|
src/Passes/LocalContextPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/LocalContextPass.php
|
MIT
|
public function pass(StatementsBlock $block): void
{
$this->passStatementBlock($block->getStatements());
}
|
Do the compilation pass.
@param StatementsBlock $block
|
pass
|
php
|
zephir-lang/zephir
|
src/Passes/LoopBreakPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/LoopBreakPass.php
|
MIT
|
public function getNumberOfMutations(string $variable): int
{
return $this->mutations[$variable] ?? 0;
}
|
Returns the number of assignment instructions that mutated a variable.
@param string $variable
@return int
|
getNumberOfMutations
|
php
|
zephir-lang/zephir
|
src/Passes/MutateGathererPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/MutateGathererPass.php
|
MIT
|
public function increaseMutations(string $variable): static
{
if (isset($this->mutations[$variable])) {
++$this->mutations[$variable];
} else {
$this->mutations[$variable] = 1;
}
return $this;
}
|
Increase the number of mutations a variable has inside a statement block.
@param string $variable
@return MutateGathererPass
|
increaseMutations
|
php
|
zephir-lang/zephir
|
src/Passes/MutateGathererPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/MutateGathererPass.php
|
MIT
|
public function pass(StatementsBlock $block): void
{
$this->passStatementBlock($block->getStatements());
}
|
Do the compilation pass.
@param StatementsBlock $block
|
pass
|
php
|
zephir-lang/zephir
|
src/Passes/MutateGathererPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/MutateGathererPass.php
|
MIT
|
public function passArray(array $expression): void
{
foreach ($expression['left'] as $item) {
if ('variable' != $item['value']['type']) {
$this->passExpression($item['value']);
}
}
}
|
Pass array expressions.
@param array $expression
|
passArray
|
php
|
zephir-lang/zephir
|
src/Passes/MutateGathererPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/MutateGathererPass.php
|
MIT
|
public function passCall(array $expression): void
{
if (isset($expression['parameters'])) {
foreach ($expression['parameters'] as $parameter) {
if ('variable' == $parameter['parameter']['type']) {
$this->increaseMutations($parameter['parameter']['value']);
} else {
$this->passExpression($parameter['parameter']);
}
}
}
}
|
Pass call expressions.
@param array $expression
|
passCall
|
php
|
zephir-lang/zephir
|
src/Passes/MutateGathererPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/MutateGathererPass.php
|
MIT
|
public function passLetStatement(array $statement): void
{
foreach ($statement['assignments'] as $assignment) {
if (isset($assignment['expr'])) {
$this->passExpression($assignment['expr']);
}
$this->increaseMutations($assignment['variable']);
}
}
|
Pass let statements.
@param array $statement
|
passLetStatement
|
php
|
zephir-lang/zephir
|
src/Passes/MutateGathererPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/MutateGathererPass.php
|
MIT
|
public function passNew(array $expression): void
{
if (isset($expression['parameters'])) {
foreach ($expression['parameters'] as $parameter) {
if ('variable' != $parameter['parameter']['type']) {
$this->passExpression($parameter['parameter']);
}
}
}
}
|
Pass "new" expressions.
@param array $expression
|
passNew
|
php
|
zephir-lang/zephir
|
src/Passes/MutateGathererPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/MutateGathererPass.php
|
MIT
|
public function passStatementBlock(array $statements): void
{
foreach ($statements as $statement) {
switch ($statement['type']) {
case 'let':
$this->passLetStatement($statement);
break;
case 'echo':
if (isset($statement['expressions'])) {
foreach ($statement['expressions'] as $expr) {
$this->passExpression($expr);
}
}
break;
case 'declare':
$this->declareVariables($statement);
break;
case 'if':
if (isset($statement['expr'])) {
$this->passExpression($statement['expr']);
}
if (isset($statement['statements'])) {
$this->passStatementBlock($statement['statements']);
}
if (isset($statement['else_statements'])) {
$this->passStatementBlock($statement['else_statements']);
}
if (isset($statement['elseif_statements'])) {
$this->passStatementBlock($statement['elseif_statements']);
}
break;
case 'switch':
if (isset($statement['expr'])) {
$this->passExpression($statement['expr']);
}
if (isset($statement['clauses'])) {
foreach ($statement['clauses'] as $clause) {
if (isset($clause['expr'])) {
$this->passExpression($clause['expr']);
}
if (isset($clause['statements'])) {
$this->passStatementBlock($clause['statements']);
}
}
}
break;
case 'while':
case 'do-while':
if (isset($statement['expr'])) {
$this->passExpression($statement['expr']);
}
if (isset($statement['statements'])) {
$this->passStatementBlock($statement['statements']);
}
break;
case 'for':
if (isset($statement['expr'])) {
$this->passExpression($statement['expr']);
}
if (isset($statement['value'])) {
$this->increaseMutations($statement['value']);
}
if (isset($statement['key'])) {
$this->increaseMutations($statement['key']);
}
if (isset($statement['statements'])) {
$this->passStatementBlock($statement['statements']);
}
break;
case 'throw':
case 'return':
if (isset($statement['expr'])) {
$this->passExpression($statement['expr']);
}
break;
case 'loop':
if (isset($statement['statements'])) {
$this->passStatementBlock($statement['statements']);
}
break;
case 'try-catch':
if (isset($statement['statements'])) {
$this->passStatementBlock($statement['statements']);
}
if (isset($statement['catches'])) {
foreach ($statement['catches'] as $catch) {
if (isset($catch['variable'])) {
$this->increaseMutations($catch['variable']['value']);
}
if (isset($catch['statements'])) {
$this->passStatementBlock($catch['statements']);
}
}
}
break;
case 'fetch':
$this->passExpression($statement['expr']);
break;
case 'mcall':
case 'scall':
case 'fcall':
case 'require':
case 'require_once':
$this->passCall($statement['expr']);
break;
case 'unset':
if ('array-access' == $statement['expr']['type']) {
if ('variable' == $statement['expr']['left']['type']) {
$this->increaseMutations($statement['expr']['left']['value']);
}
} else {
if ('list' == $statement['expr']['type']) {
if ('array-access' == $statement['expr']['left']['type']) {
if ('variable' == $statement['expr']['left']['left']['type']) {
$this->increaseMutations($statement['expr']['left']['left']['value']);
}
}
}
}
break;
case 'break':
case 'continue':
case 'empty':
case 'cblock':
case 'comment':
break;
default:
echo 'MutateGathererPassStatement=', $statement['type'], PHP_EOL;
}
}
}
|
Pass statement block.
@param array $statements
|
passStatementBlock
|
php
|
zephir-lang/zephir
|
src/Passes/MutateGathererPass.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/MutateGathererPass.php
|
MIT
|
public function getVariables(): array
{
$variableStats = [];
foreach ($this->variablesToSkip as $variables) {
foreach ($variables as $variable => $one) {
if (!isset($variableStats[$variable])) {
$variableStats[$variable] = 1;
} else {
++$variableStats[$variable];
}
}
}
$variables = [];
$numberBranches = count($this->branches);
foreach ($variableStats as $variable => $number) {
if ($number == $numberBranches) {
$variables[] = $variable;
}
}
return $variables;
}
|
Returns a list of variables that are initialized in every analyzed branch.
|
getVariables
|
php
|
zephir-lang/zephir
|
src/Passes/SkipVariantInit.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/SkipVariantInit.php
|
MIT
|
public function passLetStatement(int $branchNumber, array $statement): void
{
$skipTypes = [
'variable',
'array-access',
'property-access',
'static-property-access',
'fcall',
'mcall',
'scall',
];
foreach ($statement['assignments'] as $assignment) {
if ($assignment['assign-type'] !== 'variable' || $assignment['operator'] !== 'assign') {
continue;
}
if (in_array($assignment['expr']['type'], $skipTypes, true)) {
continue;
}
$this->variablesToSkip[$branchNumber][$assignment['variable']] = 1;
}
}
|
Check assignment types for possible skip.
|
passLetStatement
|
php
|
zephir-lang/zephir
|
src/Passes/SkipVariantInit.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/SkipVariantInit.php
|
MIT
|
public function getInferedType($variable)
{
if (isset($this->variables[$variable])) {
$type = $this->variables[$variable];
if ('variable' != $type && 'undefined' != $type && 'string' != $type && 'istring' != $type && 'array' != $type && 'null' != $type && 'numeric' != $type) {
// echo $variable, ' ', $type, PHP_EOL;
return $type;
}
}
return false;
}
|
Asks the local context information whether a variable can be stored in the stack instead of the heap.
@param string $variable
@return bool
|
getInferedType
|
php
|
zephir-lang/zephir
|
src/Passes/StaticTypeInference.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/StaticTypeInference.php
|
MIT
|
public function markVariable($variable, $type): void
{
if (isset($this->variables[$variable])) {
$currentType = $this->variables[$variable];
if ('undefined' == $currentType) {
return;
}
} else {
$this->variables[$variable] = $type;
return;
}
switch ($currentType) {
case 'numeric':
switch ($type) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
case 'double':
break;
default:
$this->variables[$variable] = 'undefined';
break;
}
break;
case 'bool':
switch ($type) {
case 'bool':
case 'numeric':
break;
default:
$this->variables[$variable] = 'undefined';
break;
}
break;
case 'double':
switch ($type) {
case 'double':
case 'numeric':
break;
default:
$this->variables[$variable] = 'undefined';
break;
}
break;
case 'string':
switch ($type) {
case 'string':
break;
default:
$this->variables[$variable] = 'undefined';
break;
}
break;
case 'int':
case 'uint':
case 'long':
case 'ulong':
case 'char':
case 'uchar':
switch ($type) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
case 'char':
case 'uchar':
case 'numeric':
break;
default:
$this->variables[$variable] = 'undefined';
break;
}
break;
case 'null':
if ('null' != $type) {
$this->variables[$variable] = 'undefined';
}
break;
case 'variable':
case 'array':
case 'mixed':
$this->variables[$variable] = 'undefined';
break;
default:
// TODO: Find the reason
echo 'StaticTypeInference=', $currentType, ' ', $type, PHP_EOL;
break;
}
}
|
Marks a variable to mandatory be stored in the heap.
@param string $variable
@param string $type
|
markVariable
|
php
|
zephir-lang/zephir
|
src/Passes/StaticTypeInference.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/StaticTypeInference.php
|
MIT
|
public function markVariableIfUnknown($variable, $type): void
{
$this->variables[$variable] = $type;
$this->infered[$variable] = $type;
}
|
Marks a variable to mandatory be stored in the heap if a type has not been defined for it.
@param string $variable
@param string $type
|
markVariableIfUnknown
|
php
|
zephir-lang/zephir
|
src/Passes/StaticTypeInference.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/StaticTypeInference.php
|
MIT
|
public function pass(StatementsBlock $block): void
{
$this->passStatementBlock($block->getStatements());
}
|
Do the compilation pass.
@param StatementsBlock $block
|
pass
|
php
|
zephir-lang/zephir
|
src/Passes/StaticTypeInference.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/StaticTypeInference.php
|
MIT
|
public function reduce(): bool
{
$pass = false;
foreach ($this->variables as $variable => $type) {
if ('variable' == $type || 'string' == $type || 'istring' == $type || 'array' == $type || 'null' == $type || 'numeric' == $type) {
unset($this->variables[$variable]);
} else {
$pass = true;
$this->infered[$variable] = $type;
}
}
return $pass;
}
|
Process the found infered types and schedule a new pass.
@return bool
|
reduce
|
php
|
zephir-lang/zephir
|
src/Passes/StaticTypeInference.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Passes/StaticTypeInference.php
|
MIT
|
public function compileHashTraverse(
CompilationContext $compilationContext,
Variable $exprVariable
): void {
/**
* Initialize 'key' variable
*/
if (isset($this->statement['key'])) {
if ('_' != $this->statement['key']) {
$keyVariable = $compilationContext->symbolTable->getVariableForWrite(
$this->statement['key'],
$compilationContext,
$this->statement['expr']
);
if ('variable' != $keyVariable->getType()) {
throw new CompilerException(
'Cannot use variable: '
. $this->statement['key']
. ' type: '
. $keyVariable->getType()
. ' as key in hash traversal',
$this->statement['expr']
);
}
} else {
$keyVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
}
$keyVariable->setMustInitNull(true);
$keyVariable->setIsInitialized(true, $compilationContext);
$keyVariable->setDynamicTypes('undefined');
}
/**
* Initialize 'value' variable
*/
if (isset($this->statement['value'])) {
if ('_' != $this->statement['value']) {
$variable = $compilationContext->symbolTable->getVariableForWrite(
$this->statement['value'],
$compilationContext,
$this->statement['expr']
);
if ('variable' != $variable->getType()) {
throw new CompilerException(
'Cannot use variable: '
. $this->statement['value']
. ' type: '
. $variable->getType()
. ' as value in hash traversal',
$this->statement['expr']
);
}
} else {
$variable = $compilationContext->symbolTable->getTempVariableForWrite('variable', $compilationContext);
}
$variable->setMustInitNull(true);
$variable->setIsInitialized(true, $compilationContext);
$variable->increaseVariantIfNull();
$variable->setDynamicTypes('undefined');
}
/**
* Variables are initialized in a different way inside cycle
*/
++$compilationContext->insideCycle;
$duplicateHash = '0';
$duplicateKey = true;
/**
* We have to check if hashes are modified within the for's block.
*/
$st = null;
if (isset($this->statement['statements'])) {
/**
* Create the statements block here to obtain the last use line.
*/
$st = new StatementsBlock($this->statement['statements']);
$detector = new ForValueUseDetector();
if ($detector->detect($exprVariable->getName(), $this->statement['statements'])) {
$duplicateHash = '1';
}
/**
* Detect if the key is modified or passed to an external scope
*/
if (isset($this->statement['key'])) {
if (!$keyVariable->isTemporal()) {
$detector->setDetectionFlags(WriteDetector::DETECT_ALL);
if ($detector->detect($keyVariable->getName(), $this->statement['statements'])) {
$duplicateKey = true;
}
}
}
}
$compilationContext->backend->forStatement(
$exprVariable,
isset($this->statement['key']) ? $keyVariable : null,
isset($this->statement['value']) ? $variable : null,
$duplicateKey,
$duplicateHash,
$this->statement,
$st,
$compilationContext
);
/**
* Restore the cycle counter
*/
--$compilationContext->insideCycle;
}
|
Compiles traversing of hash values.
- Evaluated expression must be a zval
- A key must be a zval
- A value must be a zval
|
compileHashTraverse
|
php
|
zephir-lang/zephir
|
src/Statements/ForStatement.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/ForStatement.php
|
MIT
|
public function compileIterator(array $exprRaw, CompilationContext $compilationContext): void
{
$iteratorVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'zend_object_iterator',
$compilationContext
);
$compilationContext->headersManager->add('kernel/iterator');
$codePrinter = $compilationContext->codePrinter;
/**
* Resolve first parameter of iterator(p).
*/
$expr = new Expression($exprRaw['parameters'][0]['parameter']);
$expr->setReadOnly(false);
$expression = $expr->compile($compilationContext);
if ('variable' != $expression->getType()) {
throw new CompilerException('Unknown type: ' . $expression->getType(), $exprRaw);
}
$exprVariable = $compilationContext->symbolTable->getVariableForRead(
$expression->getCode(),
$compilationContext,
$this->statement['expr']
);
/**
* Initialize 'key' variable
*/
if (isset($this->statement['key'])) {
if ('_' != $this->statement['key']) {
$keyVariable = $compilationContext->symbolTable->getVariableForWrite(
$this->statement['key'],
$compilationContext,
$this->statement['expr']
);
if ('variable' != $keyVariable->getType()) {
throw new CompilerException(
'Cannot use variable: '
. $this->statement['key']
. ' type: '
. $keyVariable->getType()
. ' as key in hash traversal',
$this->statement['expr']
);
}
} else {
/**
* Anonymous key variable.
*/
$keyVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
$keyVariable->increaseUses();
}
$keyVariable->setMustInitNull(true);
$keyVariable->setIsInitialized(true, $compilationContext);
$keyVariable->setDynamicTypes('undefined');
}
/**
* Initialize 'value' variable
*/
if (isset($this->statement['value'])) {
if ('_' != $this->statement['value']) {
$variable = $compilationContext->symbolTable->getVariableForWrite(
$this->statement['value'],
$compilationContext,
$this->statement['expr']
);
if ('variable' != $variable->getType()) {
throw new CompilerException(
'Cannot use variable: '
. $this->statement['value']
. ' type: '
. $variable->getType()
. ' as value in hash traversal',
$this->statement['expr']
);
}
} else {
/**
* Anonymous value variable.
*/
$variable = $compilationContext->symbolTable->getTempVariableForWrite('variable', $compilationContext);
$variable->increaseUses();
}
$variable->setMustInitNull(true);
$variable->setIsInitialized(true, $compilationContext);
$variable->setDynamicTypes('undefined');
}
/**
* Variables are initialized in a different way inside cycle
*/
++$compilationContext->insideCycle;
$codePrinter->output(
$iteratorVariable->getName() . ' = zephir_get_iterator(' . $compilationContext->backend->getVariableCode(
$exprVariable
) . ');'
);
$codePrinter->output($iteratorVariable->getName() . '->funcs->rewind(' . $iteratorVariable->getName() . ');');
$codePrinter->output(
'for (;'
. $iteratorVariable->getName()
. '->funcs->valid('
. $iteratorVariable->getName()
. ') == SUCCESS && !EG(exception); '
. $iteratorVariable->getName()
. '->funcs->move_forward('
. $iteratorVariable->getName()
. ')) {'
);
if (isset($this->statement['key'])) {
$compilationContext->symbolTable->mustGrownStack(true);
$codePrinter->output(
"\t"
. 'ZEPHIR_GET_IMKEY('
. $this->statement['key']
. ', '
. $iteratorVariable->getName()
. ');'
);
}
if (isset($this->statement['value'])) {
$compilationContext->symbolTable->mustGrownStack(true);
$codePrinter->increaseLevel();
$codePrinter->output('{');
$codePrinter->increaseLevel();
$compilationContext->symbolTable->mustGrownStack(true);
$compilationContext->codePrinter->output(
'ZEPHIR_ITERATOR_COPY('
. $compilationContext->backend->getVariableCode($variable)
. ', '
. $iteratorVariable->getName()
. ');'
);
$codePrinter->decreaseLevel();
$codePrinter->output('}');
$codePrinter->decreaseLevel();
}
/**
* Compile statements in the 'for' block
*/
$this->compileStatementsForBlock($compilationContext, $codePrinter);
$compilationContext->backend->destroyIterator($iteratorVariable, $compilationContext);
}
|
Compiles a 'for' statement that use an 'iterator' as expression.
@throws Exception
@throws ReflectionException
|
compileIterator
|
php
|
zephir-lang/zephir
|
src/Statements/ForStatement.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/ForStatement.php
|
MIT
|
public function compileRange(array $exprRaw, CompilationContext $compilationContext): bool
{
if (!count($exprRaw['parameters'])) {
return false;
}
if (count($exprRaw['parameters']) > 3) {
return false;
}
$functionCall = new FunctionCall();
$parameters = $functionCall->getResolvedParamsAsExpr($exprRaw['parameters'], $compilationContext, $exprRaw);
if (2 != count($parameters) && 3 != count($parameters)) {
throw new CompilerException(
'Wrong number of parameters',
$this->statement['expr']
);
}
if ('variable' != $parameters[0]->getType()) {
if (!$parameters[0]->isIntCompatibleType()) {
return false;
}
}
if ('variable' != $parameters[1]->getType()) {
if (!$parameters[1]->isIntCompatibleType()) {
return false;
}
}
$codePrinter = $compilationContext->codePrinter;
$exprBuilder = BuilderFactory::getInstance();
/**
* Initialize 'key' variable
*/
if (isset($this->statement['key'])) {
/**
* This variable is used to check if the loop is in its first iteration.
*/
$keyVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'long',
$compilationContext,
$this->statement
);
$keyVariable->increaseUses();
}
/**
* This variable is used to check if the loop is in its first iteration.
*/
$flagVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'bool',
$compilationContext,
$this->statement
);
if ('variable' != $parameters[0]->getType()) {
$tempVariable = $compilationContext->symbolTable->addTemp($parameters[0]->getType(), $compilationContext);
} else {
$rangeVariable = $compilationContext->symbolTable->getVariableForRead(
$parameters[0]->getCode(),
$compilationContext,
$this->statement['expr']
);
$tempVariable = $compilationContext->symbolTable->addTemp($rangeVariable->getType(), $compilationContext);
}
/**
* Create a copy of the current value in the end of the range to avoid modify the range
* inside the cycle
*/
if ('variable' != $parameters[1]->getType()) {
$upperBoundVariable = $compilationContext->symbolTable->getTempVariable(
$parameters[1]->getType(),
$compilationContext
);
} else {
$rangeVariable = $compilationContext->symbolTable->getVariableForRead(
$parameters[1]->getCode(),
$compilationContext,
$this->statement['expr']
);
$upperBoundVariable = $compilationContext->symbolTable->getTempVariable(
$rangeVariable->getType(),
$compilationContext
);
}
/**
* Create an implicit 'let' operation to set the current value in the upper bound of the range.
*/
$builderLet = $exprBuilder->statements()->let();
$builderLet->setAssignments([
$exprBuilder->operators()
->assignVariable(
$upperBoundVariable->getName(),
$exprBuilder->literal($parameters[1]->getType(), $parameters[1]->getCode())
->setFile($this->statement['file'])
->setLine($this->statement['line'])
->setChar($this->statement['char'])
)
->setFile($this->statement['file'])
->setLine($this->statement['line'])
->setChar($this->statement['char']),
]);
$statement = new LetStatement($builderLet->build());
$statement->compile($compilationContext);
if ($this->statement['reverse']) {
/**
* Create an implicit 'let' operation for the initialize expression
*/
$builderLet->setAssignments([
$exprBuilder->operators()
->assignVariable(
$tempVariable->getName(),
$exprBuilder->variable($upperBoundVariable->getName())
->setFile($this->statement['file'])
->setLine($this->statement['line'])
->setChar($this->statement['char'])
)
->setFile($this->statement['file'])
->setLine($this->statement['line'])
->setChar($this->statement['char']),
]);
} else {
/**
* Create an implicit 'let' operation for the initialize expression
*/
$builderLet->setAssignments([
$exprBuilder->operators()
->assignVariable(
$tempVariable->getName(),
$exprBuilder->literal($parameters[0]->getType(), $parameters[0]->getCode())
->setFile($this->statement['file'])
->setLine($this->statement['line'])
->setChar($this->statement['char'])
)
->setFile($this->statement['file'])
->setLine($this->statement['line'])
->setChar($this->statement['char']),
]);
}
$statement = new LetStatement($builderLet->build());
$statement->compile($compilationContext);
/**
* Initialize 'key' variable
*/
if (isset($this->statement['key'])) {
$codePrinter->output($keyVariable->getName() . ' = 0;');
}
$codePrinter->output($flagVariable->getName() . ' = 0;');
if ($this->statement['reverse']) {
$conditionExpr = [
'type' => 'greater-equal',
'left' => ['type' => 'variable', 'value' => $tempVariable->getName()],
'right' => ['type' => $parameters[0]->getType(), 'value' => $parameters[0]->getCode()],
];
} else {
$conditionExpr = [
'type' => 'less-equal',
'left' => ['type' => 'variable', 'value' => $tempVariable->getName()],
'right' => ['type' => 'variable', 'value' => $upperBoundVariable->getName()],
];
}
$expr = new EvalExpression();
$condition = $expr->optimize($conditionExpr, $compilationContext);
$codePrinter->output('if (' . $condition . ') {');
$codePrinter->increaseLevel();
/**
* Inside a cycle
*/
++$compilationContext->insideCycle;
$codePrinter->output('while (1) {');
$codePrinter->increaseLevel();
$codePrinter->output('if (' . $flagVariable->getName() . ') {');
$codePrinter->increaseLevel();
if (isset($this->statement['key'])) {
$codePrinter->output($keyVariable->getName() . '++;');
}
if ($this->statement['reverse']) {
if (!isset($parameters[2])) {
$statement = new LetStatement([
'type' => 'let',
'assignments' => [
[
'assign-type' => 'decr',
'variable' => $tempVariable->getName(),
'file' => $this->statement['file'],
'line' => $this->statement['line'],
'char' => $this->statement['char'],
],
],
]);
} else {
$statement = new LetStatement([
'type' => 'let',
'assignments' => [
[
'assign-type' => 'variable',
'operator' => 'sub-assign',
'variable' => $tempVariable->getName(),
'expr' => [
'type' => $parameters[2]->getType(),
'value' => $parameters[2]->getCode(),
'file' => $this->statement['file'],
'line' => $this->statement['line'],
'char' => $this->statement['char'],
],
'file' => $this->statement['file'],
'line' => $this->statement['line'],
'char' => $this->statement['char'],
],
],
]);
}
} else {
if (!isset($parameters[2])) {
$statement = new LetStatement([
'type' => 'let',
'assignments' => [
[
'assign-type' => 'incr',
'variable' => $tempVariable->getName(),
'file' => $this->statement['file'],
'line' => $this->statement['line'],
'char' => $this->statement['char'],
],
],
]);
} else {
$statement = new LetStatement([
'type' => 'let',
'assignments' => [
[
'assign-type' => 'variable',
'operator' => 'add-assign',
'variable' => $tempVariable->getName(),
'expr' => [
'type' => $parameters[2]->getType(),
'value' => $parameters[2]->getCode(),
'file' => $this->statement['file'],
'line' => $this->statement['line'],
'char' => $this->statement['char'],
],
'file' => $this->statement['file'],
'line' => $this->statement['line'],
'char' => $this->statement['char'],
],
],
]);
}
}
$statement->compile($compilationContext);
/**
* Multi-line conditions would need to be regenerated here.
*/
$condition = $expr->optimize($conditionExpr, $compilationContext);
$codePrinter->output('if (!(' . $condition . ')) {');
$codePrinter->output("\t" . 'break;');
$codePrinter->output('}');
$codePrinter->decreaseLevel();
$codePrinter->output('} else {');
$codePrinter->output("\t" . $flagVariable->getName() . ' = 1;');
$codePrinter->output('}');
/**
* Initialize 'key' variable
*/
if (isset($this->statement['key'])) {
/**
* Check for anonymous variables
*/
if ('_' != $this->statement['key']) {
$keyVariableName = $this->statement['key'];
} else {
$keyVariableName = $keyVariable->getName();
}
/**
* Create an implicit 'let' operation, TODO: use a builder.
*/
$this->getLetStatement($keyVariableName, $keyVariable, $compilationContext);
}
/**
* Initialize 'value' variable
*/
if (isset($this->statement['value'])) {
/**
* Check for anonymous variables
*/
if ('_' != $this->statement['value']) {
$valueVariable = $this->statement['value'];
} else {
$valueVariable = $tempVariable->getName();
}
/**
* Create an implicit 'let' operation, TODO: use a builder.
*/
$this->getLetStatement($valueVariable, $tempVariable, $compilationContext);
}
$codePrinter->decreaseLevel();
/**
* Compile statements in the 'for' block
*/
$this->compileStatementsForBlock($compilationContext, $codePrinter);
$codePrinter->decreaseLevel();
$codePrinter->output('}');
return true;
}
|
Compiles a for statement that use a 'range' as expression.
@throws ReflectionException
@throws Exception
|
compileRange
|
php
|
zephir-lang/zephir
|
src/Statements/ForStatement.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/ForStatement.php
|
MIT
|
public function compileStringTraverse(
CompiledExpression $expression,
CompilationContext $compilationContext,
?Variable $exprVariable = null,
): void {
$codePrinter = $compilationContext->codePrinter;
/**
* Initialize 'key' variable
*/
if (isset($this->statement['key'])) {
if ('_' != $this->statement['key']) {
$keyVariable = $compilationContext->symbolTable->getVariableForWrite(
$this->statement['key'],
$compilationContext,
$this->statement['expr']
);
switch ($keyVariable->getType()) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
case 'char':
case 'uchar':
break;
default:
throw new CompilerException(
'Cannot use variable: '
. $this->statement['key']
. ' type: '
. $keyVariable->getType()
. ' as key in string traversal',
$this->statement['expr']
);
}
} else {
$keyVariable = $compilationContext->symbolTable->getTempVariableForWrite('int', $compilationContext);
$keyVariable->increaseUses();
}
$keyVariable->setMustInitNull(true);
$keyVariable->setIsInitialized(true, $compilationContext);
}
/**
* Initialize 'value' variable
*/
if (isset($this->statement['value'])) {
if ('_' != $this->statement['value']) {
$variable = $compilationContext->symbolTable->getVariableForWrite(
$this->statement['value'],
$compilationContext,
$this->statement['expr']
);
switch ($variable->getType()) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
case 'char':
case 'uchar':
break;
default:
throw new CompilerException(
'Cannot use variable: '
. $this->statement['value']
. ' type: '
. $variable->getType()
. ' as value in string traversal',
$this->statement['expr']
);
}
} else {
$variable = $compilationContext->symbolTable->getTempVariableForWrite('char', $compilationContext);
$variable->increaseUses();
}
$variable->setMustInitNull(true);
$variable->setIsInitialized(true, $compilationContext);
}
$tempVariable = $compilationContext->symbolTable->addTemp('long', $compilationContext);
/**
* Create a temporary value to store the constant string
*/
if ('string' == $expression->getType()) {
$constantVariable = $compilationContext->symbolTable->getTempLocalVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->backend->assignString(
$constantVariable,
Name::addSlashes($expression->getCode()),
$compilationContext,
);
$stringVariable = $constantVariable;
} else {
$stringVariable = $exprVariable;
}
$stringVariableCode = $compilationContext->backend->getVariableCode($stringVariable);
if ($this->statement['reverse']) {
$codePrinter->output(
'for ('
. $tempVariable->getName()
. ' = Z_STRLEN_P('
. $stringVariableCode
. '); '
. $tempVariable->getName()
. ' >= 0; '
. $tempVariable->getName()
. '--) {'
);
} else {
$codePrinter->output(
'for ('
. $tempVariable->getName()
. ' = 0; '
. $tempVariable->getName()
. ' < Z_STRLEN_P('
. $stringVariableCode
. '); '
. $tempVariable->getName()
. '++) {'
);
}
if (isset($this->statement['key'])) {
$codePrinter->output(
"\t"
. $keyVariable->getName()
. ' = '
. $tempVariable->getName()
. '; '
);
}
$compilationContext->headersManager->add('kernel/operators');
$codePrinter->output(
"\t" . $variable->getName(
) . ' = ZEPHIR_STRING_OFFSET(' . $stringVariableCode . ', ' . $tempVariable->getName() . ');'
);
/**
* Variables are initialized in a different way inside cycle
*/
++$compilationContext->insideCycle;
/**
* Compile statements in the 'for' block
*/
$this->compileStatementsForBlock($compilationContext, $codePrinter);
}
|
Compiles traversing of string values
- Evaluated expression must be a string
- Every key must be an integer or compatible
- Every value must be a char/integer or compatible.
@throws Exception
@throws ReflectionException
|
compileStringTraverse
|
php
|
zephir-lang/zephir
|
src/Statements/ForStatement.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/ForStatement.php
|
MIT
|
private function throwStringException(Printer $printer, string $class, string $message, array $expression): void
{
$message = Name::addSlashes($message);
$path = Compiler::getShortUserPath($expression['file']);
$printer->output(
sprintf(
'ZEPHIR_THROW_EXCEPTION_DEBUG_STR(%s, "%s", "%s", %s);',
$class,
$message,
$path,
$expression['line']
)
);
$printer->output('return;');
}
|
Throws an exception escaping the data.
|
throwStringException
|
php
|
zephir-lang/zephir
|
src/Statements/ThrowStatement.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/ThrowStatement.php
|
MIT
|
public function assign(
$variable,
ZephirVariable $symbolVariable,
CompiledExpression $resolvedExpr,
CompilationContext $compilationContext,
$statement
): void {
/**
* Arrays must be stored in the HEAP
*/
$this->checkVariableInitialized($variable, $symbolVariable, $statement);
$this->checkVariableReadOnly($variable, $symbolVariable, $statement);
$this->checkVariableLocalOnly($variable, $symbolVariable, $statement);
/**
* Only dynamic variables can be used as arrays
*/
if ($symbolVariable->isNotVariableAndArray()) {
throw CompilerException::cannotUseAsArray(
$symbolVariable->getType(),
$statement
);
}
if ('variable' == $symbolVariable->getType()) {
if ($symbolVariable->hasAnyDynamicType('unknown')) {
throw CompilerException::cannotUseNonInitializedVariableAsObject($statement);
}
/**
* Trying to use a non-object dynamic variable as object
*/
if ($symbolVariable->hasDifferentDynamicType(['undefined', 'array', 'null'])) {
$compilationContext->logger->warning(
'Possible attempt to update index on a non-array dynamic variable',
['non-array-update', $statement]
);
}
}
/**
* Choose one-offset or multiple-offset functions
*/
if (1 === count($statement['index-expr'])) {
$this->_assignArrayIndexSingle($symbolVariable, $resolvedExpr, $compilationContext, $statement);
} else {
$this->assignArrayIndexMultiple(
$variable,
$resolvedExpr,
$compilationContext,
$statement
);
}
}
|
Compiles foo[y] = {expr}.
@throws Exception
@throws ReflectionException
|
assign
|
php
|
zephir-lang/zephir
|
src/Statements/Let/ArrayIndex.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/ArrayIndex.php
|
MIT
|
protected function assignArrayIndexMultiple(
$variable,
CompiledExpression $resolvedExpr,
CompilationContext $compilationContext,
$statement,
): void {
$offsetExprs = [];
foreach ($statement['index-expr'] as $indexExpr) {
$expression = new Expression($indexExpr);
$expression->setReadOnly(true);
$exprIndex = $expression->compile($compilationContext);
switch ($exprIndex->getType()) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
case 'string':
case 'variable':
break;
default:
throw new CompilerException(
sprintf(
'Index: %s cannot be used as array index in assignment without cast',
$exprIndex->getType()
),
$indexExpr
);
}
$offsetExprs[] = $exprIndex;
}
$compilationContext->headersManager->add('kernel/array');
/**
* Create a temporal zval (if needed).
*/
$symbolVariable = $this->_getResolvedArrayItem($resolvedExpr, $compilationContext);
$targetVariable = $compilationContext->symbolTable->getVariableForWrite(
$variable,
$compilationContext,
$statement
);
$compilationContext->backend->assignArrayMulti(
$targetVariable,
$symbolVariable,
$offsetExprs,
$compilationContext
);
$this->checkVariableTemporal($symbolVariable);
}
|
Compiles foo[y][x] = {expr} (multiple offset).
@throws Exception
@throws ReflectionException
|
assignArrayIndexMultiple
|
php
|
zephir-lang/zephir
|
src/Statements/Let/ArrayIndex.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/ArrayIndex.php
|
MIT
|
protected function _assignArrayIndexSingle(
ZephirVariable $symbolVariable,
CompiledExpression $resolvedExpr,
CompilationContext $compilationContext,
$statement
): void {
$expression = new Expression($statement['index-expr'][0]);
$exprIndex = $expression->compile($compilationContext);
switch ($exprIndex->getType()) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
case 'string':
case 'variable':
break;
default:
throw new CompilerException(
'Index: '
. $exprIndex->getType()
. ' cannot be used as array offset in assignment without cast',
$statement['index-expr'][0]
);
}
/**
* Create a temporal zval (if needed).
*/
$realSymbolVariable = $symbolVariable;
$symbolVariable = $this->_getResolvedArrayItem($resolvedExpr, $compilationContext);
$flags = 'PH_COPY | PH_SEPARATE';
$compilationContext->headersManager->add('kernel/array');
switch ($exprIndex->getType()) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
case 'string':
$compilationContext->backend->updateArray(
$realSymbolVariable,
$exprIndex,
$symbolVariable,
$compilationContext,
$flags
);
break;
case 'variable':
$variableIndex = $compilationContext->symbolTable->getVariableForRead(
$exprIndex->getCode(),
$compilationContext,
$statement
);
switch ($variableIndex->getType()) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
case 'string':
case 'variable':
$compilationContext->backend->updateArray(
$realSymbolVariable,
$variableIndex,
$symbolVariable,
$compilationContext,
$flags
);
break;
default:
throw new CompilerException(
'Variable: '
. $variableIndex->getType()
. ' cannot be used as array index',
$statement
);
}
break;
default:
throw new CompilerException(
'Value: ' . $exprIndex->getType() . ' cannot be used as array index',
$statement
);
}
}
|
Compiles foo[y] = {expr} (one offset).
@throws Exception
@throws ReflectionException
|
_assignArrayIndexSingle
|
php
|
zephir-lang/zephir
|
src/Statements/Let/ArrayIndex.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/ArrayIndex.php
|
MIT
|
protected function _getResolvedArrayItem(CompiledExpression $resolvedExpr, CompilationContext $compilationContext)
{
switch ($resolvedExpr->getType()) {
case 'null':
$symbolVariable = new GlobalConstant('ZEPHIR_GLOBAL(global_null)');
break;
case 'int':
case 'uint':
case 'long':
$symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext,
$resolvedExpr->getOriginal()
);
$compilationContext->backend->assignLong(
$symbolVariable,
$resolvedExpr->getCode(),
$compilationContext
);
break;
case 'char':
$symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext,
$resolvedExpr->getOriginal()
);
$compilationContext->backend->assignLong(
$symbolVariable,
'\'' . $resolvedExpr->getCode() . '\'',
$compilationContext
);
break;
case 'double':
$symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext,
$resolvedExpr->getOriginal()
);
$compilationContext->backend->assignDouble(
$symbolVariable,
$resolvedExpr->getCode(),
$compilationContext
);
break;
case 'bool':
if ('1' == $resolvedExpr->getBooleanCode()) {
$symbolVariable = new GlobalConstant('ZEPHIR_GLOBAL(global_true)');
} else {
if ('0' == $resolvedExpr->getBooleanCode()) {
$symbolVariable = new GlobalConstant('ZEPHIR_GLOBAL(global_false)');
} else {
$symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext,
$resolvedExpr->getOriginal()
);
$compilationContext->backend->assignBool(
$symbolVariable,
$resolvedExpr->getBooleanCode(),
$compilationContext
);
}
}
break;
case 'string':
$symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext,
$resolvedExpr->getOriginal()
);
$compilationContext->backend->assignString(
$symbolVariable,
$resolvedExpr->getCode(),
$compilationContext
);
break;
case 'array':
$symbolVariable = $compilationContext->symbolTable->getVariableForRead(
$resolvedExpr->getCode(),
$compilationContext,
$resolvedExpr->getOriginal()
);
break;
case 'variable':
$variableExpr = $compilationContext->symbolTable->getVariableForRead(
$resolvedExpr->getCode(),
$compilationContext,
$resolvedExpr->getOriginal()
);
switch ($variableExpr->getType()) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
$symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext,
$resolvedExpr->getOriginal()
);
$compilationContext->backend->assignLong($symbolVariable, $variableExpr, $compilationContext);
break;
case 'double':
$symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext,
$resolvedExpr->getOriginal()
);
$compilationContext->backend->assignDouble($symbolVariable, $variableExpr, $compilationContext);
break;
case 'bool':
$symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext,
$resolvedExpr->getOriginal()
);
$compilationContext->backend->assignBool($symbolVariable, $variableExpr, $compilationContext);
break;
case 'variable':
case 'string':
case 'array':
$symbolVariable = $variableExpr;
break;
default:
throw new CompilerException(
'Variable: ' . $variableExpr->getType() . ' cannot be assigned to array offset',
$resolvedExpr->getOriginal()
);
}
break;
default:
throw new CompilerException(
'Expression: ' . $resolvedExpr->getType() . ' cannot be assigned to array offset',
$resolvedExpr->getOriginal()
);
}
return $symbolVariable;
}
|
Resolves an item that will be assigned to an array offset.
@param CompiledExpression $resolvedExpr
@param CompilationContext $compilationContext
@return GlobalConstant|ZephirVariable
@throws CompilerException
|
_getResolvedArrayItem
|
php
|
zephir-lang/zephir
|
src/Statements/Let/ArrayIndex.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/ArrayIndex.php
|
MIT
|
public function assign(
$variable,
ZephirVariable $symbolVariable,
CompiledExpression $resolvedExpr,
CompilationContext $compilationContext,
$statement
): void {
/**
* Arrays must be stored in the HEAP
*/
$this->checkVariableInitialized($variable, $symbolVariable, $statement);
$this->checkVariableReadOnly($variable, $symbolVariable, $statement);
$this->checkVariableLocalOnly($variable, $symbolVariable, $statement);
/**
* Only dynamic variables and arrays can be used as arrays
*/
if ($symbolVariable->isNotVariableAndArray()) {
throw CompilerException::cannotUseAsArray(
$symbolVariable->getType(),
$statement
);
}
if ($symbolVariable->hasAnyDynamicType('unknown')) {
throw CompilerException::cannotUseNonInitializedVariableAsObject($statement);
}
/**
* Trying to use a non-object dynamic variable as object
*/
if ($symbolVariable->hasDifferentDynamicType(['undefined', 'array', 'null'])) {
$compilationContext->logger->warning(
'Possible attempt to update index on a non-array dynamic variable',
['non-array-update', $statement]
);
}
$this->assignArrayIndexMultiple($variable, $resolvedExpr, $compilationContext, $statement);
}
|
Compiles foo[y][] = {expr}.
@throws Exception
@throws ReflectionException
|
assign
|
php
|
zephir-lang/zephir
|
src/Statements/Let/ArrayIndexAppend.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/ArrayIndexAppend.php
|
MIT
|
protected function assignArrayIndexMultiple(
$variable,
CompiledExpression $resolvedExpr,
CompilationContext $compilationContext,
$statement
): void {
$offsetExprs = [];
$types = ['int', 'uint', 'long', 'ulong', 'string', 'variable'];
foreach ($statement['index-expr'] as $indexExpr) {
$expression = new Expression($indexExpr);
$expression->setReadOnly(true);
$exprIndex = $expression->compile($compilationContext);
if (!in_array($exprIndex->getType(), $types)) {
throw new CompilerException(
'Index: '
. $exprIndex->getType()
. ' cannot be used as array index in assignment without cast',
$indexExpr
);
}
$offsetExprs[] = $exprIndex;
}
$compilationContext->headersManager->add('kernel/array');
/**
* Create a temporal zval (if needed).
*/
$symbolVariable = $this->_getResolvedArrayItem($resolvedExpr, $compilationContext);
$targetVariable = $compilationContext->symbolTable->getVariableForWrite(
$variable,
$compilationContext,
$statement
);
$offsetExprs[] = 'a';
$compilationContext->backend->assignArrayMulti(
$targetVariable,
$symbolVariable,
$offsetExprs,
$compilationContext
);
}
|
Compiles foo[y][x][] = {expr} (multiple offset).
@throws Exception
@throws ReflectionException
|
assignArrayIndexMultiple
|
php
|
zephir-lang/zephir
|
src/Statements/Let/ArrayIndexAppend.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/ArrayIndexAppend.php
|
MIT
|
public function assign(
string $variable,
ZephirVariable $symbolVariable,
CompiledExpression $resolvedExpr,
CompilationContext $compilationContext,
array $statement
): void {
$this->checkVariableInitialized($variable, $symbolVariable, $statement);
if (!$symbolVariable->isVariable()) {
throw new CompilerException(
"Variable type '" . $symbolVariable->getType() . "' cannot be used as object",
$statement
);
}
$propertyName = $statement['property'];
$propertyVariable = $compilationContext->symbolTable->getVariableForRead(
$propertyName,
$compilationContext,
$statement
);
if ($propertyVariable->isNotVariableAndMixedAndString()) {
throw new CompilerException(
"Cannot use variable type '" . $propertyVariable->getType() . "' to update object property",
$statement
);
}
if (!$symbolVariable->isInitialized()) {
throw new CompilerException(
"Cannot mutate static property '" . $compilationContext->classDefinition->getCompleteName(
) . '::' . $propertyName . "' because it is not initialized",
$statement
);
}
if (!$symbolVariable->isVariable()) {
throw CompilerException::cannotUseVariableTypeAs(
$symbolVariable,
'as an object',
$statement
);
}
if ($symbolVariable->hasAnyDynamicType('unknown')) {
throw CompilerException::cannotUseNonInitializedVariableAsObject($statement);
}
/**
* Trying to use a non-object dynamic variable as object
*/
if ($symbolVariable->hasDifferentDynamicType(['undefined', 'object', 'null'])) {
$compilationContext->logger->warning(
'Possible attempt to update property on non-object dynamic property',
['non-valid-objectupdate', $statement]
);
}
$codePrinter = $compilationContext->codePrinter;
$compilationContext->headersManager->add('kernel/object');
$propertyVariableName = $compilationContext->symbolTable->getVariable($propertyName);
switch ($resolvedExpr->getType()) {
case 'null':
$compilationContext->backend->updateProperty(
$symbolVariable,
$propertyVariableName,
'null',
$compilationContext
);
break;
case 'int':
case 'long':
$tempVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->backend->assignLong($tempVariable, $resolvedExpr->getCode(), $compilationContext);
$compilationContext->backend->updateProperty(
$symbolVariable,
$propertyVariableName,
$tempVariable,
$compilationContext
);
break;
case 'string':
$tempVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->backend->assignString(
$tempVariable,
$resolvedExpr->getCode(),
$compilationContext
);
$compilationContext->backend->updateProperty(
$symbolVariable,
$propertyVariableName,
$tempVariable,
$compilationContext
);
break;
case 'bool':
if ('1' == $resolvedExpr->getBooleanCode()) {
$value = 'true';
} elseif ('0' == $resolvedExpr->getBooleanCode()) {
$value = 'false';
} else {
throw new Exception('?');
}
$compilationContext->backend->updateProperty(
$symbolVariable,
$propertyVariableName,
$value,
$compilationContext
);
break;
case 'empty-array':
$tempVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
$codePrinter->output('array_init(' . $tempVariable->getName() . ');');
$compilationContext->backend->updateProperty(
$symbolVariable,
$propertyVariableName,
$tempVariable,
$compilationContext
);
break;
case 'array':
$variableVariable = $compilationContext->symbolTable->getVariableForRead(
$resolvedExpr->getCode(),
$compilationContext,
$statement
);
$compilationContext->backend->updateProperty(
$symbolVariable,
$propertyVariableName,
$variableVariable,
$compilationContext
);
break;
case 'variable':
$variableVariable = $compilationContext->symbolTable->getVariableForRead(
$resolvedExpr->getCode(),
$compilationContext,
$statement
);
switch ($variableVariable->getType()) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
case 'char':
case 'uchar':
$tempVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->backend->assignLong($tempVariable, $variableVariable, $compilationContext);
$compilationContext->backend->updateProperty(
$symbolVariable,
$propertyVariableName,
$tempVariable,
$compilationContext
);
break;
case 'bool':
$tempVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->backend->assignBool($tempVariable, $variableVariable, $compilationContext);
$compilationContext->backend->updateProperty(
$symbolVariable,
$propertyVariableName,
$tempVariable,
$compilationContext
);
break;
case 'string':
case 'variable':
case 'array':
case 'mixed':
$compilationContext->backend->updateProperty(
$symbolVariable,
$propertyVariable,
$resolvedExpr,
$compilationContext
);
$this->checkVariableTemporal($symbolVariable);
break;
default:
throw new CompilerException('Unknown type ' . $variableVariable->getType(), $statement);
}
break;
default:
throw new CompilerException('Unknown type ' . $resolvedExpr->getType(), $statement);
}
}
|
Compiles foo->{x} = {expr}.
@throws CompilerException
@throws Exception
|
assign
|
php
|
zephir-lang/zephir
|
src/Statements/Let/ObjectDynamicProperty.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/ObjectDynamicProperty.php
|
MIT
|
public function assign(
string $variable,
ZephirVariable $symbolVariable,
CompiledExpression $resolvedExpr,
CompilationContext $compilationContext,
array $statement
): void {
$this->checkVariableInitialized($variable, $symbolVariable, $statement);
if ('variable' !== $symbolVariable->getType()) {
throw new CompilerException(
"Variable type '" . $symbolVariable->getType() . "' cannot be used as object",
$statement
);
}
$propertyName = $statement['property'];
if (!is_string($propertyName)) {
throw new CompilerException(
'Expected string to update object property, ' . gettype($propertyName) . ' received',
$statement
);
}
if (!$symbolVariable->isInitialized()) {
throw new CompilerException(
"Cannot mutate static property '" . $compilationContext->classDefinition->getCompleteName(
) . '::' . $propertyName . "' because it is not initialized",
$statement
);
}
if ($symbolVariable->hasAnyDynamicType('unknown')) {
throw CompilerException::cannotUseNonInitializedVariableAsObject($statement);
}
/**
* Trying to use a non-object dynamic variable as object
*/
if ($symbolVariable->hasDifferentDynamicType(['undefined', 'object', 'null'])) {
$compilationContext->logger->warning(
'Possible attempt to update property on non-object dynamic property',
['non-valid-objectupdate', $statement]
);
}
$compilationContext->headersManager->add('kernel/object');
switch ($resolvedExpr->getType()) {
case 'null':
$compilationContext->backend->updateProperty(
$symbolVariable,
$propertyName,
'null',
$compilationContext
);
break;
case 'int':
case 'long':
$tempVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->backend->assignLong($tempVariable, $resolvedExpr->getCode(), $compilationContext);
$compilationContext->backend->updateProperty(
$symbolVariable,
$propertyName,
$tempVariable,
$compilationContext
);
break;
case 'string':
$tempVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->backend->assignString(
$tempVariable,
$resolvedExpr->getCode(),
$compilationContext
);
$compilationContext->backend->updateProperty(
$symbolVariable,
$propertyName,
$tempVariable,
$compilationContext
);
break;
case 'bool':
if ('1' == $resolvedExpr->getBooleanCode()) {
$value = 'true';
} elseif ('0' == $resolvedExpr->getBooleanCode()) {
$value = 'false';
} else {
throw new Exception('?');
}
$compilationContext->backend->updateProperty(
$symbolVariable,
$propertyName,
$value,
$compilationContext
);
break;
case 'empty-array':
$tempVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->backend->initArray($tempVariable, $compilationContext);
$compilationContext->backend->updateProperty(
$symbolVariable,
$propertyName,
$tempVariable,
$compilationContext
);
break;
case 'array':
$variableVariable = $compilationContext->symbolTable->getVariableForRead(
$resolvedExpr->getCode(),
$compilationContext,
$statement
);
$compilationContext->backend->updateProperty(
$symbolVariable,
$propertyName,
$variableVariable,
$compilationContext
);
break;
case 'variable':
$variableVariable = $compilationContext->symbolTable->getVariableForRead(
$resolvedExpr->getCode(),
$compilationContext,
$statement
);
switch ($variableVariable->getType()) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
case 'char':
case 'uchar':
$tempVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->backend->assignLong($tempVariable, $variableVariable, $compilationContext);
$compilationContext->backend->updateProperty(
$symbolVariable,
$propertyName,
$tempVariable,
$compilationContext
);
break;
case 'bool':
$tempVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->backend->assignBool($tempVariable, $variableVariable, $compilationContext);
$compilationContext->backend->updateProperty(
$symbolVariable,
$propertyName,
$tempVariable,
$compilationContext
);
break;
case 'string':
case 'variable':
case 'array':
$compilationContext->backend->updateProperty(
$symbolVariable,
$propertyName,
$resolvedExpr,
$compilationContext
);
$this->checkVariableTemporal($symbolVariable);
break;
default:
throw new CompilerException('Unknown type ' . $variableVariable->getType(), $statement);
}
break;
default:
throw new CompilerException('Unknown type ' . $resolvedExpr->getType(), $statement);
}
}
|
Compiles foo->{"x"} = {expr}.
@throws CompilerException
@throws Exception
|
assign
|
php
|
zephir-lang/zephir
|
src/Statements/Let/ObjectDynamicStringProperty.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/ObjectDynamicStringProperty.php
|
MIT
|
protected function assignPropertyArrayMultipleIndex(
ZephirVariable $symbolVariable,
CompiledExpression $resolvedExpr,
CompilationContext $compilationContext,
array $statement
): void {
$property = $statement['property'];
$compilationContext->headersManager->add('kernel/object');
/**
* Create a temporal zval (if needed).
*/
$variableExpr = $this->_getResolvedArrayItem($resolvedExpr, $compilationContext);
/**
* Only string/variable/int.
*/
$offsetExprs = [];
foreach ($statement['index-expr'] as $indexExpr) {
$indexExpression = new Expression($indexExpr);
$resolvedIndex = $indexExpression->compile($compilationContext);
switch ($resolvedIndex->getType()) {
case 'string':
case 'int':
case 'uint':
case 'ulong':
case 'long':
case 'variable':
break;
default:
throw new CompilerException(
'Expression: '
. $resolvedIndex->getType()
. ' cannot be used as index without cast',
$statement['index-expr']
);
}
$offsetExprs[] = $resolvedIndex;
}
$compilationContext->backend->assignPropertyArrayMulti(
$symbolVariable,
$variableExpr,
$property,
$offsetExprs,
$compilationContext
);
$this->checkVariableTemporal($variableExpr);
}
|
Compiles x->y[a][b] = {expr} (multiple offset assignment).
@param ZephirVariable $symbolVariable
@param CompiledExpression $resolvedExpr
@param CompilationContext $compilationContext
@param array $statement
@throws ReflectionException
@throws Exception
|
assignPropertyArrayMultipleIndex
|
php
|
zephir-lang/zephir
|
src/Statements/Let/ObjectPropertyArrayIndex.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/ObjectPropertyArrayIndex.php
|
MIT
|
protected function assignPropertyArraySingleIndex(
ZephirVariable $symbolVariable,
CompiledExpression $resolvedExpr,
CompilationContext $compilationContext,
array $statement
): void {
$property = $statement['property'];
$compilationContext->headersManager->add('kernel/object');
/**
* Only string/variable/int.
*/
$indexExpression = new Expression($statement['index-expr'][0]);
$resolvedIndex = $indexExpression->compile($compilationContext);
switch ($resolvedIndex->getType()) {
case 'string':
case 'int':
case 'uint':
case 'ulong':
case 'long':
case 'variable':
break;
default:
throw new CompilerException(
'Expression: '
. $resolvedIndex->getType()
. ' cannot be used as index without cast',
$statement
);
}
if ('variable' == $resolvedIndex->getType()) {
$indexVariable = $compilationContext->symbolTable->getVariableForRead(
$resolvedIndex->getCode(),
$compilationContext,
$statement
);
switch ($indexVariable->getType()) {
case 'string':
case 'int':
case 'uint':
case 'ulong':
case 'long':
case 'variable':
case 'mixed':
break;
default:
throw new CompilerException(
'Variable: '
. $indexVariable->getType()
. ' cannot be used as index without cast',
$statement
);
}
if ('variable' == $indexVariable->getType()) {
if ($indexVariable->hasDifferentDynamicType(['undefined', 'int', 'string'])) {
$compilationContext->logger->warning(
'Possible attempt to use non string/long dynamic variable as array index',
['invalid-array-offset', $statement]
);
}
}
}
switch ($resolvedIndex->getType()) {
case 'int':
case 'uint':
case 'long':
$indexVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext,
$statement
);
$compilationContext->backend->assignLong(
$indexVariable,
$resolvedIndex->getCode(),
$compilationContext
);
break;
case 'string':
$indexVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext,
$statement
);
$compilationContext->backend->assignString(
$indexVariable,
$resolvedIndex->getCode(),
$compilationContext
);
break;
case 'variable':
$variableIndex = $compilationContext->symbolTable->getVariableForRead(
$resolvedIndex->getCode(),
$compilationContext,
$statement['index-expr']
);
switch ($variableIndex->getType()) {
case 'int':
case 'uint':
case 'long':
$indexVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext,
$statement
);
$compilationContext->backend->assignLong($indexVariable, $variableIndex, $compilationContext);
break;
}
break;
}
/**
* Check if the variable to update is defined
*/
if ('this' == $symbolVariable->getRealName()) {
$classDefinition = $compilationContext->classDefinition;
$this->checkClassHasProperty(
$classDefinition,
$property,
$statement
);
} else {
/**
* If we know the class related to a variable we could check if the property
* is defined on that class
*/
if ($symbolVariable->hasAnyDynamicType('object')) {
$classType = current($symbolVariable->getClassTypes());
$compiler = $compilationContext->compiler;
if ($classType !== false && $compiler->isClass($classType)) {
$classDefinition = $compiler->getClassDefinition($classType);
if (!$classDefinition) {
throw new CompilerException(
'Cannot locate class definition for class: ' . $classType,
$statement
);
}
$this->checkClassHasProperty(
$classDefinition,
$property,
$statement,
$classType
);
}
}
}
switch ($indexVariable->getType()) {
case 'variable':
case 'mixed':
case 'string':
switch ($resolvedExpr->getType()) {
case 'null':
$compilationContext->backend->assignArrayProperty(
$symbolVariable,
$property,
$indexVariable,
'null',
$compilationContext
);
break;
case 'bool':
$booleanCode = $resolvedExpr->getBooleanCode();
if ('1' == $booleanCode) {
$compilationContext->backend->assignArrayProperty(
$symbolVariable,
$property,
$indexVariable,
'true',
$compilationContext
);
} elseif ('0' == $booleanCode) {
$compilationContext->backend->assignArrayProperty(
$symbolVariable,
$property,
$indexVariable,
'false',
$compilationContext
);
} else {
$tempVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->backend->assignBool($tempVariable, $booleanCode, $compilationContext);
$compilationContext->backend->assignArrayProperty(
$symbolVariable,
$property,
$indexVariable,
$tempVariable,
$compilationContext
);
}
break;
case 'int':
case 'long':
case 'uint':
$tempVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->backend->assignLong(
$tempVariable,
$resolvedExpr->getCode(),
$compilationContext
);
$compilationContext->backend->assignArrayProperty(
$symbolVariable,
$property,
$indexVariable,
$tempVariable,
$compilationContext
);
break;
case 'char':
$tempVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->backend->assignLong(
$tempVariable,
'\'' . $resolvedExpr->getCode() . '\'',
$compilationContext
);
$compilationContext->backend->assignArrayProperty(
$symbolVariable,
$property,
$indexVariable,
$tempVariable,
$compilationContext
);
break;
case 'double':
$tempVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->backend->assignDouble(
$tempVariable,
$resolvedExpr->getCode(),
$compilationContext
);
$compilationContext->backend->assignArrayProperty(
$symbolVariable,
$property,
$indexVariable,
$tempVariable,
$compilationContext
);
break;
case 'string':
$tempVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->backend->assignString(
$tempVariable,
$resolvedExpr->getCode(),
$compilationContext
);
$compilationContext->backend->assignArrayProperty(
$symbolVariable,
$property,
$indexVariable,
$tempVariable,
$compilationContext
);
break;
case 'array':
$variableExpr = $compilationContext->symbolTable->getVariableForRead(
$resolvedExpr->getCode(),
$compilationContext,
$statement['index-expr']
);
$compilationContext->backend->assignArrayProperty(
$symbolVariable,
$property,
$indexVariable,
$variableExpr,
$compilationContext
);
break;
case 'variable':
$variableExpr = $compilationContext->symbolTable->getVariableForRead(
$resolvedExpr->getCode(),
$compilationContext,
$statement['index-expr']
);
switch ($variableExpr->getType()) {
case 'bool':
$tempVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->backend->assignBool(
$tempVariable,
$variableExpr,
$compilationContext
);
$compilationContext->backend->assignArrayProperty(
$symbolVariable,
$property,
$indexVariable,
$tempVariable,
$compilationContext
);
break;
case 'int':
case 'long':
$tempVariable = $compilationContext->symbolTable->getTempVariableForWrite(
'variable',
$compilationContext
);
$compilationContext->backend->assignLong(
$tempVariable,
$variableExpr,
$compilationContext
);
$compilationContext->backend->assignArrayProperty(
$symbolVariable,
$property,
$indexVariable,
$tempVariable,
$compilationContext
);
break;
case 'variable':
case 'mixed':
case 'string':
case 'array':
$compilationContext->backend->assignArrayProperty(
$symbolVariable,
$property,
$indexVariable,
$variableExpr,
$compilationContext
);
$this->checkVariableTemporal($variableExpr);
break;
default:
throw new CompilerException(
'Cannot update variable type: ' . $variableExpr->getType(),
$statement
);
}
break;
default:
throw new CompilerException(
'Variable index: '
. $indexVariable->getType()
. ' cannot be updated into array property',
$statement
);
}
break;
default:
throw new CompilerException(
'Index: '
. $resolvedIndex->getType()
. ' cannot be updated into array property',
$statement
);
}
}
|
Compiles x->y[z] = {expr} (single offset assignment).
@throws Exception
@throws ReflectionException
|
assignPropertyArraySingleIndex
|
php
|
zephir-lang/zephir
|
src/Statements/Let/ObjectPropertyArrayIndex.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/ObjectPropertyArrayIndex.php
|
MIT
|
public function assign(
$variable,
ZephirVariable $symbolVariable,
CompiledExpression $resolvedExpr,
CompilationContext $compilationContext,
$statement
): void {
$this->checkVariableInitialized($variable, $symbolVariable, $statement);
if (!$symbolVariable->isVariable()) {
throw new CompilerException(
'Attempt to use variable type: ' . $symbolVariable->getType() . ' as object',
$statement
);
}
$this->_assignPropertyArrayMultipleIndex($symbolVariable, $resolvedExpr, $compilationContext, $statement);
}
|
Compiles x->y[z][] = foo.
@throws Exception
@throws ReflectionException
|
assign
|
php
|
zephir-lang/zephir
|
src/Statements/Let/ObjectPropertyArrayIndexAppend.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/ObjectPropertyArrayIndexAppend.php
|
MIT
|
protected function _assignPropertyArrayMultipleIndex(
ZephirVariable $symbolVariable,
CompiledExpression $resolvedExpr,
CompilationContext $compilationContext,
array $statement
): void {
$property = $statement['property'];
$compilationContext->headersManager->add('kernel/object');
/**
* Create a temporal zval (if needed).
*/
$variableExpr = $this->_getResolvedArrayItem($resolvedExpr, $compilationContext);
if (count($statement['index-expr']) > 16) {
throw new CompilerException('Too many array indexes', $statement);
}
/**
* Only string/variable/int.
*/
$offsetExprs = [];
foreach ($statement['index-expr'] as $indexExpr) {
$indexExpression = new Expression($indexExpr);
$resolvedIndex = $indexExpression->compile($compilationContext);
switch ($resolvedIndex->getType()) {
case 'string':
case 'int':
case 'uint':
case 'ulong':
case 'long':
case 'variable':
break;
default:
throw new CompilerException(
'Expression: '
. $resolvedIndex->getType()
. ' cannot be used as index without cast',
$statement
);
}
$offsetExprs[] = $resolvedIndex;
}
/**
* Check if the property to update is defined
*/
if ('this' == $symbolVariable->getRealName()) {
$classDefinition = $compilationContext->classDefinition;
$this->checkClassHasProperty(
$classDefinition,
$property,
$statement
);
} else {
/**
* If we know the class related to a variable we could check if the property
* is defined on that class
*/
if ($symbolVariable->hasAnyDynamicType('object')) {
$classType = current($symbolVariable->getClassTypes());
$compiler = $compilationContext->compiler;
if ($compiler->isClass($classType)) {
$classDefinition = $compiler->getClassDefinition($classType);
if (!$classDefinition) {
throw new CompilerException(
'Cannot locate class definition for class: ' . $classType,
$statement
);
}
$this->checkClassHasProperty(
$classDefinition,
$property,
$statement,
$classType
);
}
}
}
$offsetExprs[] = 'a';
$compilationContext->backend->assignPropertyArrayMulti(
$symbolVariable,
$variableExpr,
$property,
$offsetExprs,
$compilationContext
);
}
|
Compiles x->y[a][b][] = {expr} (multiple offset assignment).
@throws ReflectionException
@throws Exception
|
_assignPropertyArrayMultipleIndex
|
php
|
zephir-lang/zephir
|
src/Statements/Let/ObjectPropertyArrayIndexAppend.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/ObjectPropertyArrayIndexAppend.php
|
MIT
|
public function assignStatic(
$className,
$property,
CompiledExpression $resolvedExpr,
CompilationContext $compilationContext,
array $statement
): void {
$compiler = $compilationContext->compiler;
if (!in_array($className, ['self', 'static', 'parent'])) {
$className = $compilationContext->getFullName($className);
if ($compiler->isClass($className)) {
$classDefinition = $compiler->getClassDefinition($className);
} else {
if ($compiler->isBundledClass($className)) {
$classDefinition = $compiler->getInternalClassDefinition($className);
} else {
throw new CompilerException("Cannot locate class '" . $className . "'", $statement);
}
}
} else {
if (in_array($className, ['self', 'static'])) {
$classDefinition = $compilationContext->classDefinition;
} else {
if ('parent' == $className) {
$classDefinition = $compilationContext->classDefinition;
$extendsClass = $classDefinition->getExtendsClass();
if (!$extendsClass) {
throw new CompilerException(
'Cannot assign static property "'
. $property
. '" on parent because class '
. $classDefinition->getCompleteName()
. ' does not extend any class',
$statement
);
} else {
$classDefinition = $classDefinition->getExtendsClassDefinition();
}
}
}
}
$this->checkClassHasProperty(
$classDefinition,
$property,
$statement
);
/** @var Property $propertyDefinition */
$propertyDefinition = $classDefinition->getProperty($property);
$this->checkAccessNonStaticProperty(
$propertyDefinition,
$classDefinition,
$property,
$statement
);
if ($propertyDefinition->isPrivate()) {
if ($classDefinition != $compilationContext->classDefinition) {
throw new CompilerException(
"Cannot access private static property '"
. $classDefinition->getCompleteName()
. '::'
. $property
. "' out of its declaring context",
$statement
);
}
}
$compilationContext->headersManager->add('kernel/object');
$classEntry = $classDefinition->getClassEntry($compilationContext);
$this->assignStaticPropertyArrayMultipleIndex(
$classEntry,
$resolvedExpr,
$compilationContext,
$statement,
);
}
|
Compiles ClassName::foo[index] = {expr}.
@throws Exception
@throws ReflectionException
|
assignStatic
|
php
|
zephir-lang/zephir
|
src/Statements/Let/StaticPropertyAppend.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/StaticPropertyAppend.php
|
MIT
|
public function assignStatic(
string $className,
string $property,
CompiledExpression $resolvedExpr,
CompilationContext $compilationContext,
array $statement
): void {
$classDefinition = $compilationContext->classLookup($className);
$method = $this->methodName;
if (!$propertyDefinition = $classDefinition->getProperty($property)) {
throw CompilerException::classDoesNotHaveProperty(
$classDefinition->getCompleteName(),
$property,
$statement
);
}
$this->checkAccessNonStaticProperty(
$propertyDefinition,
$classDefinition,
$property,
$statement
);
if ($propertyDefinition->isPrivate()) {
if ($classDefinition->getCompleteName() != $compilationContext->classDefinition->getCompleteName()) {
throw new CompilerException(
sprintf(
"Cannot access private static property '%s::%s out of its declaring context",
$classDefinition->getCompleteName(),
$property
),
$statement
);
}
}
$codePrinter = $compilationContext->codePrinter;
$compilationContext->headersManager->add('kernel/object');
try {
$classEntry = $classDefinition->getClassEntry($compilationContext);
} catch (Exception $e) {
throw new CompilerException($e->getMessage(), $statement, $e->getCode(), $e);
}
switch ($resolvedExpr->getType()) {
case 'null':
$compilationContext->backend->updateStaticProperty($classEntry, $property, 'null', $compilationContext);
break;
case 'int':
case 'uint':
case 'long':
$tempVariable = $compilationContext->symbolTable->getTempNonTrackedVariable(
'variable',
$compilationContext,
true
);
$compilationContext->backend->assignLong(
$tempVariable,
$resolvedExpr->getBooleanCode(),
$compilationContext
);
$compilationContext->backend->$method(
$classEntry,
$property,
$tempVariable,
$compilationContext
);
$this->checkVariableTemporal($tempVariable);
break;
case 'char':
case 'uchar':
$tempVariable = $compilationContext->symbolTable->getTempNonTrackedVariable(
'variable',
$compilationContext,
true
);
$compilationContext->backend->assignLong(
$tempVariable,
'\'' . $resolvedExpr->getCode() . '\'',
$compilationContext
);
$compilationContext->backend->$method(
$classEntry,
$property,
$tempVariable,
$compilationContext
);
$this->checkVariableTemporal($tempVariable);
break;
case 'double':
$tempVariable = $compilationContext->symbolTable->getTempNonTrackedVariable(
'variable',
$compilationContext,
true
);
$compilationContext->backend->assignDouble(
$tempVariable,
$resolvedExpr->getCode(),
$compilationContext
);
$compilationContext->backend->$method(
$classEntry,
$property,
$tempVariable,
$compilationContext
);
$this->checkVariableTemporal($tempVariable);
break;
case 'string':
$tempVariable = $this->processStringType(
$resolvedExpr,
$compilationContext,
$statement
);
$tempVariable->initVariant($compilationContext);
if ($resolvedExpr->getCode()) {
$compilationContext->backend->assignString(
$tempVariable,
$resolvedExpr->getCode(),
$compilationContext
);
} else {
$codePrinter->output('ZVAL_EMPTY_STRING(' . $tempVariable->getName() . ');');
}
$this->checkVariableTemporal($tempVariable);
$compilationContext->backend->$method(
$classEntry,
$property,
$tempVariable,
$compilationContext
);
break;
case 'bool':
if ('1' == $resolvedExpr->getBooleanCode()) {
$compilationContext->backend->$method(
$classEntry,
$property,
'true',
$compilationContext
);
} else {
if ('0' == $resolvedExpr->getBooleanCode()) {
$compilationContext->backend->$method(
$classEntry,
$property,
'false',
$compilationContext
);
} else {
$codePrinter->output('if (' . $resolvedExpr->getBooleanCode() . ') {');
$codePrinter->increaseLevel();
$compilationContext->backend->$method(
$classEntry,
$property,
'true',
$compilationContext
);
$codePrinter->decreaseLevel();
$codePrinter->output('} else {');
$codePrinter->increaseLevel();
$compilationContext->backend->$method(
$classEntry,
$property,
'false',
$compilationContext
);
$codePrinter->decreaseLevel();
$codePrinter->output('}');
}
}
break;
case 'empty-array':
$tempVariable = $compilationContext->symbolTable->getTempNonTrackedVariable(
'variable',
$compilationContext,
true
);
$compilationContext->backend->initArray($tempVariable, $compilationContext);
$compilationContext->backend->$method(
$classEntry,
$property,
$tempVariable,
$compilationContext
);
$this->checkVariableTemporal($tempVariable);
break;
case 'array':
$compilationContext->backend->$method(
$classEntry,
$property,
$resolvedExpr,
$compilationContext
);
break;
case 'variable':
$variableVariable = $compilationContext->symbolTable->getVariableForRead(
$resolvedExpr->getCode(),
$compilationContext,
$statement
);
switch ($variableVariable->getType()) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
case 'char':
case 'uchar':
$this->processVariableIntType(
$statement,
$variableVariable
);
$tempVariable = $compilationContext->symbolTable->getTempNonTrackedVariable(
'variable',
$compilationContext,
true
);
$compilationContext->backend->assignLong($tempVariable, $variableVariable, $compilationContext);
$compilationContext->backend->$method(
$classEntry,
$property,
$tempVariable,
$compilationContext
);
$this->checkVariableTemporal($tempVariable);
break;
case 'double':
$this->processVariableDoubleType(
$statement,
$variableVariable
);
$tempVariable = $compilationContext->symbolTable->getTempNonTrackedVariable(
'variable',
$compilationContext,
true
);
$compilationContext->backend->assignDouble(
$tempVariable,
$variableVariable,
$compilationContext
);
$compilationContext->backend->$method(
$classEntry,
$property,
$tempVariable,
$compilationContext
);
$this->checkVariableTemporal($tempVariable);
break;
case 'bool':
$tempVariable = $compilationContext->symbolTable->getTempNonTrackedVariable(
'variable',
$compilationContext,
true
);
$compilationContext->backend->assignBool($tempVariable, $variableVariable, $compilationContext);
$compilationContext->backend->$method(
$classEntry,
$property,
$tempVariable,
$compilationContext
);
$this->checkVariableTemporal($tempVariable);
break;
default:
$this->processDefaultType(
$compilationContext,
$statement,
$classEntry,
$property,
$variableVariable
);
break;
}
break;
default:
throw new CompilerException('Unknown type ' . $resolvedExpr->getType(), $statement);
}
}
|
Compiles ClassName::foo = {expr}.
@throws CompilerException
@throws IllegalOperationException
|
assignStatic
|
php
|
zephir-lang/zephir
|
src/Statements/Let/StaticPropertySub.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/StaticPropertySub.php
|
MIT
|
public function assign(
string $variable,
ZephirVariable $symbolVariable,
CompiledExpression $resolvedExpr,
ReadDetector $readDetector,
CompilationContext $compilationContext,
array $statement
): void {
$this->checkVariableReadOnly($variable, $symbolVariable, $statement);
$codePrinter = $compilationContext->codePrinter;
/**
* Only initialize variables if it's direct assignment
*/
if ('assign' == $statement['operator']) {
$symbolVariable->setIsInitialized(true, $compilationContext);
} else {
$this->checkVariableInitialized($variable, $symbolVariable, $statement);
}
/**
* Set the assigned value to the variable as a CompiledExpression
* We could use this expression for further analysis
*/
$symbolVariable->setPossibleValue($resolvedExpr, $compilationContext);
$type = $symbolVariable->getType();
switch ($type) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
case 'char':
case 'uchar':
$this->doNumericAssignment(
$codePrinter,
$resolvedExpr,
$variable,
$statement,
$compilationContext
);
break;
case 'double':
$this->doDoubleAssignment(
$codePrinter,
$resolvedExpr,
$variable,
$statement,
$compilationContext
);
break;
case 'array':
$this->doArrayAssignment(
$codePrinter,
$resolvedExpr,
$symbolVariable,
$variable,
$statement,
$compilationContext
);
break;
case 'string':
$this->doStringAssignment(
$codePrinter,
$resolvedExpr,
$symbolVariable,
$variable,
$statement,
$compilationContext
);
break;
case 'bool':
$this->doBoolAssignment(
$codePrinter,
$resolvedExpr,
$variable,
$statement,
$compilationContext
);
break;
case 'variable':
case 'mixed':
$this->doVariableAssignment(
$codePrinter,
$resolvedExpr,
$symbolVariable,
$variable,
$statement,
$compilationContext,
$readDetector
);
break;
default:
throw new CompilerException('Unknown type: ' . $type, $statement);
}
}
|
Compiles foo = {expr}
Changes the value of a mutable variable.
@throws CompilerException
|
assign
|
php
|
zephir-lang/zephir
|
src/Statements/Let/Variable.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/Variable.php
|
MIT
|
private function doArrayAssignment(
Printer $codePrinter,
CompiledExpression $resolvedExpr,
ZephirVariable $symbolVariable,
string $variable,
array $statement,
CompilationContext $compilationContext
): void {
switch ($resolvedExpr->getType()) {
case 'variable':
case 'array':
$this->doArrayAssignmentProcess(
$statement,
$resolvedExpr,
$variable,
$symbolVariable,
$compilationContext,
$codePrinter
);
break;
default:
throw new CompilerException(
"Cannot '" . $statement['operator'] . "' " . $resolvedExpr->getType() . ' for array type',
$resolvedExpr->getOriginal()
);
}
}
|
Performs array assignment.
@throws CompilerException
@throws IllegalOperationException
|
doArrayAssignment
|
php
|
zephir-lang/zephir
|
src/Statements/Let/Variable.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/Variable.php
|
MIT
|
private function doBoolAssignment(
Printer $codePrinter,
CompiledExpression $resolvedExpr,
string $variable,
array $statement,
CompilationContext $compilationContext
): void {
switch ($resolvedExpr->getType()) {
case 'null':
switch ($statement['operator']) {
case 'assign':
$codePrinter->output($variable . ' = 0;');
break;
default:
throw new IllegalOperationException($statement, $resolvedExpr);
}
break;
case 'int':
case 'uint':
case 'long':
case 'ulong':
switch ($statement['operator']) {
case 'assign':
$codePrinter->output($variable . ' = ((' . $resolvedExpr->getCode() . ') ? 1 : 0);');
break;
default:
throw new IllegalOperationException($statement, $resolvedExpr);
}
break;
case 'double':
switch ($statement['operator']) {
case 'assign':
$codePrinter->output($variable . ' = ((' . $resolvedExpr->getCode() . ' != 0.0) ? 1 : 0);');
break;
default:
throw new IllegalOperationException($statement, $resolvedExpr);
}
break;
case 'char':
case 'uchar':
switch ($statement['operator']) {
case 'assign':
$codePrinter->output($variable . ' = ((\'' . $resolvedExpr->getCode() . '\') ? 1 : 0);');
break;
default:
throw new IllegalOperationException($statement, $resolvedExpr);
}
break;
case 'bool':
switch ($statement['operator']) {
case 'assign':
$codePrinter->output($variable . ' = ' . $resolvedExpr->getBooleanCode() . ';');
break;
default:
throw new IllegalOperationException($statement, $resolvedExpr);
}
break;
case 'variable':
$itemVariable = $compilationContext->symbolTable->getVariableForRead(
$resolvedExpr->getCode(),
$compilationContext,
$statement
);
switch ($itemVariable->getType()) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
switch ($statement['operator']) {
case 'assign':
$codePrinter->output($variable . ' = ((' . $itemVariable->getName() . ') ? 1 : 0);');
break;
default:
throw new IllegalOperationException($statement, $itemVariable);
}
break;
case 'double':
switch ($statement['operator']) {
case 'assign':
$codePrinter->output(
$variable . ' = ((' . $itemVariable->getName() . ' != 0.0) ? 1 : 0);'
);
break;
default:
throw new IllegalOperationException($statement, $itemVariable);
}
break;
case 'bool':
switch ($statement['operator']) {
case 'assign':
$codePrinter->output($variable . ' = ' . $itemVariable->getName() . ';');
break;
default:
throw new IllegalOperationException($statement, $itemVariable);
}
break;
case 'variable':
case 'mixed':
case 'string':
case 'array':
switch ($statement['operator']) {
case 'assign':
$compilationContext->headersManager->add('kernel/operators');
$codePrinter->output(
$variable . ' = zephir_is_true(' . $compilationContext->backend->getVariableCode(
$itemVariable
) . ');'
);
break;
default:
throw new IllegalOperationException($statement, $itemVariable);
}
break;
default:
throw new CompilerException('Cannot assign variable: ' . $itemVariable->getType(), $statement);
}
break;
default:
throw new CompilerException('Unknown type: ' . $resolvedExpr->getType(), $statement);
}
}
|
Performs boolean assignment.
@param Printer $codePrinter
@param CompiledExpression $resolvedExpr
@param string $variable
@param array $statement
@param CompilationContext $compilationContext
@throws CompilerException
@throws IllegalOperationException
|
doBoolAssignment
|
php
|
zephir-lang/zephir
|
src/Statements/Let/Variable.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/Variable.php
|
MIT
|
private function doDoubleAssignment(
Printer $codePrinter,
CompiledExpression $resolvedExpr,
string $variable,
array $statement,
CompilationContext $compilationContext
): void {
switch ($resolvedExpr->getType()) {
case 'null':
$this->doNumberAssignmentNull(
$statement,
$codePrinter,
$variable,
$resolvedExpr,
'0.0'
);
break;
case 'int':
case 'uint':
case 'long':
case 'ulong':
$this->doNumericAssignmentLong(
$statement,
$codePrinter,
$variable,
$resolvedExpr,
'(double)'
);
break;
case 'double':
$this->doNumericAssignmentLong(
$statement,
$codePrinter,
$variable,
$resolvedExpr
);
break;
case 'bool':
switch ($statement['operator']) {
case 'assign':
$codePrinter->output($variable . ' = ' . $resolvedExpr->getBooleanCode() . ';');
break;
case 'add-assign':
$codePrinter->output($variable . ' += ' . $resolvedExpr->getBooleanCode() . ';');
break;
case 'sub-assign':
$codePrinter->output($variable . ' -= ' . $resolvedExpr->getBooleanCode() . ';');
break;
case 'mul-assign':
$codePrinter->output($variable . ' *= ' . $resolvedExpr->getBooleanCode() . ';');
break;
default:
throw new IllegalOperationException($statement, $resolvedExpr);
}
break;
case 'variable':
$itemVariable = $compilationContext->symbolTable->getVariableForRead(
$resolvedExpr->getCode(),
$compilationContext,
$statement
);
switch ($itemVariable->getType()) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
case 'bool':
$this->doNumericAssignmentVar(
$statement,
$codePrinter,
$variable,
$itemVariable,
'(double)'
);
break;
case 'double':
$this->doNumericAssignmentVar(
$statement,
$codePrinter,
$variable,
$itemVariable
);
break;
case 'variable':
case 'mixed':
$compilationContext->headersManager->add('kernel/operators');
$this->processDoNumericAssignmentMixed(
$compilationContext,
$itemVariable,
$statement,
$codePrinter,
$variable,
$itemVariable
);
break;
default:
throw new CompilerException('Unknown type: ' . $itemVariable->getType(), $statement);
}
break;
default:
throw new CompilerException('Unknown type ' . $resolvedExpr->getType(), $statement);
}
}
|
Performs double assignment.
@throws CompilerException
@throws IllegalOperationException
|
doDoubleAssignment
|
php
|
zephir-lang/zephir
|
src/Statements/Let/Variable.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/Variable.php
|
MIT
|
private function doNumberAssignmentNull(
array $statement,
Printer $codePrinter,
string $variable,
CompiledExpression $resolvedExpr,
string $value
): void {
switch ($statement['operator']) {
case 'assign':
$codePrinter->output($variable . ' = ' . $value . ';');
break;
case 'add-assign':
$codePrinter->output($variable . ' += ' . $value . ';');
break;
case 'sub-assign':
$codePrinter->output($variable . ' -= ' . $value . ';');
break;
case 'mul-assign':
$codePrinter->output($variable . ' *= ' . $value . ';');
break;
default:
throw new IllegalOperationException($statement, $resolvedExpr);
}
}
|
@param array $statement
@param Printer $codePrinter
@param string $variable
@param CompiledExpression $resolvedExpr
@param string $value
@return void
|
doNumberAssignmentNull
|
php
|
zephir-lang/zephir
|
src/Statements/Let/Variable.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/Variable.php
|
MIT
|
private function doNumericAssignment(
Printer $codePrinter,
CompiledExpression $resolvedExpr,
string $variable,
array $statement,
CompilationContext $compilationContext
): void {
switch ($resolvedExpr->getType()) {
case 'null':
$this->doNumberAssignmentNull(
$statement,
$codePrinter,
$variable,
$resolvedExpr,
'0'
);
break;
case 'int':
case 'uint':
case 'long':
case 'ulong':
$operator = match ($statement['operator']) {
'assign' => ' = ',
'add-assign' => ' += ',
'sub-assign' => ' -= ',
'mul-assign' => ' *= ',
'div-assign' => ' /= ',
'mod-assign' => ' %= ',
default => throw new IllegalOperationException($statement, $resolvedExpr)
};
$codePrinter->output($variable . $operator . $resolvedExpr->getCode() . ';');
break;
case 'char':
case 'uchar':
$operator = match ($statement['operator']) {
'assign' => ' = ',
'add-assign' => ' += ',
'sub-assign' => ' -= ',
'mul-assign' => ' *= ',
default => throw new IllegalOperationException($statement, $resolvedExpr)
};
$codePrinter->output($variable . $operator . '\'' . $resolvedExpr->getCode() . '\';');
break;
case 'double':
$this->doNumericAssignmentLong(
$statement,
$codePrinter,
$variable,
$resolvedExpr,
'(long)'
);
break;
case 'bool':
$operator = match ($statement['operator']) {
'assign' => ' = ',
'add-assign' => ' += ',
'sub-assign' => ' -= ',
default => throw new IllegalOperationException($statement, $resolvedExpr)
};
$codePrinter->output($variable . $operator . $resolvedExpr->getBooleanCode() . ';');
break;
case 'variable':
$itemVariable = $compilationContext->symbolTable->getVariableForRead(
$resolvedExpr->getCode(),
$compilationContext,
$statement
);
switch ($itemVariable->getType()) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
case 'bool':
case 'char':
case 'uchar':
$operator = match ($statement['operator']) {
'assign' => ' = ',
'add-assign' => ' += ',
'sub-assign' => ' -= ',
'mul-assign' => ' *= ',
'div-assign' => ' /= ',
'mod-assign' => ' %= ',
default => throw new IllegalOperationException($statement, $itemVariable)
};
$codePrinter->output($variable . $operator . $itemVariable->getName() . ';');
break;
case 'double':
$this->doNumericAssignmentVar(
$statement,
$codePrinter,
$variable,
$itemVariable,
'(long)'
);
break;
case 'variable':
case 'mixed':
$compilationContext->headersManager->add('kernel/operators');
$exprVariable = $compilationContext->symbolTable->getVariableForWrite(
$resolvedExpr->resolve(null, $compilationContext),
$compilationContext
);
$this->processDoNumericAssignmentMixed(
$compilationContext,
$exprVariable,
$statement,
$codePrinter,
$variable,
$itemVariable
);
break;
default:
throw new CompilerException('Unknown type: ' . $itemVariable->getType(), $statement);
}
break;
default:
throw new CompilerException(
"Value type '" . $resolvedExpr->getType() . "' cannot be assigned to variable: int",
$statement
);
}
}
|
Performs numeric assignment.
@throws CompilerException
@throws IllegalOperationException
|
doNumericAssignment
|
php
|
zephir-lang/zephir
|
src/Statements/Let/Variable.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/Variable.php
|
MIT
|
private function doNumericAssignmentLong(
array $statement,
Printer $codePrinter,
string $variable,
CompiledExpression $resolvedExpr,
string $cast = ''
): void {
switch ($statement['operator']) {
case 'assign':
$codePrinter->output(
$variable . ' = ' . $cast
. ' (' . $resolvedExpr->getCode() . ');'
);
break;
case 'add-assign':
$codePrinter->output(
$variable . ' += ' . $cast
. ' (' . $resolvedExpr->getCode() . ');'
);
break;
case 'sub-assign':
$codePrinter->output(
$variable . ' -= ' . $cast
. ' (' . $resolvedExpr->getCode() . ');'
);
break;
case 'mul-assign':
$codePrinter->output(
$variable . ' *= ' . $cast
. ' (' . $resolvedExpr->getCode() . ');'
);
break;
default:
throw new IllegalOperationException($statement, $resolvedExpr);
}
}
|
@param array $statement
@param Printer $codePrinter
@param string $variable
@param CompiledExpression $resolvedExpr
@param string $cast
@return void
|
doNumericAssignmentLong
|
php
|
zephir-lang/zephir
|
src/Statements/Let/Variable.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/Variable.php
|
MIT
|
private function doStringAssignment(
Printer $codePrinter,
CompiledExpression $resolvedExpr,
ZephirVariable $symbolVariable,
string $variable,
array $statement,
CompilationContext $compilationContext
): void {
switch ($resolvedExpr->getType()) {
case 'null':
switch ($statement['operator']) {
case 'assign':
$symbolVariable->initVariant($compilationContext);
$compilationContext->backend->assignString($symbolVariable, null, $compilationContext);
break;
default:
throw new IllegalOperationException($statement, $resolvedExpr);
}
break;
case 'int':
case 'uint':
case 'long':
case 'ulong':
switch ($statement['operator']) {
case 'assign':
$symbolVariable->initVariant($compilationContext);
$compilationContext->backend->assignString(
$symbolVariable,
$resolvedExpr->getCode(),
$compilationContext
);
break;
case 'concat-assign':
$codePrinter->output(
'zephir_concat_self_str(&' . $variable . ', "' . $resolvedExpr->getCode(
) . '", sizeof("' . $resolvedExpr->getCode() . '") - 1);'
);
break;
default:
throw new IllegalOperationException($statement, $resolvedExpr);
}
break;
case 'string':
switch ($statement['operator']) {
case 'assign':
$symbolVariable->initVariant($compilationContext);
if ($resolvedExpr->getCode()) {
$compilationContext->backend->assignString(
$symbolVariable,
$resolvedExpr->getCode(),
$compilationContext
);
} else {
$compilationContext->backend->assignString($symbolVariable, null, $compilationContext);
}
break;
case 'concat-assign':
$codePrinter->output(
'zephir_concat_self_str(&' . $variable . ', "' . $resolvedExpr->getCode(
) . '", sizeof("' . $resolvedExpr->getCode() . '") - 1);'
);
break;
default:
throw new IllegalOperationException($statement, $resolvedExpr);
}
break;
case 'char':
case 'uchar':
switch ($statement['operator']) {
case 'assign':
$symbolVariable->initVariant($compilationContext);
if ($resolvedExpr->getCode()) {
$compilationContext->backend->assignString(
$symbolVariable,
$resolvedExpr->getCode(),
$compilationContext
);
} else {
$compilationContext->backend->assignString($symbolVariable, null, $compilationContext);
}
break;
case 'concat-assign':
$compilationContext->headersManager->add('kernel/operators');
$codePrinter->output(
'zephir_concat_self_str(&' . $variable . ', "' . $resolvedExpr->getCode(
) . '", sizeof("' . $resolvedExpr->getCode() . '") - 1);'
);
break;
default:
throw new IllegalOperationException($statement, $resolvedExpr);
}
break;
case 'variable':
$itemVariable = $compilationContext->symbolTable->getVariableForRead(
$resolvedExpr->getCode(),
$compilationContext,
$statement
);
switch ($itemVariable->getType()) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
switch ($statement['operator']) {
case 'assign':
$symbolVariable->initVariant($compilationContext);
$compilationContext->headersManager->add('kernel/string');
// FIXME: Most likely this code is outdated and no longer works.
$codePrinter->output(
'Z_STRLEN_P(' . $variable . ') = zephir_spprintf(&Z_STRVAL_P(' . $variable . '), 0, "%ld", ' . $itemVariable->getName(
) . ');'
);
$codePrinter->output('Z_TYPE_P(' . $variable . ') = IS_STRING;');
break;
case 'concat-assign':
$compilationContext->headersManager->add('kernel/operators');
$codePrinter->output(
'zephir_concat_self_long(&' . $variable . ', ' . $itemVariable->getName() . ');'
);
break;
default:
throw new IllegalOperationException($statement, $itemVariable);
}
break;
case 'char':
case 'uchar':
switch ($statement['operator']) {
case 'assign':
$symbolVariable->initVariant($compilationContext);
$compilationContext->headersManager->add('kernel/string');
// FIXME: Most likely this code is outdated and no longer works.
$codePrinter->output(
'Z_STRLEN_P(' . $variable . ') = zephir_spprintf(&Z_STRVAL_P(' . $variable . '), 0, "%c", ' . $itemVariable->getName(
) . ');'
);
$codePrinter->output('Z_TYPE_P(' . $variable . ') = IS_STRING;');
break;
case 'concat-assign':
$compilationContext->headersManager->add('kernel/operators');
$codePrinter->output(
'zephir_concat_self_char(&' . $variable . ', ' . $itemVariable->getName() . ');'
);
break;
default:
throw new IllegalOperationException($statement, $itemVariable);
}
break;
case 'string':
switch ($statement['operator']) {
case 'assign':
$symbolVariable->setMustInitNull(true);
$symbolVariable->increaseVariantIfNull();
$compilationContext->symbolTable->mustGrownStack(true);
if ($variable != $itemVariable->getName()) {
$compilationContext->backend->copyOnWrite(
$symbolVariable,
$itemVariable,
$compilationContext
);
}
break;
case 'concat-assign':
$compilationContext->headersManager->add('kernel/operators');
$compilationContext->backend->concatSelf(
$symbolVariable,
$itemVariable,
$compilationContext
);
break;
default:
throw new IllegalOperationException($statement, $itemVariable);
}
break;
case 'variable':
case 'mixed':
switch ($statement['operator']) {
case 'assign':
$symbolVariable->setMustInitNull(true);
$compilationContext->symbolTable->mustGrownStack(true);
$compilationContext->headersManager->add('kernel/operators');
$codePrinter->output(
'zephir_get_strval(' . $compilationContext->backend->getVariableCode(
$symbolVariable
) . ', ' . $compilationContext->backend->getVariableCode($itemVariable) . ');'
);
break;
case 'concat-assign':
$compilationContext->headersManager->add('kernel/operators');
$compilationContext->backend->concatSelf(
$symbolVariable,
$itemVariable,
$compilationContext
);
break;
default:
throw new IllegalOperationException($statement, $itemVariable);
}
break;
default:
throw new CompilerException('Unknown type: ' . $itemVariable->getType(), $statement);
}
break;
default:
throw new CompilerException('Unknown type ' . $resolvedExpr->getType(), $statement);
}
}
|
Performs string assignment.
@throws CompilerException
@throws IllegalOperationException
|
doStringAssignment
|
php
|
zephir-lang/zephir
|
src/Statements/Let/Variable.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Statements/Let/Variable.php
|
MIT
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.