| > The calling convention for C++ non-static member functions always includes a this pointer, even if the function does not use it. Yes. Since we are not in C++ we can choose to get rid of this useless pointer. > Removing it on member functions that do not use it would pose a problem if another class inherited from this class and overrode the function definition with one that did use it. That problem has nothing to do with the this pointer specifically. When you change the method signature of an inherited method you always have this problem. This simply means, that the superclass prescribes limits to subclasses, which is why it's possible to use a subclass inplace of a superclass. > Maybe in very special cases whole program optimization could safely remove the this pointer, but it is questionable whether any compiler author Yes, that's why its not done in C++, but we can do it, if we handroll it. > it would break ABI stability It does not if it has always been like this. > For static member functions, inheritance is irrelevant since they are tied to the class. Calls to static member functions go directly to the mangled function with no indirections In other words, ->check_flags() can't be implemented as a static member functions in C++. It would simply have a this pointer, that it just wouldn't use, since C++ has no way to express non-static member functions, that just don't take a this pointer. > thus can tell the linker to have calls there go to the function In our case the linker can only resolve the call to the appropriate vtable, since the type isn't known until runtime. |
If you were trying to implement OOP in the kernel in C and implemented a vtable, you cannot get rid of the this pointer in vtable entries since a child class might want to use it in the overrode definition. It is one of the same reasons why you cannot remove it in C++. The entire point of a vtable is to enable inheritance. If OOP really were being done, an out of tree module could make a class that inherits from this one without needing any code changes and use the this pointer, but you cannot do that if you drop the this pointer. I already explained this.