(#14985) Enable calling_class and calling_module for defined types

This enables calling_class and calling_module for defined resource types. The
code had been disconnected when puppet started using `[]` on
`Puppet::Parser::Scope`, and has been re-enabled. In addition it now uses the
mechanism for finding the calling module by using similar information as used
by the scope for the `$caller_module_name` variable (introduced in commit
50a626daa1bef956ea63c405fddeaeab8a9a0756).

In order to understand how to test this and if it was relying on the correct
information some refactors to the variable lookup code was also done to attempt
to make it easier to understand the rules of lookup as expressed in the code.