Building records from modules.

This commit does most of the work but does not introduce the
feature yet. It can be seen as a refactoring.

This patch and design is joint work with Daniel Gustafsson.
The design is based on discussions with Ulf Norell.