Stan language .def files

Hi,

Can someone tell me the correct way to include stan’s language sources? It seems to be impossible to include it in multiple translation units as the header sources (especially ones with suffix _def.hpp) define function code.

So… this is a bit complicated. Maybe provide a little more context?

Stan’s current compiler, written in C++, should be capable of being built in one translation unit. But there are practical reasons why this doesn’t work: we blow out the memory of compilers. Where this trips depends on the compiler used and interacts with the optimization level.

What we do now is explicit template instantiation for different parts of the compiler. Those template instantiations are in their own translation units. This is all controlled through the make build process.

So… back to your question… you might run into a conflict with the one definition rule if you include the source multiple times. I think you might be able to avoid the problem if you’re careful about how you link it, but I think that’s linker-dependent on how that works.

Does that help? It’d help me if I understood a little bit more about what you’re trying to do.

I would like to refactor the stan language unit tests to not use ast_def.cpp, which is not ODR-safe. This is preventing me from collecting all the language tests into a single test binary.

I’m not sure what you’re after. We needed to break the language into multiple independent translation units because otherwise it was too big for the Windows C++ tool chain.

As is, they’re done in the standard way:

  • foo.hpp: pure header file
  • foo_def.hpp: header-only definition file
  • foo_inst.cpp: instantiations necessary for linking

When compiling in multiple transliation units, foo_inst.cpp is included in exactly one translation unit and all others translation units include foo.hpp.

For a pure header-only build in a single translation unit, everyone can just include foo_def.hpp.