How to register uses for declarations which are outside of the current file?

David Nolden david.nolden.kdevelop at art-master.de
Sun Mar 13 16:16:27 UTC 2011


Each top-contexts assigns indices to each declaration that is used in
it, this is probably what confuses you.

To reference the actual used declaration persistently, the
"DeclarationId" class is used. Your problem is probably, that the
declaration you're using can not properly be accessed through
DeclarationId.

Take a look at DeclarationId::getDeclarations(topContext). Normally, a
top-context is given to this function, in which case the function will
only consider declarations that are contained by top-contexts which
are recursively imported into the given topContext. This is correct
for functions like C++, where everything has to be imported before it
can be used. It won't work properly for python though, where each
declaration can be imported individually.

Rather than hacking around the problem right in DeclarationId, there's
another, much better solution, that comes into my mind:

Whenever you see a "from BLA import bla", you should create an
AliasDeclaration with the name "bla", that points to the real
declaration. The real declaration is then referenced through an
IndexedDeclaration, and thus it will always work (see
AliasDeclaration::setAliasedDeclaration). The duchain will
transparently replace the AliasDeclaration with the pointed-to
declaration when you perform searches like findDeclaration.

Greetings, David




More information about the KDevelop-devel mailing list