Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Test #2078 #2079

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .codacy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ engines:
enabled: true
config:
rulesets: "phpmd.xml.dist"

exclude_paths:
- 'ext/**'
- 'prototypes/**'
25 changes: 18 additions & 7 deletions .dir-locals.el
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
;;; Directory Local Variables
;; For more information see (info "(emacs) Directory Variables")

((zephir-mode . ((fill-column . 120)
(indent-tabs-mode . nil)))
(c-mode . ((fill-column . 80)
(c-file-style . "k&r")
(tab-width . 4)
(c-basic-offset . 4)
(indent-tabs-mode . t))))
((nil
(indent-tabs-mode . nil))
(zephir-mode
(fill-column . 120))
(c-mode
(fill-column . 80)
(c-file-style . "k&r")
(tab-width . 4)
(c-basic-offset . 4)
(indent-tabs-mode . t)
(flycheck-checker . c/c++-gcc)
(flycheck-disabled-checkers . (c/c++-clang))
(flycheck-gcc-language-standard . "gnu99"))
(php-mode
(fill-column . 120)
(c-file-style . nil)
(indent-tabs-mode . nil)
(flycheck-disabled-checkers . (php-phpmd php-phpcs))))
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# Directories to ignore (do not add trailing '/'s, they skip symlinks).
.zephir
.libs
.phpunit
ide
doc
vendor
Expand All @@ -28,11 +27,13 @@ compile.log
compile-errors.log

# Test and build configurations.
phpunit.xml
phpcs.xml
.php_cs
.php_cs.cache
.phpunit
box.json
phpcs.xml
phpmd.xml
phpunit.xml

# Build artefact
zephir.phar
Expand Down
29 changes: 16 additions & 13 deletions Library/Backends/ZendEngine2/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function generateInitCode(&$groupVariables, $type, $pointer, Variable $va
/**
* {@inheritdoc}
*/
public function initializeVariableDefaults($variables, CompilationContext $compilationContext): string
public function initializeVariableDefaults(array $variables, CompilationContext $context): string
{
throw new CompilerException(
'ZendEngine2 backend is no longer supported'
Expand Down Expand Up @@ -274,18 +274,11 @@ public function concatSelf(Variable $variable, Variable $itemVariable, Compilati
$context->codePrinter->output('zephir_concat_self('.$variable.', '.$itemVariable.');');
}

public function initArray(Variable $variable, CompilationContext $context, $size = null, $useCodePrinter = true)
public function initArray(Variable $variable, CompilationContext $context, int $size = null)
{
if (!isset($size)) {
$output = 'array_init('.$this->getVariableCode($variable).');';
} else {
$output = 'zephir_create_array('.$this->getVariableCode($variable).', '.$size.', 0);';
}
if ($useCodePrinter) {
$context->codePrinter->output($output);
}

return $output;
throw new CompilerException(
'ZendEngine2 backend is no longer supported'
);
}

public function createClosure(Variable $variable, $classDefinition, CompilationContext $context)
Expand Down Expand Up @@ -690,7 +683,17 @@ public function resolveValue($value, CompilationContext $context, $usePointer =
return $value;
}

public function updateProperty(Variable $symbolVariable, $propertyName, $value, CompilationContext $context)
/**
* {@inheritdoc}
*
* @param Variable $variable
* @param string|Variable $property
* @param mixed $value
* @param CompilationContext $context
*
* @return void
*/
public function updateProperty(Variable $variable, $property, $value, CompilationContext $context)
{
throw new CompilerException(
'ZendEngine2 backend is no longer supported'
Expand Down
221 changes: 93 additions & 128 deletions Library/Backends/ZendEngine3/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ public function generateInitCode(&$groupVariables, $type, $pointer, Variable $va
$groupVariables[] = $pointer.$variable->getName();
break;

/* @noinspection PhpMissingBreakStatementInspection */
case 'char':
if (\strlen($defaultValue) > 4) {
if (\strlen($defaultValue) > 10) {
Expand Down Expand Up @@ -401,141 +400,43 @@ public function generateInitCode(&$groupVariables, $type, $pointer, Variable $va
* {@inheritdoc}
*
* @param Variable[] $variables
* @param CompilationContext $compilationContext
* @param CompilationContext $context
*
* @throws CompilerException
*
* @return string
*/
public function initializeVariableDefaults($variables, CompilationContext $compilationContext): string
public function initializeVariableDefaults(array $variables, CompilationContext $context): string
{
$codePrinter = new CodePrinter();
$codePrinter->increaseLevel();
$oldCodePrinter = $compilationContext->codePrinter;
$compilationContext->codePrinter = $codePrinter;

$oldCodePrinter = $context->codePrinter;
$context->codePrinter = $codePrinter;

$variablesManager = new VariablesManager();

/* Initialize default values in dynamic variables */
foreach ($variables as $variable) {
/*
* Initialize 'dynamic' variables with default values
*/
if ('variable' == $variable->getType()) {
if ($variable->getNumberUses() > 0) {
if ('this_ptr' != $variable->getName() && 'return_value' != $variable->getName() && 'return_value_ptr' != $variable->getName()) {
$defaultValue = $variable->getDefaultInitValue();
if (\is_array($defaultValue)) {
$compilationContext->symbolTable->mustGrownStack(true);
$compilationContext->backend->initVar($variable, $compilationContext);
switch ($defaultValue['type']) {
case 'int':
case 'uint':
case 'long':
$compilationContext->backend->assignLong($variable, $defaultValue['value'], $compilationContext);
break;

case 'bool':
$compilationContext->backend->assignBool($variable, $defaultValue['value'], $compilationContext);
break;

case 'char':
case 'uchar':
if (\strlen($defaultValue['value']) > 2) {
if (\strlen($defaultValue['value']) > 10) {
throw new CompilerException("Invalid char literal: '".substr($defaultValue['value'], 0, 10)."...'", $defaultValue);
} else {
throw new CompilerException("Invalid char literal: '".$defaultValue['value']."'", $defaultValue);
}
}
$compilationContext->backend->assignLong($variable, '\''.$defaultValue['value'].'\'', $compilationContext);
break;

case 'null':
$compilationContext->backend->assignNull($variable, $compilationContext);
break;

case 'double':
$compilationContext->backend->assignDouble($variable, $defaultValue['value'], $compilationContext);
break;

case 'string':
$compilationContext->backend->assignString(
$variable,
add_slashes($defaultValue['value']),
$compilationContext
);
break;

case 'array':
case 'empty-array':
$compilationContext->backend->initArray($variable, $compilationContext, null);
break;

default:
throw new CompilerException('Invalid default type: '.$defaultValue['type'].' for data type: '.$variable->getType(), $variable->getOriginal());
}
}
}
}
/* Do not initialize unused variable */
if ($variable->getNumberUses() < 1) {
continue;
}

/*
* Initialize 'string' variables with default values
*/
if ('string' == $variable->getType()) {
if ($variable->getNumberUses() > 0) {
$defaultValue = $variable->getDefaultInitValue();
if (\is_array($defaultValue)) {
$compilationContext->symbolTable->mustGrownStack(true);
$compilationContext->backend->initVar($variable, $compilationContext);
switch ($defaultValue['type']) {
case 'string':
$compilationContext->backend->assignString(
$variable,
add_slashes($defaultValue['value']),
$compilationContext
);
break;

case 'null':
$compilationContext->backend->assignString($variable, null, $compilationContext);
break;

default:
throw new CompilerException('Invalid default type: '.$defaultValue['type'].' for data type: '.$variable->getType(), $variable->getOriginal());
}
}
}
/* The default init value to be used bellow.
Actually this value should be in array form and
provide 'type' and 'value' keys. */
$value = $variable->getDefaultInitValue();
if (!\is_array($value)) {
continue;
}

/*
* Initialize 'array' variables with default values
*/
if ('array' == $variable->getType()) {
if ($variable->getNumberUses() > 0) {
$defaultValue = $variable->getDefaultInitValue();
if (\is_array($defaultValue)) {
$compilationContext->symbolTable->mustGrownStack(true);
$compilationContext->backend->initVar($variable, $compilationContext);
switch ($defaultValue['type']) {
case 'null':
$compilationContext->backend->assignNull($variable, $compilationContext);
break;

case 'array':
case 'empty-array':
$compilationContext->backend->initArray($variable, $compilationContext, null);
break;

default:
throw new CompilerException('Invalid default type: '.$defaultValue['type'].' for data type: '.$variable->getType(), $variable->getOriginal());
}
}
}
}
$variablesManager->initializeDefaults($variable, $value, $context);
}
$compilationContext->codePrinter = $oldCodePrinter;

return (string) $codePrinter->getOutput();
$context->codePrinter = $oldCodePrinter;

return $codePrinter->getOutput();
}

public function declareConstant($type, $name, $value, CompilationContext $context)
Expand Down Expand Up @@ -939,15 +840,57 @@ public function resolveValue($value, CompilationContext $context, $usePointer =
return $value;
}

public function updateProperty(Variable $symbolVariable, $propertyName, $value, CompilationContext $context)
/**
* {@inheritdoc}
*
* @param Variable $variable
* @param string|Variable $property
* @param mixed $value
* @param CompilationContext $context
*
* @return void
*/
public function updateProperty(Variable $variable, $property, $value, CompilationContext $context)
{
//TODO: maybe optimizations as well as above
// TODO(serghei): maybe optimizations as well as above
$value = $this->resolveValue($value, $context);
if ($propertyName instanceof Variable) {
$context->codePrinter->output('zephir_update_property_zval_zval('.$this->getVariableCode($symbolVariable).', '.$this->getVariableCode($propertyName).', '.$value.');');
} else {
$context->codePrinter->output('zephir_update_property_zval('.$this->getVariableCode($symbolVariable).', SL("'.$propertyName.'"), '.$value.');');

if ($property instanceof Variable) {
$context->codePrinter->output(
sprintf(
'zephir_update_property_zval_zval(%s, %s, %s);',
$this->getVariableCode($variable),
$this->getVariableCode($property),
$value
)
);

return;
}

/* Are we going to init default object property value? */
if ($context->currentMethod &&
preg_match('/^zephir_init_properties/', $context->currentMethod->getName())) {
$context->codePrinter->output(
sprintf(
'zephir_init_property_zval(%s, ZEND_STRL("%s"), %s);',
$this->getVariableCode($variable),
$property,
$value
)
);

return;
}

$context->codePrinter->output(
sprintf(
'zephir_update_property_zval(%s, ZEND_STRL("%s"), %s);',
$this->getVariableCode($variable),
$property,
$value
)
);
}

public function updateStaticProperty($classEntry, $property, $value, CompilationContext $context)
Expand Down Expand Up @@ -1214,16 +1157,38 @@ public function fetchClassEntry($str)
* {@inheritdoc}
*
* @param string $type
* @param CompilationContext $compilationContext
* @param CompilationContext $context
* @param bool $isLocal
*
* @return Variable
*/
public function getScalarTempVariable(
string $type,
CompilationContext $compilationContext,
CompilationContext $context,
$isLocal = true
): Variable {
return $compilationContext->symbolTable->getTempNonTrackedVariable($type, $compilationContext);
return $context->symbolTable->getTempNonTrackedVariable($type, $context);
}

/**
* {@inheritdoc}
*
* @param Variable $variable
* @param CompilationContext $context
* @param int $size
*
* @return void
*/
public function initArray(Variable $variable, CompilationContext $context, int $size = null)
{
$code = $this->getVariableCode($variable);

if (null === $size) {
$output = "array_init({$code});";
} else {
$output = "zephir_create_array({$code}, {$size}, 0);";
}

$context->codePrinter->output($output);
}
}
Loading