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

Add the ability to cast an object to mixed type #890

Open
mkornaukhov03 opened this issue Aug 22, 2023 · 2 comments
Open

Add the ability to cast an object to mixed type #890

mkornaukhov03 opened this issue Aug 22, 2023 · 2 comments
Labels
compiler Feature related to compiler enhancement New feature or request missing functionality Missing PHP functions, classes, constants and etc

Comments

@mkornaukhov03
Copy link
Contributor

mkornaukhov03 commented Aug 22, 2023

<?php
class A { }

function foo(mixed $x) {
	if ($x instanceof A) {
		echo "A" . "\n";
	}
	else {
		echo "Not A" . "\n";
	}
}

foo(new A);
foo(42);

For now, KPHP shows the following error:

Compilation error at stage: Calc actual calls, gen by type-inferer.cpp:53
  index.php:5  in foo
    if ($x instanceof A) {

pass mixed to argument $instance of instance_cast
but it's declared as @param object

  index.php:5  in foo
    if ($x instanceof A) {
    $x is mixed

  index.php:4  in foo
    function foo(mixed $x) {
    argument $x declared as mixed

This functionality assumes two tasks:

  1. Track all the places where object is cast to mixed
  2. Change the mixed implementation

Talking about mixed implementation, we should add OBJECT value to mixed::type.
In layout: mixed.storage should contain a pointer to an object.
Key note: for all classes, that are cast to mixed, we should generate the following code(A class from previous snippet:

struct VirtualBaseForMixedClass { virtual ~VirtualBaseForMixedClass() = default; };
...
struct C$A : VirtualBaseForMixedClass { ~C$A() {} };

It will allow us to do the following:

$x instanceof A

--> (pseudocode)

auto* $tmp = dynamic_cast<rhs.as_instanceof()::class_type *>(reinterpret_cast<VirtualBaseForMixedClass*>(lhs.as_mixed().storage))
if ($tmp == nullptr) { error(...) }
@mkornaukhov03 mkornaukhov03 added enhancement New feature or request missing functionality Missing PHP functions, classes, constants and etc compiler Feature related to compiler labels Aug 22, 2023
@vkaverin
Copy link

But why? The fact that a class instance cannot be mixed was a fundamental statement for KPHP for years, why break this rule now?

@mkornaukhov03
Copy link
Contributor Author

But why? The fact that a class instance cannot be mixed was a fundamental statement for KPHP for years, why break this rule now?

It allows a bigger subset of PHP to be compiled. But for now it's only idea about its implementation in runtime, not about type system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler Feature related to compiler enhancement New feature or request missing functionality Missing PHP functions, classes, constants and etc
Projects
None yet
Development

No branches or pull requests

2 participants