4 ms·
I assume his point was that there's no technical reason for C to not support it.
by stefncb 3y ago
I assume his point was that there's no technical reason for C to not support it.
- mhh__ 3y agoThe technical reason is that it makes the compiler a lot more complicated.
- badsectoracula 3y agoI've implemented this for calls in a single-pass compiler and it is really simple. You just treat the yet-undeclared call as if it was used correctly and add it to a list of calls to check later. Compared to everything else a compiler needs to do this is trivial. Doing it for types is harder though as far as single-pass compilers are concerned. Both are much easier if the compiler builds a syntax tree (or at least a list of all declarations even if the function bodies are handled at a later pass) ahead of time though.
- moonchild 3y agoIt hinders single-pass in just the same fashion as types do. Consider: f() { g(1); } g(x) double x; { printf("%lf\n", x); } You have to know the type of g's parameter before you can generate code for f.
- proto_lambda 3y ago(that function definition would be "void g(double x) { ... }" in non-obsolete C)
- rand_flip_bit 3y agoAs another commenter pointed out, this is obsolete C, however: this is just not how modern compilers work. at all. Compilation is performed in phases, you have syntax analysis, semantic analysis, and then optimization and codegen. It’s trivial for a compiler to handle this code, heck C++ compilers (which your C compiler is under the hood) already do this for struct/classes (allowing member functions to be called before being declared). Pretty much every other language supports this too, but universally and not just in classes. We’ve come a long way since the 60’s.
- Someone 3y ago> Compilation is performed in phases In this context, it isn’t. The comment you replied to was a response to the statement: > I've implemented this for calls in a single-pass compiler and it is really simple. You just treat the yet-undeclared call as if it was used correctly and add it to a list of calls to check later.
- rand_flip_bit 3y agoLooks like you missed the point.
- moonchild 3y agoThe k&r-style function definitions apparently amused only me, but are beside the point regardless. Single-pass compilation is very pertinent. One tenet of c's design is that it should be compilable in a single pass (which is why it is compilable in a single pass, unlike many other programming languages). This is relevant to us because it allows us to make predictions about the language; in particular, if the addition of some feature would cause c to not be compilable in a single pass, we can predict that the feature will not be added to the language. Notice that I have not made any value judgments. You may disagree about the importance of supporting single-pass compilers, but I am not the person you need to convince of that.
- megous 3y agoThat doesn't work if definition is part of a different compilation unit. 99% of functions in C will be (libc, other system libraries you may be using, etc.).
- mhh__ 3y agoIt's the checking later bit that can cause issues. You need to be able to (ideally) treat compilation as a dynamic schedule rather than a static list of passes.
- moonchild 3y agoD needs to do that because it's weird. I don't see why c would, in order to have forward references. It doesn't do inference or have any fancy compile time features.
- deleted 3y ago[deleted]
- mhh__ 3y agoI guess but allow me to nudge towards the more interesting problem rather than just writing 1 more ISO C compiler with a slight modification.
- pornel 3y agoComplicated for the single-pass PDP-11 compiler. Everything else that can fit AST in RAM can do it relatively easily. Modern C compilers have millions of lines and hundreds of person-years of work in them, so two passes and a lookup table is not a big deal. It's just not done because the C standard doesn't say it should. Perhaps there is some preprocessor crime that this isn't compatible with, or they just don't want to break that PDP-11 compatibility.
- phoe-krk 3y ago"There is no point in not supporting it" is not "it is supported" though. The former is an opinion, the latter is simply false.
- mistercheph 3y agoAnd the former is the meaning of TFA, the latter is a fictional alternative intention of TFA where it fails to meet the standards of truth.