[Lazarus] Parser

Hans-Peter Diettrich DrDiettrich1 at aol.com
Thu Jul 1 17:31:58 CEST 2010


Florian Klaempfl schrieb:

>> In fact I already missed sadly a fully working preprocessor (to be
>> activated optionally). 
> 
> Even if FPC supported such a mess, this won't solve the fundamental
> problem with C headers: they work only properly if every used header is
> compiled for each source file again so the whole unit concept of object
> pascal has to be thrown away.

This is only partly true. gcc already supports (non-standard) 
extensions, like #include_next and #include_once - don't beat me for the 
exact wording ;-)

Other compilers use other mechanisms (heuristics, pragmas...), in order 
to reduce the number of passes over the same header files. Only the 
standard-conforming guards require to scan and preprocess all #included 
files on every occurence, because the terminating #endif could reside 
anywhere in the file, not only at its end.

And finally, a Pascal compiler works different from a C compiler, with 
regards to the interfaces of other source modules (see below)

> So using a fully automated always working
> approach you've go back to {$I ...}-style imports. Even ignoring the
> fact that the real value of a half automated translated header is that
> someone spent some brain into the translation and how it can be done in
> a pascal styled way.

The Pascal style suffers from essentially the same problems, when {$I 
...} is found in the source code - I just came across the {$I 
fpcdefs.inc} in the FPC compiler. The major advantage of the OPL unit 
and project concept (or .NET assemblies...) is the combination of 
interface and implementation in an single file, that must be parsed only 
once in order to extract its exports.

This means that #/$include in a Pascal source file has a different 
meaning to the compilation, because that file *must* become part of the 
source. If C/C++ had an equivalent to the OPL "uses", the *compiler* 
could determine whether those files have to be included literally, or 
whether they could be parsed once for exported symbols and macro 
definitions (precompiled headers...).

Also a Pascal compiler can compile multiple units in one go, whereas a 
C/C++ compiler has no idea of related files, and must compile every 
single source file independently, #including all the used header files 
literally.

In so far $I and #include are fully equivalent, it's only the *usage* of 
$I, that is not required for used units. The (currently) only difference 
between C and FPC preprocessors is the macro definition and expansion, 
that allows for much trickier constructs in an C preprocessor (macro 
arguments, recursive expansion, # and ## substitution...).

DoDi





More information about the Lazarus mailing list