The code generator was assuming all declarations for the current
file sit in the same namepace. Now uses the "on demand" namespace
switching we had for the forward declarations.
Also fixed a bug related to namespace lookup.
Change-Id: Ib54a3efbc752cbb9590302fa0707c0c73448db3d
Tested: on Linux.