DFHack / dfhack

Memory hacking library for Dwarf Fortress and a set of tools that use it
Other
1.84k stars 463 forks source link

constify identities #4702

Closed ab9rf closed 3 weeks ago

ab9rf commented 3 weeks ago

This PR marks all of the core data identity classes as const to reflect that these types are effectively immutable.

As noted elsewhere, compound_identity has a small degree of technical mutability due to lazy initialization, and virtual_identity has mutability due to its dual purpose for implementing vtable interposes; these are both accommodated by marking the related class members as mutable. This results in some const_casts, in the compound_identity and struct_identity constructors, and in a couple of places in code related to virtual identities. There are also several const_casts associated with storing type identity pointers into Lua metatables and lightuserdata objects, which are downcasts into void* and thus have to be const_casted to maintain correctness. Some of these are arguably code smells arguing for a redesign of the code in question, but that's for a future PR.

I also rewrote a couple of methods in VMethodInterposeLinkBase to avoid using map::operator[] (because the latter breaks constness); this has the side effect that searching for a vmethod by index will no longer insert an empty pointer into a virtual class's vtable interpose map on a lookup for a previously undefined index.

While these changes will enable making a significant fraction of data_identity instances constinit or constexpr (which has advantages in terms of more predictable static initialization, faster startup, and possibly other optimizations), such changes are not included in this PR.