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 compile(CompilationContext $compilationContext): void
{
$this->processValue($compilationContext);
$constantValue = $this->value['value'] ?? null;
$compilationContext->backend->declareConstant(
$this->value['type'],
$this->getName(),
$constantValue,
$compilationContext
);
}
|
Produce the code to register a class constant.
@throws Exception
@throws ReflectionException
|
compile
|
php
|
zephir-lang/zephir
|
src/Class/Constant.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Constant.php
|
MIT
|
public function getDocBlock(): ?string
{
return $this->docblock;
}
|
Returns the docblock related to the constant.
|
getDocBlock
|
php
|
zephir-lang/zephir
|
src/Class/Constant.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Constant.php
|
MIT
|
public function getValueType(): string
{
return $this->value['type'];
}
|
Get the type of the value of the constant.
|
getValueType
|
php
|
zephir-lang/zephir
|
src/Class/Constant.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Constant.php
|
MIT
|
public function getValueValue(): mixed
{
return $this->value['value'] ?? null;
}
|
Get value of the value of the constant.
|
getValueValue
|
php
|
zephir-lang/zephir
|
src/Class/Constant.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Constant.php
|
MIT
|
public function processValue(CompilationContext $compilationContext): void
{
if ('constant' === $this->value['type']) {
$compiledExpression = (new Constants())->compile($this->value, $compilationContext);
$this->value = [
'type' => $compiledExpression->getType(),
'value' => $compiledExpression->getCode(),
];
return;
}
if ('static-constant-access' === $this->value['type']) {
$compiledExpression = (new StaticConstantAccess())->compile($this->value, $compilationContext);
$this->value = [
'type' => $compiledExpression->getType(),
'value' => $compiledExpression->getCode(),
];
}
}
|
Process the value of the class constant if needed.
@throws Exception
@throws ReflectionException
|
processValue
|
php
|
zephir-lang/zephir
|
src/Class/Constant.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Constant.php
|
MIT
|
public static function escape(string $className): string
{
return str_replace(static::NAMESPACE_SEPARATOR, '\\\\', $className);
}
|
Prepares a class name to be used as a C-string.
|
escape
|
php
|
zephir-lang/zephir
|
src/Class/Entry.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Entry.php
|
MIT
|
private function isInternalClass(string $className): bool
{
$this->isInternal = preg_match(
'/^' . $className . '/',
$this->compilationContext->classDefinition->getNamespace()
) === 1;
return $this->isInternal;
}
|
Detect if start of namespace class belongs to project namespace.
|
isInternalClass
|
php
|
zephir-lang/zephir
|
src/Class/Entry.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Entry.php
|
MIT
|
public function compile(CompilationContext $compilationContext): void
{
switch ($this->defaultValue['type']) {
case 'long':
case 'int':
case 'string':
case 'double':
case 'bool':
$this->declareProperty($compilationContext, $this->defaultValue['type'], $this->defaultValue['value']);
break;
case 'array':
case 'empty-array':
$this->initializeArray();
// no break
case 'null':
$this->declareProperty($compilationContext, $this->defaultValue['type'], null);
break;
case 'static-constant-access':
$expression = new Expression($this->defaultValue);
$compiledExpression = $expression->compile($compilationContext);
$this->declareProperty(
$compilationContext,
$compiledExpression->getType(),
$compiledExpression->getCode()
);
break;
default:
throw new CompilerException('Unknown default type: ' . $this->defaultValue['type'], $this->original);
}
}
|
Produce the code to register a property.
@throws Exception
@throws ReflectionException
|
compile
|
php
|
zephir-lang/zephir
|
src/Class/Property.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Property.php
|
MIT
|
public function getClassDefinition(): Definition
{
return $this->classDefinition;
}
|
Returns the class definition where the method was declared.
|
getClassDefinition
|
php
|
zephir-lang/zephir
|
src/Class/Property.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Property.php
|
MIT
|
public function getDocBlock(): ?string
{
return $this->docBlock;
}
|
Returns the docblock related to the property.
|
getDocBlock
|
php
|
zephir-lang/zephir
|
src/Class/Property.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Property.php
|
MIT
|
public function getVisibilityAccessor(): string
{
$modifiers = [];
foreach ($this->visibility as $visibility) {
switch ($visibility) {
case 'protected':
$modifiers['ZEND_ACC_PROTECTED'] = true;
break;
case 'private':
$modifiers['ZEND_ACC_PRIVATE'] = true;
break;
case 'public':
$modifiers['ZEND_ACC_PUBLIC'] = true;
break;
case 'static':
$modifiers['ZEND_ACC_STATIC'] = true;
break;
default:
throw new Exception('Unknown modifier ' . $visibility);
}
}
return implode('|', array_keys($modifiers));
}
|
Returns the C-visibility accessors for the model.
@throws Exception
|
getVisibilityAccessor
|
php
|
zephir-lang/zephir
|
src/Class/Property.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Property.php
|
MIT
|
public function isPrivate(): bool
{
return in_array('private', $this->visibility);
}
|
Checks whether the variable is private.
|
isPrivate
|
php
|
zephir-lang/zephir
|
src/Class/Property.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Property.php
|
MIT
|
public function isProtected(): bool
{
return in_array('protected', $this->visibility);
}
|
Checks whether the variable is protected.
|
isProtected
|
php
|
zephir-lang/zephir
|
src/Class/Property.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Property.php
|
MIT
|
public function isPublic(): bool
{
return in_array('public', $this->visibility);
}
|
Checks whether the variable is public.
|
isPublic
|
php
|
zephir-lang/zephir
|
src/Class/Property.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Property.php
|
MIT
|
public function isStatic(): bool
{
return in_array('static', $this->visibility);
}
|
Checks whether the variable is static.
|
isStatic
|
php
|
zephir-lang/zephir
|
src/Class/Property.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Property.php
|
MIT
|
protected function declareProperty(CompilationContext $compilationContext, string $type, $value): void
{
$codePrinter = $compilationContext->codePrinter;
if (is_object($value)) {
return;
}
$classEntry = $compilationContext->classDefinition->getClassEntry();
switch ($type) {
case Types::T_INT:
case Types::T_LONG:
$codePrinter->output(
sprintf(
'zend_declare_property_long(%s, SL("%s"), %s, %s);',
$classEntry,
$this->getName(),
$value,
$this->getVisibilityAccessor()
),
false
);
break;
case Types::T_DOUBLE:
$codePrinter->output(
sprintf(
'zend_declare_property_double(%s, SL("%s"), %s, %s);',
$classEntry,
$this->getName(),
$value,
$this->getVisibilityAccessor()
),
false
);
break;
case Types::T_BOOL:
$codePrinter->output(
sprintf(
'zend_declare_property_bool(%s, SL("%s"), %s, %s);',
$classEntry,
$this->getName(),
$this->getBooleanCode($value),
$this->getVisibilityAccessor()
),
false
);
break;
case Types::T_CHAR:
case Types::T_STRING:
$codePrinter->output(
sprintf(
'zend_declare_property_string(%s, SL("%s"), "%s", %s);',
$classEntry,
$this->getName(),
Name::addSlashes($value),
$this->getVisibilityAccessor()
),
false
);
break;
case 'array':
case 'empty-array':
case 'null':
$codePrinter->output(
sprintf(
'zend_declare_property_null(%s, SL("%s"), %s);',
$classEntry,
$this->getName(),
$this->getVisibilityAccessor()
),
false
);
break;
default:
throw new CompilerException('Unknown default type: ' . $type, $this->original);
}
}
|
Declare class property with default value.
@throws Exception
@throws CompilerException
|
declareProperty
|
php
|
zephir-lang/zephir
|
src/Class/Property.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Property.php
|
MIT
|
protected function removeInitializationStatements(array &$statements): void
{
foreach ($statements as $index => $statement) {
if (!$this->isStatic()) {
if ($statement['expr']['left']['right']['value'] == $this->name) {
unset($statements[$index]);
}
} else {
if ($statement['assignments'][0]['property'] == $this->name) {
unset($statements[$index]);
}
}
}
}
|
Removes all initialization statements related to this property.
|
removeInitializationStatements
|
php
|
zephir-lang/zephir
|
src/Class/Property.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Property.php
|
MIT
|
public function addConstant(Constant $constant): void
{
if (isset($this->constants[$constant->getName()])) {
throw new CompilerException("Constant '" . $constant->getName() . "' was defined more than one time");
}
$this->constants[$constant->getName()] = $constant;
}
|
Adds a constant to the definition.
@throws CompilerException
|
addConstant
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function addMethod(Method $method, array $statement = []): void
{
$methodName = strtolower($method->getName());
if (isset($this->methods[$methodName])) {
throw new CompilerException(
"Method '" . $method->getName() . "' was defined more than one time",
$statement
);
}
$this->methods[$methodName] = $method;
}
|
Adds a method to the class definition.
@throws CompilerException
|
addMethod
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function addProperty(Property $property): void
{
if (isset($this->properties[$property->getName()])) {
throw new CompilerException(
"Property '" . $property->getName() . "' was defined more than one time",
$property->getOriginal()
);
}
$this->properties[$property->getName()] = $property;
}
|
Adds a property to the definition.
@throws CompilerException
|
addProperty
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function checkInterfaceImplements(self $classDefinition, self $interfaceDefinition): void
{
foreach ($interfaceDefinition->getMethods() as $method) {
if (!$classDefinition->hasMethod($method->getName())) {
throw new CompilerException(
sprintf(
'Class %s must implement a method called: "%s" as requirement of interface: "%s"',
$classDefinition->getCompleteName(),
$method->getName(),
$interfaceDefinition->getCompleteName()
)
);
}
if (!$method->hasParameters()) {
continue;
}
$implementedMethod = $classDefinition->getMethod($method->getName());
if (
$implementedMethod->getNumberOfRequiredParameters() > $method->getNumberOfRequiredParameters() ||
$implementedMethod->getNumberOfParameters() < $method->getNumberOfParameters()
) {
throw new CompilerException(
sprintf(
'Method %s::%s() does not have the same number of required parameters in interface: "%s"',
$classDefinition->getCompleteName(),
$method->getName(),
$interfaceDefinition->getCompleteName()
)
);
}
}
}
|
Checks if a class implements an interface.
@throws CompilerException
|
checkInterfaceImplements
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function compile(CompilationContext $compilationContext): void
{
$this->compiler = $compilationContext->compiler;
/**
* Sets the current object as global class definition
*/
$compilationContext->classDefinition = $this;
/**
* Get the global codePrinter.
*/
$codePrinter = $compilationContext->codePrinter;
/**
* The ZEPHIR_INIT_CLASS defines properties and constants exported by the class.
*/
$initClassName = $this->getCNamespace() . '_' . $this->getName();
$codePrinter->output('ZEPHIR_INIT_CLASS(' . $initClassName . ')');
$codePrinter->output('{');
$codePrinter->increaseLevel();
/**
* Method entry.
*/
$methods = &$this->methods;
$initMethod = $this->getLocalOrParentInitMethod();
if (count($methods) > 0 || $initMethod) {
$methodEntry = strtolower($this->getCNamespace()) . '_' . strtolower($this->getName()) . '_method_entry';
} else {
$methodEntry = 'NULL';
}
foreach ($methods as $method) {
$method->setupOptimized($compilationContext);
}
$namespace = str_replace('\\', '_', $compilationContext->config->get('namespace'));
$flags = '0';
if ($this->isAbstract()) {
$flags = 'ZEND_ACC_EXPLICIT_ABSTRACT_CLASS';
}
if ($this->isFinal()) {
if ('0' === $flags) {
$flags = 'ZEND_ACC_FINAL_CLASS';
} else {
$flags .= '|ZEND_ACC_FINAL_CLASS';
}
}
/**
* Register the class with extends + interfaces.
*/
$classExtendsDefinition = null;
if ($this->extendsClass) {
$classExtendsDefinition = $this->extendsClassDefinition;
if ($classExtendsDefinition instanceof self && !$classExtendsDefinition->isBundled()) {
$classEntry = $classExtendsDefinition->getClassEntry($compilationContext);
} else {
$className = method_exists(
$classExtendsDefinition,
'getCompleteName'
) ? $classExtendsDefinition->getCompleteName() : $classExtendsDefinition->getName();
$classEntry = (new Entry('\\' . ltrim($className, '\\'), $compilationContext))->get();
}
if (self::TYPE_CLASS === $this->getType()) {
$codePrinter->output(
'ZEPHIR_REGISTER_CLASS_EX(' . $this->getNCNamespace() . ', ' . $this->getName(
) . ', ' . $namespace . ', ' . strtolower(
$this->getSCName($namespace)
) . ', ' . $classEntry . ', ' . $methodEntry . ', ' . $flags . ');'
);
} else {
$codePrinter->output(
'ZEPHIR_REGISTER_INTERFACE_EX(' . $this->getNCNamespace() . ', ' . $this->getName(
) . ', ' . $namespace . ', ' . strtolower(
$this->getSCName($namespace)
) . ', ' . $classEntry . ', ' . $methodEntry . ');'
);
}
} else {
if (self::TYPE_CLASS === $this->getType()) {
$codePrinter->output(
'ZEPHIR_REGISTER_CLASS(' . $this->getNCNamespace() . ', ' . $this->getName(
) . ', ' . $namespace . ', ' . strtolower(
$this->getSCName($namespace)
) . ', ' . $methodEntry . ', ' . $flags . ');'
);
} else {
$codePrinter->output(
'ZEPHIR_REGISTER_INTERFACE(' . $this->getNCNamespace() . ', ' . $this->getName(
) . ', ' . $namespace . ', ' . strtolower($this->getSCName($namespace)) . ', ' . $methodEntry . ');'
);
}
}
$codePrinter->outputBlankLine();
/**
* Compile properties.
*/
foreach ($this->getProperties() as $property) {
$docBlock = $property->getDocBlock();
if ($docBlock) {
$codePrinter->outputDocBlock($docBlock);
}
$property->compile($compilationContext);
$codePrinter->outputBlankLine();
}
$initMethod = $this->getInitMethod();
if ($initMethod) {
$codePrinter->output(
$namespace . '_' . strtolower(
$this->getSCName($namespace)
) . '_ce->create_object = ' . $initMethod->getName() . ';'
);
}
/**
* Compile constants.
*/
foreach ($this->getConstants() as $constant) {
$docBlock = $constant->getDocBlock();
if ($docBlock) {
$codePrinter->outputDocBlock($docBlock);
}
$constant->compile($compilationContext);
$codePrinter->outputBlankLine();
}
/**
* Implemented interfaces.
*/
$codePrinter->outputBlankLine(true);
foreach ($this->interfaces as $interface) {
/**
* Try to find the interface.
*/
$classEntry = null;
if ($compilationContext->compiler->isInterface($interface)) {
$classInterfaceDefinition = $compilationContext->compiler->getClassDefinition($interface);
$classEntry = $classInterfaceDefinition->getClassEntry($compilationContext);
} elseif ($compilationContext->compiler->isBundledInterface($interface)) {
$classInterfaceDefinition = $compilationContext->compiler->getInternalClassDefinition($interface);
$classEntry = (new Entry(
'\\' . $classInterfaceDefinition->getName(),
$compilationContext
))->get();
}
if (!$classEntry) {
if ($compilationContext->compiler->isClass($interface)) {
throw new CompilerException(
sprintf(
'Cannot locate interface %s when implementing interfaces on %s. ' .
'%s is currently a class',
$interface,
$this->getCompleteName(),
$interface
),
$this->originalNode
);
} else {
throw new CompilerException(
sprintf(
'Cannot locate interface %s when implementing interfaces on %s',
$interface,
$this->getCompleteName()
),
$this->originalNode
);
}
}
/**
* We don't check if abstract classes implement the methods in their interfaces
*/
if (!$this->isAbstract() && !$this->isInterface()) {
$this->checkInterfaceImplements($this, $classInterfaceDefinition);
}
$codePrinter->output(
sprintf(
'zend_class_implements(%s, 1, %s);',
$this->getClassEntry(),
$classEntry
)
);
}
if (!$this->isAbstract() && !$this->isInterface()) {
/**
* Interfaces in extended classes may have
*/
if ($classExtendsDefinition instanceof self && !$classExtendsDefinition->isBundled()) {
$interfaces = $classExtendsDefinition->getImplementedInterfaces();
foreach ($interfaces as $interface) {
$classInterfaceDefinition = null;
if ($compilationContext->compiler->isInterface($interface)) {
$classInterfaceDefinition = $compilationContext->compiler->getClassDefinition($interface);
} elseif ($compilationContext->compiler->isBundledInterface($interface)) {
$classInterfaceDefinition = $compilationContext
->compiler
->getInternalClassDefinition($interface)
;
}
if ($classInterfaceDefinition !== null) {
$this->checkInterfaceImplements($this, $classInterfaceDefinition);
}
}
}
}
$codePrinter->output('return SUCCESS;');
$codePrinter->decreaseLevel();
$codePrinter->output('}');
$codePrinter->outputBlankLine();
/**
* Compile methods
*/
foreach ($methods as $method) {
$docBlock = $method->getDocBlock();
if ($docBlock) {
$codePrinter->outputDocBlock($docBlock);
}
if (self::TYPE_CLASS === $this->getType()) {
if (!$method->isInternal()) {
$codePrinter->output(
'PHP_METHOD(' . $this->getCNamespace() . '_' . $this->getName() . ', ' . $method->getName(
) . ')'
);
} else {
$codePrinter->output(
$compilationContext->backend->getInternalSignature($method, $compilationContext)
);
}
$codePrinter->output('{');
if (!$method->isAbstract()) {
$method->compile($compilationContext);
}
$codePrinter->output('}');
$codePrinter->outputBlankLine();
} else {
$codePrinter->output(
'ZEPHIR_DOC_METHOD(' . $this->getCNamespace() . '_' . $this->getName() . ', ' . $method->getName(
) . ');'
);
}
}
/**
* Check whether classes must be exported.
*/
$exportClasses = $compilationContext->config->get('export-classes', 'extra');
$exportAPI = $exportClasses ? 'extern ZEPHIR_API' : 'extern';
/**
* Create a code printer for the header file.
*/
$codePrinter = new Printer();
$codePrinter->outputBlankLine();
$codePrinter->output($exportAPI . ' zend_class_entry *' . $this->getClassEntry() . ';');
$codePrinter->outputBlankLine();
$codePrinter->output('ZEPHIR_INIT_CLASS(' . $this->getCNamespace() . '_' . $this->getName() . ');');
$codePrinter->outputBlankLine();
if (self::TYPE_CLASS === $this->getType() && count($methods) > 0) {
foreach ($methods as $method) {
if (!$method->isInternal()) {
$codePrinter->output(
'PHP_METHOD(' . $this->getCNamespace() . '_' . $this->getName() . ', ' . $method->getName(
) . ');'
);
} else {
$internalSignature = $compilationContext->backend->getInternalSignature(
$method,
$compilationContext
);
$codePrinter->output($internalSignature . ';');
}
}
$codePrinter->outputBlankLine();
}
/**
* Specifying Argument Information
*/
foreach ($methods as $method) {
$argInfo = new ArgInfoDefinition(
$method->getArgInfoName($this),
$method,
$codePrinter,
$compilationContext
);
$argInfo->setBooleanDefinition('_IS_BOOL');
$argInfo->setRichFormat(true);
$argInfo->render();
}
if (count($methods) > 0) {
$codePrinter->output(
sprintf(
'ZEPHIR_INIT_FUNCS(%s_%s_method_entry) {',
strtolower($this->getCNamespace()),
strtolower($this->getName())
)
);
foreach ($methods as $method) {
if (self::TYPE_CLASS === $this->getType()) {
if (!$method->isInternal()) {
$richFormat = $method->isReturnTypesHintDetermined() && $method->areReturnTypesCompatible();
if ($richFormat || $method->hasParameters()) {
$codePrinter->output(
sprintf(
// TODO: Rename to ZEND_ME
"\tPHP_ME(%s_%s, %s, %s, %s)",
$this->getCNamespace(),
$this->getName(),
$method->getName(),
$method->getArgInfoName($this),
$method->getModifiers()
)
);
} else {
$codePrinter->output(
sprintf(
// TODO: Rename to ZEND_ME
'PHP_ME(%s_%s, %s, %s, %s)',
$this->getCNamespace(),
$this->getName(),
$method->getName(),
$method->getArgInfoName($this),
$method->getModifiers()
)
);
}
}
} else {
$richFormat = $method->isReturnTypesHintDetermined() && $method->areReturnTypesCompatible();
if ($method->isStatic()) {
if ($richFormat || $method->hasParameters()) {
$codePrinter->output(
sprintf(
"\tZEND_FENTRY(%s, NULL, %s, ZEND_ACC_STATIC|ZEND_ACC_ABSTRACT|ZEND_ACC_PUBLIC)",
$method->getName(),
$method->getArgInfoName($this)
)
);
} else {
$codePrinter->output(
sprintf(
'ZEND_FENTRY(%s, NULL, %s, ZEND_ACC_STATIC|ZEND_ACC_ABSTRACT|ZEND_ACC_PUBLIC)',
$method->getName(),
$method->getArgInfoName($this)
)
);
}
} else {
$isInterface = $method->getClassDefinition()->isInterface();
$codePrinter->output(
sprintf(
"\tPHP_ABSTRACT_ME(%s_%s, %s, %s)",
$this->getCNamespace(),
$this->getName(),
$method->getName(),
$isInterface ? $method->getArgInfoName($this) : 'NULL'
)
);
}
}
}
$codePrinter->output("\t" . 'PHP_FE_END');
$codePrinter->output('};'); // ZEPHIR_INIT_FUNCS
}
$compilationContext->headerPrinter = $codePrinter;
}
|
Compiles a class/interface.
@throws Exception
@throws ReflectionException
|
compile
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function getCNamespace(): string
{
return str_replace('\\', '_', $this->namespace);
}
|
Returns a valid namespace to be used in C-sources.
|
getCNamespace
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function getClassEntry(?CompilationContext $compilationContext = null): string
{
if ($this->external) {
if ($compilationContext === null) {
throw new Exception('A compilation context is required');
}
$this->compiler = $compilationContext->compiler;
/**
* Automatically add the external header
*/
$compilationContext->headersManager->add($this->getExternalHeader(), HeadersManager::POSITION_LAST);
}
return strtolower(str_replace('\\', '_', $this->namespace) . '_' . $this->name) . '_ce';
}
|
Returns the name of the zend_class_entry according to the class name.
@throws Exception
|
getClassEntry
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function getCompleteName(): string
{
return $this->namespace . '\\' . $this->shortName;
}
|
Returns the class name including its namespace.
|
getCompleteName
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function getConstant(string $constantName): ?Constant
{
if (isset($this->constants[$constantName])) {
return $this->constants[$constantName];
}
$extendsClassDefinition = $this->getExtendsClassDefinition();
if ($extendsClassDefinition instanceof self && $extendsClassDefinition->hasConstant($constantName)) {
return $extendsClassDefinition->getConstant($constantName);
}
/**
* Gets constant from interfaces
*/
return $this->getConstantFromInterfaces($constantName);
}
|
Returns a constant definition by its name.
@throws InvalidArgumentException
|
getConstant
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function getConstants(): array
{
return $this->constants;
}
|
Returns all constants defined in the class.
@return Constant[]
|
getConstants
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function getDependencies(): array
{
$dependencies = [];
if ($this->extendsClassDefinition instanceof self) {
$dependencies[] = $this->extendsClassDefinition;
}
foreach ($this->implementedInterfaceDefinitions as $interfaceDefinition) {
if ($interfaceDefinition instanceof self) {
$dependencies[] = $interfaceDefinition;
}
}
return $dependencies;
}
|
Calculate the dependency rank of the class based on its dependencies.
|
getDependencies
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function getDependencyRank(): int
{
return $this->dependencyRank;
}
|
Returns the dependency rank for this class.
|
getDependencyRank
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function getExtendsClassDefinition(): ?AbstractDefinition
{
if (!$this->extendsClassDefinition && $this->extendsClass) {
$this->setExtendsClassDefinition($this->compiler->getClassDefinition($this->extendsClass));
}
return $this->extendsClassDefinition;
}
|
Returns the class definition related to the extended class.
|
getExtendsClassDefinition
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function getExternalHeader(): string
{
$parts = explode('\\', $this->namespace);
return 'ext/' . strtolower(
$parts[0] . DIRECTORY_SEPARATOR . str_replace(
'\\',
DIRECTORY_SEPARATOR,
$this->namespace
) . DIRECTORY_SEPARATOR . $this->name
) . '.zep';
}
|
Returns an absolute location to the class header.
|
getExternalHeader
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function getImplementedInterfaceDefinitions(): array
{
return $this->implementedInterfaceDefinitions;
}
|
Returns the class definition for the implemented interfaces.
|
getImplementedInterfaceDefinitions
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function getInitMethod(): ?Method
{
$initClassName = $this->getCNamespace() . '_' . $this->getName();
return $this->getMethod('zephir_init_properties_' . $initClassName);
}
|
Returns the initialization method if any does exist.
|
getInitMethod
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function getLocalOrParentInitMethod(): ?Method
{
$method = $this->getInitMethod();
if ($method === null) {
return null;
}
$parentClassDefinition = $this->getExtendsClassDefinition();
if ($parentClassDefinition instanceof self) {
$method = $parentClassDefinition->getInitMethod();
if ($method instanceof Method) {
$this->addInitMethod($method->getStatementsBlock());
}
}
return $method;
}
|
Returns the initialization method if any does exist.
|
getLocalOrParentInitMethod
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function getMethods(): array
{
return $this->methods;
}
|
Returns all methods defined in the class.
@return Method[]
|
getMethods
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function getNCNamespace(): string
{
return str_replace('\\', '\\\\', $this->namespace);
}
|
Returns a valid namespace to be used in C-sources.
|
getNCNamespace
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function getPossibleMethodName(string $methodName): ?string
{
$methodNameLower = strtolower($methodName);
foreach ($this->methods as $name => $method) {
if (metaphone($methodNameLower) === metaphone($name)) {
return $method->getName();
}
}
if ($this->extendsClassDefinition instanceof self) {
return $this->extendsClassDefinition->getPossibleMethodName($methodName);
}
return null;
}
|
Tries to find the most similar name.
|
getPossibleMethodName
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function getProperties(): array
{
return $this->properties;
}
|
Returns all properties defined in the class.
@return Property[]
|
getProperties
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function getProperty(string $propertyName): ?Property
{
if (isset($this->properties[$propertyName])) {
return $this->properties[$propertyName];
}
$extendsClassDefinition = $this->getExtendsClassDefinition();
if ($extendsClassDefinition instanceof self) {
return $extendsClassDefinition->getProperty($propertyName);
}
return null;
}
|
Returns a method definition by its name.
|
getProperty
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function getSCName(string $namespace): string
{
return str_replace(
$namespace . '_',
'',
strtolower(str_replace('\\', '_', $this->namespace) . '_' . $this->name)
);
}
|
Class name without namespace prefix for class registration.
|
getSCName
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function getShortName(): string
{
return $this->shortName;
}
|
Returns the class name without namespace.
|
getShortName
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function getStaticInitMethod(): ?Method
{
$initClassName = $this->getCNamespace() . '_' . $this->getName();
return $this->getMethod('zephir_init_static_properties_' . $initClassName);
}
|
Returns the initialization method if any does exist.
|
getStaticInitMethod
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function hasConstant(string $name): bool
{
if (isset($this->constants[$name])) {
return true;
}
$extendsClassDefinition = $this->getExtendsClassDefinition();
if ($extendsClassDefinition instanceof self && $extendsClassDefinition->hasConstant($name)) {
return true;
}
/**
* Check if constant is defined in interfaces
*/
return $this->hasConstantFromInterfaces($name);
}
|
Checks if class definition has a property.
|
hasConstant
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function hasMethod(string $methodName): bool
{
$methodNameLower = strtolower($methodName);
foreach ($this->methods as $name => $method) {
if ($methodNameLower === $name) {
return true;
}
}
$extendsClassDefinition = $this->getExtendsClassDefinition();
if ($extendsClassDefinition instanceof DefinitionRuntime) {
try {
$extendsClassDefinition = $this->compiler->getInternalClassDefinition(
$extendsClassDefinition->getName()
);
} catch (ReflectionException $e) {
// Do nothing
return false;
}
}
while ($extendsClassDefinition instanceof self) {
if ($extendsClassDefinition->hasMethod($methodName)) {
return true;
}
$extendsClassDefinition = $extendsClassDefinition->getExtendsClassDefinition();
}
return false;
}
|
Checks if the class implements a specific name.
|
hasMethod
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function hasProperty(string $name): bool
{
if (isset($this->properties[$name])) {
return true;
}
$extendsClassDefinition = $this->getExtendsClassDefinition();
return $extendsClassDefinition instanceof self && $extendsClassDefinition->hasProperty($name);
}
|
Checks if a class definition has a property.
|
hasProperty
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function increaseDependencyRank(int $rank): void
{
$this->dependencyRank += $rank + 1;
}
|
A class definition calls this method to mark this class as a dependency of another.
@param int $rank
|
increaseDependencyRank
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function isAbstract(): bool
{
return $this->abstract;
}
|
Checks whether the class is abstract or not.
|
isAbstract
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function isBundled(): bool
{
return $this->isBundled;
}
|
Returns whether the class is bundled or not.
|
isBundled
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function isExternal(): bool
{
return $this->external;
}
|
Returns whether the class is internal or not.
|
isExternal
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function isFinal(): bool
{
return $this->final;
}
|
Checks whether the class is abstract or not.
|
isFinal
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function isInterface(): bool
{
return Definition::TYPE_INTERFACE === $this->type;
}
|
Check if the class definition correspond to an interface.
|
isInterface
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function preCompile(CompilationContext $compilationContext): void
{
$this->compiler = $compilationContext->compiler;
/**
* Pre-Compile methods
*/
foreach ($this->methods as $method) {
if (self::TYPE_CLASS === $this->getType() && !$method->isAbstract()) {
$method->preCompile($compilationContext);
}
}
}
|
Pre-compiles a class/interface gathering method information required by other methods.
@throws CompilerException
|
preCompile
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function setExtendsClassDefinition(AbstractDefinition $classDefinition): void
{
$this->extendsClassDefinition = $classDefinition;
}
|
Sets the class definition for the extended class.
|
setExtendsClassDefinition
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function setImplementedInterfaceDefinitions(array $implementedInterfaceDefinitions): void
{
$this->implementedInterfaceDefinitions = $implementedInterfaceDefinitions;
}
|
Sets the class definition for the implemented interfaces.
|
setImplementedInterfaceDefinitions
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function setIsAbstract(bool $abstract): void
{
$this->abstract = $abstract;
}
|
Sets if the class is final.
|
setIsAbstract
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function setIsBundled(bool $isBundled): void
{
$this->isBundled = $isBundled;
}
|
Sets if the class is internal or not.
|
setIsBundled
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function setIsExternal(bool $isExternal): void
{
$this->external = $isExternal;
}
|
Sets whether the class is external or not.
|
setIsExternal
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function setIsFinal(bool $final): void
{
$this->final = $final;
}
|
Sets if the class is final.
|
setIsFinal
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function setOriginalNode(array $originalNode): void
{
$this->originalNode = $originalNode;
}
|
Set the original node where the class was declared.
|
setOriginalNode
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function setType(string $type): void
{
$this->type = $type;
}
|
Set the class' type (class/interface).
|
setType
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function updateMethod(Method $method, array $statement = []): void
{
$methodName = strtolower($method->getName());
if (!isset($this->methods[$methodName])) {
throw new CompilerException("Method '" . $method->getName() . "' does not exist", $statement);
}
$this->methods[$methodName] = $method;
}
|
Updates an existing method definition.
@throws CompilerException
|
updateMethod
|
php
|
zephir-lang/zephir
|
src/Class/Definition/Definition.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Definition/Definition.php
|
MIT
|
public function areReturnTypesBoolCompatible(): bool
{
return isset($this->returnTypes['bool']);
}
|
Checks whether at least one return type hint is bool compatible.
|
areReturnTypesBoolCompatible
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function areReturnTypesCompatible(): bool
{
// void
if ($this->isVoid()) {
return true;
}
$totalTypes = count($this->returnTypes);
// union types
if ($totalTypes > 1) {
$diff = array_diff(array_keys($this->returnTypes), array_keys($this->mayBeArgTypes));
if (count($diff) === 0) {
return true;
}
}
// T1 | T2
if (2 === $totalTypes && !isset($this->returnTypes['null'])) {
return false;
}
// null | T1 | T2
if ($totalTypes > 2) {
return false;
}
return true;
}
|
Checks if the method have compatible return types.
|
areReturnTypesCompatible
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function areReturnTypesDoubleCompatible(): bool
{
return isset($this->returnTypes['double']);
}
|
Checks whether at least one return type hint is double compatible.
|
areReturnTypesDoubleCompatible
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function areReturnTypesFalseCompatible(): bool
{
return isset($this->returnTypes['false']);
}
|
Checks whether at least one return type hint is false compatible.
|
areReturnTypesFalseCompatible
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function areReturnTypesIntCompatible(): bool
{
$types = ['int', 'uint', 'char', 'uchar', 'long', 'ulong'];
foreach ($this->returnTypes as $returnType => $definition) {
if (in_array($returnType, $types)) {
return true;
}
}
return false;
}
|
Checks whether at least one return type hint is integer compatible.
|
areReturnTypesIntCompatible
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function areReturnTypesNullCompatible(): bool
{
return isset($this->returnTypes['null']);
}
|
Checks whether at least one return type hint is null compatible.
|
areReturnTypesNullCompatible
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function areReturnTypesStringCompatible(): bool
{
return isset($this->returnTypes['string']);
}
|
Checks whether at least one return type hint is string compatible.
|
areReturnTypesStringCompatible
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function assignDefaultValue(array $parameter, CompilationContext $compilationContext): string
{
/**
* Class-Hinted parameters only can be null?
*/
if (isset($parameter['cast'])) {
if ('null' !== $parameter['default']['type']) {
throw new CompilerException(
'Class-Hinted parameters only can have "null" as default parameter',
$parameter
);
}
}
$oldCodePrinter = $compilationContext->codePrinter;
$codePrinter = new Printer();
$codePrinter->increaseLevel();
$codePrinter->increaseLevel();
$compilationContext->codePrinter = $codePrinter;
$paramVariable = $compilationContext->symbolTable->getVariableForWrite($parameter['name'], $compilationContext);
/**
* TODO: Refactoring this place, move to one - static-constant-access
*/
$dataType = $this->getParamDataType($parameter);
switch ($dataType) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
switch ($parameter['default']['type']) {
case 'static-constant-access':
/**
* Now I can write code for easy use on Expression because code in this method don't write with codePrinter ;(.
*
* TODO: Rewrite all to codePrinter
*/
$symbolVariable = $compilationContext->symbolTable->getVariableForWrite(
$parameter['name'],
$compilationContext,
$parameter['default']
);
$expression = new Expression($parameter['default']);
$expression->setExpectReturn(true, $symbolVariable);
$compiledExpression = $expression->compile($compilationContext);
if ('int' != $compiledExpression->getType()) {
throw new CompilerException(
'Default parameter value type: '
. $compiledExpression->getType()
. ' cannot be assigned to variable(int)',
$parameter
);
}
$parameter['default']['type'] = $compiledExpression->getType();
$parameter['default']['value'] = $compiledExpression->getCode();
$compilationContext->codePrinter = $oldCodePrinter;
return $this->assignDefaultValue($parameter, $compilationContext);
case 'null':
$codePrinter->output($parameter['name'] . ' = 0;');
break;
case 'int':
case 'uint':
case 'long':
$codePrinter->output($parameter['name'] . ' = ' . $parameter['default']['value'] . ';');
break;
case 'double':
$codePrinter->output($parameter['name'] . ' = (int) ' . $parameter['default']['value'] . ';');
break;
default:
throw new CompilerException(
'Default parameter value type: ' . $parameter['default']['type'] . ' cannot be assigned to variable(int)',
$parameter
);
}
break;
case 'double':
switch ($parameter['default']['type']) {
case 'static-constant-access':
return $this->processStaticConstantAccess(
$compilationContext,
$parameter,
$oldCodePrinter,
'double'
);
case 'null':
$codePrinter->output($parameter['name'] . ' = 0;');
break;
case 'int':
case 'uint':
case 'long':
$codePrinter->output(
$parameter['name'] . ' = (double) ' . $parameter['default']['value'] . ';'
);
break;
case 'double':
$codePrinter->output($parameter['name'] . ' = ' . $parameter['default']['value'] . ';');
break;
default:
throw new CompilerException(
'Default parameter value type: ' . $parameter['default']['type'] . ' cannot be assigned to variable(double)',
$parameter
);
}
break;
case 'bool':
switch ($parameter['default']['type']) {
case 'static-constant-access':
/**
* Now I can write code for easy use on Expression because code in this method don't write with codePrinter ;(.
*
* TODO: Rewrite all to codePrinter
*/
$symbolVariable = $compilationContext->symbolTable->getVariableForWrite(
$parameter['name'],
$compilationContext,
$parameter['default']
);
$expression = new Expression($parameter['default']);
$expression->setExpectReturn(true, $symbolVariable);
$compiledExpression = $expression->compile($compilationContext);
if ('bool' !== $compiledExpression->getType()) {
throw new CompilerException(
'Default parameter value type: '
. $compiledExpression->getType()
. ' cannot be assigned to variable(bool)',
$parameter
);
}
$parameter['default']['type'] = $compiledExpression->getType();
$parameter['default']['value'] = $compiledExpression->getCode();
$compilationContext->codePrinter = $oldCodePrinter;
return $this->assignDefaultValue($parameter, $compilationContext);
case 'null':
$codePrinter->output($parameter['name'] . ' = 0;');
break;
case 'bool':
if ('true' === $parameter['default']['value']) {
$codePrinter->output($parameter['name'] . ' = 1;');
} else {
$codePrinter->output($parameter['name'] . ' = 0;');
}
break;
default:
throw new CompilerException(
'Default parameter value type: '
. $parameter['default']['type']
. ' cannot be assigned to variable(bool)',
$parameter
);
}
break;
case 'string':
$compilationContext->symbolTable->mustGrownStack(true);
$compilationContext->headersManager->add('kernel/memory');
switch ($parameter['default']['type']) {
case 'static-constant-access':
return $this->processStaticConstantAccess(
$compilationContext,
$parameter,
$oldCodePrinter,
'string'
);
case 'null':
$compilationContext->backend->initVar($paramVariable, $compilationContext);
$compilationContext->backend->assignString($paramVariable, null, $compilationContext);
break;
case 'string':
$compilationContext->backend->initVar($paramVariable, $compilationContext);
$compilationContext->backend->assignString(
$paramVariable,
Name::addSlashes($parameter['default']['value']),
$compilationContext
);
break;
default:
throw new CompilerException(
sprintf(
'Default parameter value type: %s cannot be assigned to variable(string)',
$parameter['default']['type']
),
$parameter
);
}
break;
case 'array':
$compilationContext->symbolTable->mustGrownStack(true);
$compilationContext->headersManager->add('kernel/memory');
switch ($parameter['default']['type']) {
case 'null':
$compilationContext->backend->initVar($paramVariable, $compilationContext);
break;
case 'empty-array':
case 'array':
$compilationContext->backend->initVar($paramVariable, $compilationContext);
$compilationContext->backend->initArray($paramVariable, $compilationContext);
break;
default:
throw new CompilerException(
'Default parameter value type: '
. $parameter['default']['type']
. ' cannot be assigned to variable(array)',
$parameter
);
}
break;
case 'variable':
$symbolVariable = $compilationContext->symbolTable->getVariableForWrite(
$parameter['name'],
$compilationContext,
$parameter['default']
);
switch ($parameter['default']['type']) {
case 'static-constant-access':
/**
* Now I can write code for easy use on Expression because code in this method don't write with codePrinter ;(.
*
* TODO: Rewrite all to codePrinter
*/
$expression = new Expression($parameter['default']);
$expression->setExpectReturn(true, $symbolVariable);
$compiledExpression = $expression->compile($compilationContext);
$parameter['default']['type'] = $compiledExpression->getType();
$parameter['default']['value'] = $compiledExpression->getCode();
$compilationContext->codePrinter = $oldCodePrinter;
return $this->assignDefaultValue($parameter, $compilationContext);
case 'int':
case 'uint':
case 'long':
case 'ulong':
$compilationContext->symbolTable->mustGrownStack(true);
$compilationContext->headersManager->add('kernel/memory');
$compilationContext->backend->initVar($symbolVariable, $compilationContext);
$compilationContext->backend->assignLong(
$symbolVariable,
$parameter['default']['value'],
$compilationContext
);
break;
case 'double':
$compilationContext->symbolTable->mustGrownStack(true);
$compilationContext->headersManager->add('kernel/memory');
$compilationContext->backend->initVar($symbolVariable, $compilationContext);
$compilationContext->backend->assignDouble(
$symbolVariable,
$parameter['default']['value'],
$compilationContext
);
break;
case 'string':
$compilationContext->symbolTable->mustGrownStack(true);
$compilationContext->headersManager->add('kernel/memory');
$compilationContext->backend->initVar($symbolVariable, $compilationContext);
$compilationContext->backend->assignString(
$paramVariable,
Name::addSlashes($parameter['default']['value']),
$compilationContext
);
break;
case 'bool':
$expectedMutations = $compilationContext->symbolTable->getExpectedMutations($parameter['name']);
if ($expectedMutations < 2) {
if ('true' == $parameter['default']['value']) {
$compilationContext->backend->assignZval(
$paramVariable,
$compilationContext->backend->resolveValue('true', $compilationContext),
$compilationContext
);
} else {
$compilationContext->backend->assignZval(
$paramVariable,
$compilationContext->backend->resolveValue('false', $compilationContext),
$compilationContext
);
}
} else {
$compilationContext->symbolTable->mustGrownStack(true);
$compilationContext->headersManager->add('kernel/memory');
if ('true' == $parameter['default']['value']) {
$compilationContext->backend->copyOnWrite(
$paramVariable,
$compilationContext->backend->resolveValue('true', $compilationContext),
$compilationContext
);
} else {
$compilationContext->backend->copyOnWrite(
$paramVariable,
$compilationContext->backend->resolveValue('false', $compilationContext),
$compilationContext
);
}
}
break;
case 'null':
$expectedMutations = $compilationContext->symbolTable->getExpectedMutations($parameter['name']);
if ($expectedMutations < 2) {
$compilationContext->backend->assignZval(
$symbolVariable,
$compilationContext->backend->resolveValue('null', $compilationContext),
$compilationContext
);
} else {
$compilationContext->symbolTable->mustGrownStack(true);
$compilationContext->headersManager->add('kernel/memory');
$compilationContext->backend->copyOnWrite(
$paramVariable,
$compilationContext->backend->resolveValue('null', $compilationContext),
$compilationContext
);
}
break;
case 'empty-array':
$compilationContext->symbolTable->mustGrownStack(true);
$compilationContext->headersManager->add('kernel/memory');
$compilationContext->backend->initVar($symbolVariable, $compilationContext);
$compilationContext->backend->initArray($symbolVariable, $compilationContext);
break;
default:
throw new CompilerException(
'Default parameter value type: ' . $parameter['default']['type'] . ' cannot be assigned to variable(variable)',
$parameter
);
}
break;
default:
throw new CompilerException('Default parameter type: ' . $dataType, $parameter);
}
$compilationContext->codePrinter = $oldCodePrinter;
return $codePrinter->getOutput();
}
|
Assigns a default value.
@throws Exception
@throws ReflectionException
|
assignDefaultValue
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function assignZvalValue(array $parameter, CompilationContext $compilationContext): string
{
$dataType = $this->getParamDataType($parameter);
if (in_array($dataType, ['variable', 'callable', 'object', 'resource', 'mixed'])) {
return '';
}
$compilationContext->headersManager->add('kernel/operators');
$parameterVariable = $compilationContext->symbolTable->getVariableForWrite(
$parameter['name'] . '_param',
$compilationContext
);
$parameterCode = $compilationContext->backend->getVariableCode($parameterVariable);
$inputParamVar = $compilationContext->symbolTable->getVariableForWrite(
$parameter['name'],
$compilationContext
);
$inputParamCode = $compilationContext->backend->getVariableCode($inputParamVar);
switch ($dataType) {
case 'int':
case 'uint':
case 'long':
case 'ulong':
// Value already passed in `Z_PARAM_LONG()`
return '';
case 'char':
return "\t" . $parameter['name'] . ' = zephir_get_charval(' . $parameterCode . ');' . PHP_EOL;
case 'bool':
//return "\t" . $parameter['name'] . ' = zephir_get_boolval(' . $parameterCode . ');' . PHP_EOL;
return '';
case 'double':
return "\t" . $parameter['name'] . ' = zephir_get_doubleval(' . $parameterCode . ');' . PHP_EOL;
case 'string':
$compilationContext->symbolTable->mustGrownStack(true);
return "\t" . 'zephir_get_strval(' . $inputParamCode . ', ' . $parameterCode . ');' . PHP_EOL;
case 'array':
$compilationContext->symbolTable->mustGrownStack(true);
return "\t" . 'zephir_get_arrval(' . $inputParamCode . ', ' . $parameterCode . ');' . PHP_EOL;
default:
throw new CompilerException('Parameter type: ' . $dataType, $parameter);
}
}
|
Assigns a zval value to a static low-level type.
@throws CompilerException
|
assignZvalValue
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function checkStrictType(array $parameter, CompilationContext $compilationContext): string
{
$dataType = $this->getParamDataType($parameter);
$compilationContext->headersManager->add('ext/spl/spl_exceptions');
$compilationContext->headersManager->add('kernel/exception');
$codePrinter = new Printer();
$codePrinter->increaseLevel();
$oldCodePrinter = $compilationContext->codePrinter;
$compilationContext->codePrinter = $codePrinter;
$compilationContext->backend->checkStrictType($dataType, $parameter, $compilationContext);
$compilationContext->codePrinter = $oldCodePrinter;
return $codePrinter->getOutput();
}
|
Assigns a zval value to a static low-level type.
TODO: rewrite this to build ifs and throw from builders
@throws CompilerException
|
checkStrictType
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function checkVisibility(array $visibility, string $name, ?array $original = null): void
{
if (count($visibility) > 1) {
if (in_array('public', $visibility) && in_array('protected', $visibility)) {
throw new CompilerException(
"Method '$name' cannot be 'public' and 'protected' at the same time",
$original
);
}
if (in_array('public', $visibility) && in_array('private', $visibility)) {
throw new CompilerException(
"Method '$name' cannot be 'public' and 'private' at the same time",
$original
);
}
if (in_array('private', $visibility) && in_array('protected', $visibility)) {
throw new CompilerException(
"Method '$name' cannot be 'protected' and 'private' at the same time",
$original
);
}
if (in_array('private', $visibility) && in_array('internal', $visibility)) {
throw new CompilerException(
"Method '$name' cannot be 'internal' and 'private' at the same time",
$original
);
}
if (in_array('protected', $visibility) && in_array('internal', $visibility)) {
throw new CompilerException(
"Method '$name' cannot be 'internal' and 'protected' at the same time",
$original
);
}
if (in_array('public', $visibility) && in_array('internal', $visibility)) {
throw new CompilerException(
"Method '$name' cannot be 'internal' and 'public' at the same time",
$original
);
}
}
if ('__construct' === $name) {
if (in_array('static', $visibility)) {
throw new CompilerException("Constructors cannot be 'static'", $original);
}
} elseif ('__destruct' === $name) {
if (in_array('static', $visibility)) {
throw new CompilerException("Destructors cannot be 'static'", $original);
}
}
$this->isAbstract = in_array('abstract', $visibility);
$this->isStatic = in_array('static', $visibility);
$this->isFinal = in_array('final', $visibility);
$this->isPublic = in_array('public', $visibility);
$this->isInternal = in_array('internal', $visibility);
}
|
Checks for visibility congruence.
@throws CompilerException
|
checkVisibility
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function compile(CompilationContext $compilationContext): void
{
/**
* Set the method currently being compiled
*/
$compilationContext->currentMethod = $this;
/**
* Assign pre-made compilation passes.
*/
$typeInference = $this->typeInference;
$callGathererPass = $this->callGathererPass;
/**
* Initialization of parameters happens in a fictitious external branch.
*/
$branch = new Branch();
$branch->setType(Branch::TYPE_EXTERNAL);
/**
* BranchManager helps to create graphs of conditional/loop/root/jump branches.
*/
$branchManager = new BranchManager();
$branchManager->addBranch($branch);
$compilationContext->branchManager = $branchManager;
/**
* Every method has its own symbol table.
*/
$symbolTable = new SymbolTable($compilationContext);
if ($this->localContext instanceof LocalContextPass) {
$symbolTable->setLocalContext($this->localContext);
}
foreach ($this->staticVariables as $var) {
$localVar = clone $var;
$localVar->setIsExternal(true);
$localVar->setLocalOnly(true);
$localVar->setDynamicTypes($localVar->getType());
$localVar->setType('variable');
$localVar->setIsDoublePointer(false);
$symbolTable->addRawVariable($localVar);
}
/**
* Parameters has an additional extra mutation.
*/
if ($this->localContext instanceof LocalContextPass && $this->parameters instanceof Parameters) {
foreach ($this->parameters->getParameters() as $parameter) {
$this->localContext->increaseMutations($parameter['name']);
}
}
/**
* Cache Manager manages function calls, method calls and class entries caches.
*/
$cacheManager = new Manager();
$cacheManager->setGatherer($callGathererPass);
$compilationContext->cacheManager = $cacheManager;
$compilationContext->typeInference = $typeInference;
$compilationContext->symbolTable = $symbolTable;
$oldCodePrinter = $compilationContext->codePrinter;
/**
* Change the code printer to a single method instance.
*/
$codePrinter = new Printer();
$compilationContext->codePrinter = $codePrinter;
/**
* Set an empty function cache
*/
$compilationContext->functionCache = null;
/**
* Reset try/catch and loop counter
*/
$compilationContext->insideCycle = 0;
$compilationContext->insideTryCatch = 0;
$compilationContext->currentTryCatch = 0;
if ($this->parameters instanceof Parameters) {
/**
* Round 1. Create variables in parameters in the symbol table.
*/
$substituteVars = [];
foreach ($this->parameters->getParameters() as $parameter) {
$symbolParam = null;
if (isset($parameter['data-type'])) {
switch ($parameter['data-type']) {
case 'object':
case 'callable':
case 'resource':
case 'variable':
case 'mixed':
$symbol = $symbolTable->addVariable(
$parameter['data-type'],
$parameter['name'],
$compilationContext
);
/* TODO: Move this to the respective backend, which requires refactoring how this works */
$symbol->setIsDoublePointer(true);
$substituteVars[$parameter['name']] = $symbolTable->addVariable(
'variable',
$parameter['name'] . '_sub',
$compilationContext
);
break;
default:
$symbol = $symbolTable->addVariable(
$parameter['data-type'],
$parameter['name'],
$compilationContext
);
$symbolParam = $symbolTable->addVariable(
'variable',
$parameter['name'] . '_param',
$compilationContext
);
/* TODO: Move this to the respective backend, which requires refactoring how this works */
$symbolParam->setIsDoublePointer(true);
if ('string' == $parameter['data-type'] || 'array' == $parameter['data-type']) {
$symbol->setMustInitNull(true);
}
break;
}
} else {
$symbol = $symbolTable->addVariable('variable', $parameter['name'], $compilationContext);
}
/**
* ZE3 only
*/
if (isset($substituteVars[$parameter['name']])) {
$substituteVar = $substituteVars[$parameter['name']];
$substituteVar->increaseUses();
}
/**
* Some parameters can be read-only
*/
if (!empty($parameter['const'])) {
$symbol->setReadOnly(true);
if (is_object($symbolParam)) {
$symbolParam->setReadOnly(true);
}
}
if (is_object($symbolParam)) {
/**
* Parameters are marked as 'external'
*/
$symbolParam->setIsExternal(true);
/**
* Assuming they're initialized
*/
$symbolParam->setIsInitialized(true, $compilationContext);
/**
* Initialize auxiliary parameter zvals to null
*/
$symbolParam->setMustInitNull(true);
/**
* Increase uses
*/
$symbolParam->increaseUses();
} else {
if (isset($parameter['default'])) {
if (isset($parameter['data-type'])) {
if ('variable' === $parameter['data-type']) {
$symbol->setMustInitNull(true);
}
} else {
$symbol->setMustInitNull(true);
}
}
}
/**
* Original node where the variable was declared
*/
$symbol->setOriginal($parameter);
/**
* Parameters are marked as 'external'
*/
$symbol->setIsExternal(true);
/**
* Assuming they're initialized
*/
$symbol->setIsInitialized(true, $compilationContext);
/**
* Variables with class/type must be objects across the execution
*/
if (isset($parameter['cast'])) {
$symbol->setDynamicTypes('object');
$symbol->setClassTypes($compilationContext->getFullName($parameter['cast']['value']));
} else {
if (isset($parameter['data-type'])) {
if ('variable' === $parameter['data-type']) {
$symbol->setDynamicTypes('undefined');
}
} else {
$symbol->setDynamicTypes('undefined');
}
}
}
}
$compilationContext->backend->onPreCompile($this, $compilationContext);
/**
* Compile the block of statements if any
*/
if (is_object($this->statements)) {
$compilationContext->staticContext = $this->hasModifier('static');
/**
* Compile the statements block as a 'root' branch
*/
$this->statements->compile($compilationContext, false, Branch::TYPE_ROOT);
}
/**
* Initialize variable default values.
*/
$initVarCode = $compilationContext->backend->initializeVariableDefaults(
$symbolTable->getVariables(),
$compilationContext
);
/**
* Fetch parameters from vm-top.
*/
$initCode = '';
$code = '';
$requiredParams = [];
$optionalParams = [];
if ($this->parameters instanceof Parameters) {
/**
* Round 2. Fetch the parameters in the method.
*/
$params = $this->parameters->fetchParameters($this->isInternal);
$numberRequiredParams = $this->parameters->countRequiredParameters();
$numberOptionalParams = $this->parameters->countOptionalParameters();
$requiredParams = $this->parameters->getRequiredParameters();
$optionalParams = $this->parameters->getOptionalParameters();
/**
* Pass the write detector to the method statement block to check if the parameter
* variable is modified so as do the proper separation.
*/
$parametersToSeparate = [];
if (is_object($this->statements)) {
if (!$this->localContext instanceof LocalContextPass) {
$writeDetector = new WriteDetector();
}
foreach ($this->parameters->getParameters() as $parameter) {
$dataType = $parameter['data-type'] ?? 'variable';
switch ($dataType) {
case 'variable':
case 'string':
case 'array':
case 'resource':
case 'object':
case 'callable':
$name = $parameter['name'];
if (!$this->localContext instanceof LocalContextPass) {
if ($writeDetector->detect($name, $this->statements->getStatements())) {
$parametersToSeparate[$name] = true;
}
} else {
if ($this->localContext->getNumberOfMutations($name) > 1) {
$parametersToSeparate[$name] = true;
}
}
break;
}
}
}
/**
* Initialize required parameters
*/
foreach ($requiredParams as $parameter) {
$mandatory = $parameter['mandatory'] ?? 0;
$dataType = $this->getParamDataType($parameter);
if ('variable' !== $dataType) {
/**
* Assign value from zval to low level type
*/
if ($mandatory) {
$initCode .= $this->checkStrictType($parameter, $compilationContext);
} else {
$initCode .= $this->assignZvalValue($parameter, $compilationContext);
}
}
switch ($dataType) {
case 'variable':
case 'resource':
case 'object':
case 'callable':
if (isset($parametersToSeparate[$parameter['name']])) {
$symbolTable->mustGrownStack(true);
$initCode .= "\t" . 'ZEPHIR_SEPARATE_PARAM(' . $parameter['name'] . ');' . PHP_EOL;
}
break;
}
}
/**
* Initialize optional parameters
*/
foreach ($optionalParams as $parameter) {
$mandatory = $parameter['mandatory'] ?? 0;
$dataType = $this->getParamDataType($parameter);
$name = match ($dataType) {
'object',
'callable',
'resource',
'variable',
'mixed' => $parameter['name'],
default => $parameter['name'] . '_param',
};
/**
* Assign the default value according to the variable's type.
*/
$targetVar = $compilationContext->symbolTable->getVariableForWrite($name, $compilationContext);
$initCode .= "\t" . $compilationContext->backend->ifVariableValueUndefined(
$targetVar,
$compilationContext,
false,
false
) . PHP_EOL;
if ($targetVar->isDoublePointer() && isset($substituteVars[$parameter['name']])) {
$substituteVar = $substituteVars[$parameter['name']];
$initCode .= "\t\t"
. $targetVar->getName()
. ' = &'
. $substituteVar->getName()
. ';'
. PHP_EOL;
}
$initCode .= $this->assignDefaultValue($parameter, $compilationContext);
if (isset($parametersToSeparate[$name]) || 'variable' !== $dataType) {
$initCode .= "\t" . '} else {' . PHP_EOL;
if (isset($parametersToSeparate[$name])) {
$initCode .= "\t\t" . 'ZEPHIR_SEPARATE_PARAM(' . $name . ');' . PHP_EOL;
} else {
if ($mandatory) {
$initCode .= $this->checkStrictType($parameter, $compilationContext);
} else {
$initCode .= "\t" . $this->assignZvalValue($parameter, $compilationContext);
}
}
}
$initCode .= "\t" . '}' . PHP_EOL;
}
/**
* Fetch the parameters to zval pointers
*/
$codePrinter->preOutputBlankLine();
if (!$this->isInternal()) {
$compilationContext->headersManager->add('kernel/memory');
if ($symbolTable->getMustGrownStack()) {
$code .= "\t"
. 'zephir_fetch_params(1, '
. $numberRequiredParams
. ', '
. $numberOptionalParams
. ', '
. implode(', ', $params)
. ');'
. PHP_EOL;
} else {
$code .= "\t"
. 'zephir_fetch_params_without_memory_grow('
. $numberRequiredParams
. ', '
. $numberOptionalParams
. ', '
. implode(', ', $params)
. ');'
. PHP_EOL;
}
} else {
foreach ($params as $param) {
/* TODO: Migrate all this code to codeprinter, get rid of temp code printer */
$tempCodePrinter = new Printer();
$realCodePrinter = $compilationContext->codePrinter;
$compilationContext->codePrinter = $tempCodePrinter;
$paramVar = $compilationContext->symbolTable->getVariableForRead(
$param,
$compilationContext
);
$compilationContext->backend->assignZval($paramVar, $param . '_ext', $compilationContext);
$code .= "\t" . $tempCodePrinter->getOutput() . PHP_EOL;
$compilationContext->codePrinter = $realCodePrinter;
}
}
}
$code .= $initCode . $initVarCode;
$codePrinter->preOutput($code);
$compilationContext->headersManager->add('kernel/object');
/**
* Fetch used superglobals
*/
foreach ($symbolTable->getVariables() as $name => $variable) {
if ($variable->isSuperGlobal()) {
$globalVar = $symbolTable->getVariable($name);
$codePrinter->preOutput(
"\t" . $compilationContext->backend->fetchGlobal($globalVar, $compilationContext, false)
);
}
if ($variable->isLocalStatic()) {
$staticVar = $symbolTable->getVariable($name);
$codePrinter->preOutput(
sprintf(
"\t" . 'zephir_read_static_property_ce(&%s, %s, SL("%s"), PH_NOISY_CC%s);',
$staticVar->getName(),
$this->classDefinition->getClassEntry(),
$staticVar->getName(),
''
)
);
}
}
/**
* Grow the stack if needed
*/
if ($symbolTable->getMustGrownStack()) {
$compilationContext->headersManager->add('kernel/memory');
if (!$compilationContext->symbolTable->hasVariable('ZEPHIR_METHOD_GLOBALS_PTR')) {
$methodGlobals = new Variable('zephir_method_globals', 'ZEPHIR_METHOD_GLOBALS_PTR', $compilationContext->branchManager->getCurrentBranch());
$methodGlobals->setMustInitNull(true);
$methodGlobals->increaseUses();
$methodGlobals->setReusable(false);
$methodGlobals->setReadOnly(true);
$methodGlobals->setUsed(true);
$compilationContext->symbolTable->addRawVariable($methodGlobals);
}
// #define ZEPHIR_MM_GROW()
$codePrinter->preOutput("\t" . 'zephir_memory_grow_stack(ZEPHIR_METHOD_GLOBALS_PTR, __func__);');
$codePrinter->preOutput("\t" . 'ZEPHIR_METHOD_GLOBALS_PTR = pecalloc(1, sizeof(zephir_method_globals), 0);');
}
/**
* Check if there are unused variables.
*/
$usedVariables = [];
$completeName = $this->getClassDefinition()?->getCompleteName() ?: '[unknown]';
$thisUnused = false;
foreach ($symbolTable->getVariables() as $variable) {
if ($variable->getNumberUses() <= 0) {
if (!$variable->isExternal()) {
$compilationContext->logger->warning(
'Variable "'
. $variable->getName()
. '" declared but not used in '
. $completeName
. '::'
. $this->getName(),
['unused-variable', $variable->getOriginal()]
);
continue;
}
$compilationContext->logger->warning(
'Variable "'
. $variable->getName()
. '" declared but not used in '
. $completeName
. '::'
. $this->getName(),
['unused-variable-external', $variable->getOriginal()]
);
}
if (
'this_ptr' !== $variable->getName() &&
'return_value' !== $variable->getName() &&
'return_value_ptr' !== $variable->getName()
) {
$type = $variable->getType();
if (!isset($usedVariables[$type])) {
$usedVariables[$type] = [];
}
$usedVariables[$type][] = $variable;
}
if ('this_ptr' === $variable->getName() && !$variable->isUsed()) {
$thisUnused = true;
}
}
/**
* Check if there are assigned but not used variables
* Warn whenever a variable is unused aside from its declaration.
*/
foreach ($symbolTable->getVariables() as $variable) {
if ($variable->isExternal() || $variable->isTemporal()) {
continue;
}
if (
'this_ptr' === $variable->getName() ||
'return_value' === $variable->getName() ||
'return_value_ptr' === $variable->getName() ||
'ZEPHIR_LAST_CALL_STATUS' === $variable->getName()
) {
continue;
}
if (!$variable->isUsed()) {
$node = $variable->getLastUsedNode();
if (is_array($node)) {
$expression = $node['expr'] ?? $node;
$compilationContext->logger->warning(
'Variable "'
. $variable->getName()
. '" assigned but not used in '
. $completeName
. '::'
. $this->getName(),
['unused-variable', $expression]
);
} else {
$compilationContext->logger->warning(
'Variable "'
. $variable->getName()
. '" assigned but not used in '
. $completeName
. '::'
. $this->getName(),
['unused-variable', $variable->getOriginal()]
);
}
}
}
if (count($usedVariables)) {
$codePrinter->preOutputBlankLine();
}
/**
* ZEND_PARSE_PARAMETERS
*/
$tempCodePrinter = new Printer();
if ($this->parameters instanceof Parameters && $this->parameters->count() > 0) {
// Do not declare variable when it is not needed.
if ($this->parameters->hasNullableParameters()) {
$tempCodePrinter->output("\t" . 'bool is_null_true = 1;');
}
$tempCodePrinter->output(
sprintf(
"\t" . 'ZEND_PARSE_PARAMETERS_START(%d, %d)',
$this->parameters->countRequiredParameters(),
$this->parameters->count()
)
);
foreach ($requiredParams as $requiredParam) {
$tempCodePrinter->output("\t\t" . $this->detectParam($requiredParam, $compilationContext));
}
if (!empty($optionalParams)) {
$tempCodePrinter->output("\t\t" . 'Z_PARAM_OPTIONAL');
foreach ($optionalParams as $optionalParam) {
$tempCodePrinter->output("\t\t" . $this->detectParam($optionalParam, $compilationContext));
}
}
$tempCodePrinter->output("\t" . 'ZEND_PARSE_PARAMETERS_END();');
}
$codePrinter->preOutput($tempCodePrinter->getOutput());
/**
* Generate the variable definition for variables used.
*/
$initCode = sprintf(
"\t%s",
implode(
PHP_EOL . "\t",
$compilationContext->backend->declareVariables(
$this,
$usedVariables
)
)
);
$codePrinter->preOutput($initCode);
/**
* Finalize the method compilation
*/
if (is_object($this->statements) && !empty($statement = $this->statements->getLastStatement())) {
/**
* If the last statement is not a 'return' or 'throw' we need to
* restore the memory stack if needed.
*/
$lastType = $this->statements->getLastStatementType();
if (
'return' !== $lastType &&
'throw' !== $lastType &&
!$this->hasChildReturnStatementType($statement)
) {
if ($symbolTable->getMustGrownStack()) {
$compilationContext->headersManager->add('kernel/memory');
$codePrinter->output("\t" . 'ZEPHIR_MM_RESTORE();');
}
/**
* If a method has return-type hints we need to ensure the last
* statement is a 'return' statement
*/
if ($this->hasReturnTypes()) {
throw new CompilerException(
'Reached end of the method without returning a valid type specified in the return-type hints',
$this->expression['return-type']
);
}
}
}
$compilationContext->backend->onPostCompile($this, $compilationContext);
/**
* Remove macros that grow/restore the memory frame stack if it wasn't used.
*/
$code = $this->removeMemoryStackReferences($symbolTable, $codePrinter->getOutput());
/**
* Remove unused this_ptr variable.
*/
if ($thisUnused) {
$code = str_replace("\t" . 'zval *this_ptr = getThis();' . PHP_EOL, '', $code);
}
$code = preg_replace("/(\R){3,}/", "$1", $code);
/**
* Restore the compilation context
*/
$oldCodePrinter->output($code, false);
$compilationContext->codePrinter = $oldCodePrinter;
$compilationContext->branchManager = null;
$compilationContext->cacheManager = null;
$compilationContext->typeInference = null;
$codePrinter->clear();
}
|
Compiles the method.
@throws Exception
@throws ReflectionException
|
compile
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function getArgInfoName(?Definition $classDefinition = null): string
{
if ($classDefinition instanceof Definition) {
return sprintf(
'arginfo_%s_%s_%s',
strtolower($classDefinition->getCNamespace()),
strtolower($classDefinition->getName()),
strtolower($this->getName())
);
}
return sprintf('arginfo_%s', strtolower($this->getInternalName()));
}
|
Returns arginfo name for current method.
|
getArgInfoName
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function getCallGathererPass(): CallGathererPass
{
return $this->callGathererPass;
}
|
Returns the call gatherer pass information.
|
getCallGathererPass
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function getClassDefinition(): ?Definition
{
return $this->classDefinition;
}
|
Returns the class definition where the method was declared.
|
getClassDefinition
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function getLine(): mixed
{
return $this->expression['line'];
}
|
the starting line of the method in the source file.
|
getLine
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function getModifiers(): string
{
$modifiers = [];
foreach ($this->visibility as $visibility) {
switch ($visibility) {
case 'public':
$modifiers['ZEND_ACC_PUBLIC'] = $visibility;
break;
case 'protected':
$modifiers['ZEND_ACC_PROTECTED'] = $visibility;
break;
case 'private':
$modifiers['ZEND_ACC_PRIVATE'] = $visibility;
break;
case 'static':
$modifiers['ZEND_ACC_STATIC'] = $visibility;
break;
case 'final':
$modifiers['ZEND_ACC_FINAL'] = $visibility;
break;
case 'abstract':
$modifiers['ZEND_ACC_ABSTRACT'] = $visibility;
break;
case 'deprecated':
$modifiers['ZEND_ACC_DEPRECATED'] = $visibility;
break;
case 'inline':
case 'scoped':
case 'internal':
break;
default:
throw new Exception('Unknown modifier "' . $visibility . '"');
}
}
if ('__construct' === $this->name) {
$modifiers['ZEND_ACC_CTOR'] = true;
} elseif ('__destruct' === $this->name) {
$modifiers['ZEND_ACC_DTOR'] = true;
}
return implode('|', array_keys($modifiers));
}
|
Returns the C-modifier flags.
@throws Exception
|
getModifiers
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function getNumberOfParameters(): int
{
if ($this->parameters instanceof Parameters) {
return $this->parameters->count();
}
return 0;
}
|
Returns the number of parameters the method has.
|
getNumberOfParameters
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function getNumberOfRequiredParameters(): int
{
if ($this->parameters === null) {
return 0;
}
$required = 0;
foreach ($this->parameters->getParameters() as $parameter) {
if (!isset($parameter['default'])) {
++$required;
}
}
return $required;
}
|
Returns the number of required parameters the method has.
|
getNumberOfRequiredParameters
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function getReturnClassTypes(): array
{
return $this->returnClassTypes;
}
|
Returned class-type hints by the method.
|
getReturnClassTypes
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function getReturnTypes(): array
{
return $this->returnTypes;
}
|
Returned type hints by the method.
|
getReturnTypes
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function hasChildReturnStatementType(array $statement): bool
{
if (!isset($statement['statements']) || !is_array($statement['statements'])) {
return false;
}
if ('if' === $statement['type']) {
$ret = false;
$statements = $statement['statements'];
foreach ($statements as $item) {
$type = $item['type'] ?? null;
if ('return' === $type || 'throw' === $type) {
$ret = true;
} else {
$ret = $this->hasChildReturnStatementType($item);
}
}
if (!$ret || !isset($statement['else_statements'])) {
return false;
}
$statements = $statement['else_statements'];
} else {
$statements = $statement['statements'];
}
foreach ($statements as $item) {
$type = $item['type'] ?? null;
if ('return' === $type || 'throw' === $type) {
return true;
}
return $this->hasChildReturnStatementType($item);
}
return false;
}
|
Simple method to check if one of the paths are returning the right expected type.
|
hasChildReturnStatementType
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function hasModifier(string $modifier): bool
{
return in_array($modifier, $this->visibility);
}
|
Checks whether the method has a specific modifier.
|
hasModifier
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function hasParameters(): bool
{
return $this->parameters instanceof Parameters && $this->parameters->count() > 0;
}
|
Returns the number of parameters the method has.
|
hasParameters
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function hasReturnTypes(): bool
{
return count($this->returnTypes) || count($this->returnClassTypes);
}
|
Checks if the method has return-type or cast hints.
|
hasReturnTypes
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function isBundled(): bool
{
return $this->isBundled;
}
|
Checks whether the method is bundled.
|
isBundled
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function isConstructor(): bool
{
return '__construct' === $this->name;
}
|
Check whether the current method is a constructor.
|
isConstructor
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function isEmpty(): bool
{
return $this->statements->isEmpty();
}
|
Checks whether the method is empty.
|
isEmpty
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function isFinal(): bool
{
return $this->isFinal;
}
|
Checks whether the method is final.
|
isFinal
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function isInitializer(): bool
{
return $this->isInitializer;
}
|
Checks whether the method is an initializer.
|
isInitializer
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function isInternal(): bool
{
return $this->isInternal;
}
|
Checks whether the method is internal.
|
isInternal
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function isMixed(): bool
{
return $this->mixed;
}
|
Checks if the methods return type is `mixed`.
|
isMixed
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function isPrivate(): bool
{
return in_array('private', $this->visibility);
}
|
Checks if the method is private.
|
isPrivate
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function isProtected(): bool
{
return in_array('protected', $this->visibility);
}
|
Checks if the method is protected.
|
isProtected
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function isPublic(): bool
{
return $this->isPublic;
}
|
Checks if the method is public.
|
isPublic
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function isReturnTypeNullableObject(): bool
{
return count($this->returnTypes) === 2
&& isset($this->returnTypes['object'])
&& isset($this->returnTypes['null']);
}
|
Checks if method's return type is nullable object `?object`.
|
isReturnTypeNullableObject
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function isReturnTypeObject(): bool
{
return count($this->returnTypes) === 1 && isset($this->returnTypes['object']);
}
|
Checks if method's return type is object `object`.
|
isReturnTypeObject
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function isReturnTypesHintDetermined(): bool
{
if ($this->isVoid()) {
return true;
}
if (0 === count($this->returnTypes)) {
return false;
}
foreach ($this->returnTypes as $returnType => $definition) {
switch ($returnType) {
case 'variable':
case 'callable':
case 'resource':
return false;
}
if (isset($definition['type']) && 'return-type-annotation' === $definition['type']) {
if (
$this->areReturnTypesBoolCompatible() ||
$this->areReturnTypesDoubleCompatible() ||
$this->areReturnTypesIntCompatible() ||
$this->areReturnTypesNullCompatible() ||
$this->areReturnTypesStringCompatible() ||
$this->areReturnTypesFalseCompatible() ||
$this->areReturnTypesObjectCompatible() ||
array_key_exists('array', $this->getReturnTypes())
) {
continue;
}
/**
* TODO: Probably we should detect return type more more carefully.
* It is hard to process return type from the annotations at this time.
* Thus we just return false here.
*/
return false;
}
}
return true;
}
|
Is method have determined return type hint.
This method is used to generate:
- ZEND_BEGIN_ARG_INFO_EX
- ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX
Examples:
- TRUE: function foo() -> void;
- TRUE: function foo() -> null;
- TRUE: function foo() -> bool|string|...;
- TRUE: function foo() -> <\stdClass>;
- FALSE: function foo();
- FALSE: function foo() -> var;
- FALSE: function foo() -> resource|callable;
|
isReturnTypesHintDetermined
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
public function isStatic(): bool
{
return $this->isStatic;
}
|
Checks whether the method is static.
|
isStatic
|
php
|
zephir-lang/zephir
|
src/Class/Method/Method.php
|
https://github.com/zephir-lang/zephir/blob/master/src/Class/Method/Method.php
|
MIT
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.