[fpc-devel] Circular references and forward declarations

Florian Klaempfl florian at freepascal.org
Tue Jan 5 19:28:17 CET 2010

Jonas Maebe schrieb:
> On 05 Jan 2010, at 17:45, Juha Manninen wrote:
>> On tiistai, 5. tammikuuta 2010 18:14:53 Jonas Maebe wrote:
>>> The reason that they are conceptually not the same thing is that
>>> in Pascal two different units can both declare a different class
>>> with the same name (just like they can both have global variables
>>> and procedures/functions in the interface with the same name).
>>> You would at least have to do something like one of the following
>>>  a) use one global name space for all classes (i.e., forbid that
>>> two different units used in a program declare a class with the
>>> same name anywhere), or b) add some way to specify the unit name
>>> in which this external class is specified, or
>>> There might be other solutions (maybe some kind of class-specific
>>>  namespace support), but it would definitely require some more
>>> language features rather than merely accepting anonymous class
>>> definitions anywhere. Otherwise the type checking is going to run
>>> completely haywire.
>> Yes, I even suggested a syntax: TMyClass = class; defined in
>> "MyUnit.pas"
> You're right, sorry.
>> which would solve the namespace issue. It is the case b) you
>> listed. It could also be: MyUnit.TMyClass = class;
>> which is similar than syntax referencing global variables from
>> another unit.
> It would have at least two consequences (there are undoubtedly more
> that I'm not thinking of currently): a) all forward-declared types
> from the interface would have to be resolved after the uses-clause in
> the implementation has been parsed (because only at that point all
> classes would be known) b) this would cause the interface crc of
> those units to change (since the types change once they are
> resolved), which means that every unit containing such a construct
> (and every unit depending on them, even if they don't touch these
> types) would be recompiled at least once (some immediately in case of
> circular interface-implementation unit references, some only when you
> recompile the project)
> This wouldn't double the compilation time, but it would probably slow
> down things quite a bit. I'm also not sure whether the ppu
> storing/logic logic would still work if it could be called at a point
> where not all forward-declared types are resolved. It would probably
> require special code for that situation.

Defining forward types in a different unit breaks completly with the
spirit how object pascal is designed and I fear it will break much more
stuff in the compiler. Changing interface CRCs are probably only one of
the prominent problems. Resolving cyclic forward class references
between units will be probably a real nightmare.

More information about the fpc-devel mailing list