c++ - Why do compilers only look backward for type and function declarations? -


This is entirely to satisfy my own curiosity, but how and how to solve functions and types against those definitions ago << p>

Sometimes it looks like the function needs to call each other:

 < code> zero foo (int depth) {bar (depth + 2); } Zero times (int depth) {if (depth; gt; 0) foo (depth - 3); }   

Where you need to move the bar before either foo, or announce the bar already:

  void bar (int ); Zero foo (int depth) {bar (depth + 2); } Zero times (int depth) {if (depth; gt; 0) foo (depth - 3); }   

In more complex examples, you may need to review #include trees and #ifndef guards to find out if your code defines the function or type in any other file Why not, and if you already know that you should check for it.


And then it's definitely classic:

  typedef struct {node * next; } Node;   

Where you need to know that it is possible:

  typedef struct node {struct node * next; } Node;   

... But apparently many people do not do that they use 'zero *' or 'structure node' everywhere.


So one of the reasons there is that these compilers do not solve even further? I can understand the preprocessor that things can be emptied only after the last check, but when a type or function is declared once it is good and can not be overloaded by definition later is.

Is it for historical reasons, with limited technology which was available when they first developed? Or is there any logical ambiguity I am missing?

The answer to your question is simply "If this does not happen then it is very easy to write a compiler It would be difficult- "The language specification says that it should be done in such a way, but the reason for this word in the language specification is that" it makes it easy to write a compiler route ". To a lesser extent, it is also possible that in the olden days, as the compiler code, "they will accompany", and all the code will not be read before in a translation unit (source file), then process it.

Keep in mind that the C and C ++ compilers are still being used on machines that do not have large amounts of memory and are very fast processors. Therefore, if you try to compile a large quantity of code on a small capacity machine, then "we do not want to read all the sources before," to tell it, the approach is 16 GB, more than the quad-core desktop It makes sense. I hope you can load the entire source code together in a single memory (for example, all files in the LLVM + clan are about 450 MB, so easily fit into memory on modern desktop / laptop May be). Edit: It should be noted that "interpreted languages", such as PHP, Javascript and Basic, generally do not require this requirement, but other compiled languages ​​usually do it - For example, Pascal has a special keyword forwarded to tell the compiler that this function is present, but I'm telling you what it contains later.

Both Pascal and C (and C ++ because it is based on C in this aspect) allow pointers to such structures that are not yet complete. Simply this simple "you do not have all the information yet" means that the compiler has to create type information, and then "go back and fix it" [clearly as required only] but this allows us to Returns:

  struct x {struct x * next; ...};   

or C ++:

  straight X {X * next; ...}; Editing 2: This blog by Jan Hikka, GCC developer, tells about some problems that "tackle all the code at once" Of course, most of us are Firefox and so on. There are not many types of shaped projects compiled, but large projects, when you have to deal with all the codes at once, problems arise with modern machines with "not enough memory available", if developers "Do not keep the compiler on the diet from time to time."  



Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -