What is the compilation model of D?
David Piepgrass
qwertie256 at gmail.com
Tue Jul 24 17:16:04 PDT 2012
(Maybe this should be in D.learn but it's a somewhat advanced
topic)
I would really like to understand how D compiles a program or
library. I looked through TDPL and it doesn't seem to say
anything about how compilation works.
- Does it compile all source files in a project at once?
- Does the compiler it have to re-parse all Phobos templates (in
modules used by the program) whenever it starts?
- Is there any concept of an incremental build?
- Obviously, one can set up circular dependencies in which the
compile-time meaning of some code in module A depends on the
meaning of some code in module B, which in turn depends on the
meaning of some other code in module A. Sometimes the D compiler
can resolve the ultimate meaning, other times it cannot. I was
pleased that the compiler successfully understood this:
// Y.d
import X;
struct StructY {
int a = StructX().c;
auto b() { return StructX().d(); }
}
// X.d
import Y;
struct StructX {
int c = 3;
auto d()
{
static if (StructY().a == 3 && StructY().a.sizeof == 3)
return 3;
else
return "C";
}
}
But what procedure does the compiler use to resolve the semantics
of the code? Is there a specification anywhere? Does it have some
limitations, such that there is code with an unambiguous meaning
that a human could resolve but the compiler cannot?
- In light of the above (that the meaning of D code can be
interdependent with other D code, plus the presence of mixins and
all that), what are the limitations of __traits(allMembers...)
and other compile-time reflection operations, and what kind of
problems might a user expect to encounter?
More information about the Digitalmars-d
mailing list