[Parser] Add support for covariant return types in overriden functions
Right now, if class B derives from A, and overrides a function from A while changing the return type of the function, the ZHL parser rejects the override as ill-formed. According to the rules of C++, virtual overrides are allowed to change the return type to any type that is covariant with the original type.
Covariance in C++ restricted to ZHL can be reduced to "the return type of the function in A is a base of the return type of the function in B".
Can you explain this more with a code sample? Curious if maybe I've run into this already in ours.
I don't think you ran into this issue. I rewrote the entire ZHL parser in C++, using ANTLR (considering moving it to bison so we don't have to build ANTLR each time we do a release) because I found lpeg horrendous to use, and its error reporting awful. In FTL I see you still have the original lpeg based parser.
In our version of the parser, we can put a special block in a class definition to define the vtable. This allows the parser to do all kind of checks regarding the validity of overrides in child classes, ensure we don't call pure virtual functions, etc.
Here's an example. Given this code in an executable we hook:
class A { };
class B : public A { };
class C {
public:
virtual A* foo();
};
class D : public C {
public:
virtual B* foo() override;
};
We can have the following in ZHL files:
class A {};
class B : public A {};
class C depends (A) {
__vtable {
"<signature here>":
A* foo();
}
};
class D depends(B) : public C {
__vtable {
override
"<signature here>":
B* foo();
}
};
Despite this being valid C++, our parser screams because the return type of foo in D is not exactly the same as in C. The Lua parser doesn't do this kind of checks, that's why I think you probably never ran into this issue.