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

Sven Brauch svenbrauch at googlemail.com
Sun Mar 13 16:41:15 UTC 2011


Hey,

thank you very much for your reply. That AliasDeclaration thing sounds
like a very good solution for "from foo import bar".
There's another case, however; for "import foo", you must actually
create a new... container object or whatever which is called foo and
contains all declarations from foo.py. Currently I'm doing this by
creating a declaration called foo and assigning it the topContext of
the file I want to import with setInternalContext(). How would you
reference a declaration in that internal context?

Thanks,
Sven

2011/3/13 David Nolden <david.nolden.kdevelop at art-master.de>:
> 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