I've few questions about C++ compilers
Are C++ compilers required to be one-pass compiler? Does the Standard talk about it anywhere?
In particular, is GCC one-pass compiler? If it is, then why does it generate the following error twice in this example (though the template argument is different in each error message)?
error: declaration of ‘adder<T> item’ shadows a parameter
error: declaration of ‘adder<char [21]> item’ shadows a parameter
A more general question
Useful links:
There are over 50 compilers for C like ICC by Intel to GNU GCC by GNU Project. The focus of having multiple compilers is to optimize the compiled C code for specific hardware and software environments.
G++ is the name of the compiler. (Note: G++ also compiles C++ code, but since C is directly compatible with C++, so we can use it.).
Clang is much faster and uses far less memory than GCC. Clang aims to provide extremely clear and concise diagnostics (error and warning messages), and includes support for expressive diagnostics. GCC's warnings are sometimes acceptable, but are often confusing and it does not support expressive diagnostics.
The standard sets no requirements what so ever with regards to how a compiler is implemented. But what do you mean by "one-pass"? Most compilers today do only read the input file once. They create an in memory representation (often in the form of some sort of parse tree), and may make multiple passes over that. And almost certainly make multiple passes over parts of it. The compiler must make a "pass" over the internal representation of a template each time it is instantiated, for example; there's no way of avoiding that. G++ also makes a "pass" over the template when it is defined, before any instantiation, and reports some errors then. (The standard committee expressedly designed templates to allow a maximum of error detection at the point of definition. This is the motivation behind the requirement for typename in certain places, for example.) Even without templates, a compiler will generally have to make two passes over a class definition if there are functions defined in it.
With regards to the more general question, again, I think you'd have to define exactly what you mean by "one-pass". I don't know of any compiler today which reads the source file several times, but almost all will visit some or all of the nodes in the parse tree more than once. Is this one-pass or multi-pass? The distinction was more significant in the past, when memory wasn't sufficient to maintain much of the source code in an internal representation. Languages like Pascal and, to a lesser degree C, were sometimes designed to be easy to implement with a single pass compiler, since a single pass compiler would be significantly faster. Today, this issue is largely irrelevant, and modern languages, including C++, tend to ignore it; where C++ seems to conform to the needs of a one-pass compiler, it's largely for reasons of C compatibility, and where C compatibility is not an issue (e.g. in a class definition), it often makes order of declaration irrelevant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With