There is no need for the second phase. You can record the implicit declaration when you encounter it, and if there is a subsequent declaration, you can check that it's consistent or error out immediately.
This is what C compilers already do, in fact, to produce warnings when an implicit declaration doesn't match a later explicit declaration. But this is a best-effort warning only; it doesn't work if there is no declaration because the function is defined in a different translation unit, as I pointed out above.
Re:Porting to GCC 14: C language issues
by mfichman on Monday February 19, 2024 @05:14PM
(#39435475)
Usually, the second pass in a compiler does not re-parse source files. Rather, it operates on another data structure, like an AST, intermediate representation, or the list mentioned in the original comment. At least, that’s my understanding of multi-pass compilation.
Re:Porting to GCC 14: C language issues
by Joker_vD on Tuesday February 20, 2024 @06:34AM
(#39440090)
Well, this "list mentioned in the original comment" is not an AST or an immediate representation of the program in any reasonable sense just as a symbol table is not. Otherwise, setting the exact values of the text/data/bss size fields in the executable file header's at the end of the compilation would count as the second pass as well which IMO it should not.
Re:Porting to GCC 14: C language issues
by mfichman on Tuesday February 20, 2024 @07:00AM
(#39440244)
The difference is that you need to make a complete second iteration (or pass) over the entire list to correctly check all of the callsites after all function type information is collected. The same is not true for symbol table usage in a single-pass C compiler.
Granted, you can call that a second pass although that's not that different from emitting a function's epilogue IMHO.