Question about injectContext

Niko Sams niko.sams at gmail.com
Wed Mar 23 02:53:08 UTC 2011


On Wed, Mar 23, 2011 at 05:18, David Nolden
<david.nolden.kdevelop at art-master.de> wrote:
> 2011/3/22 Sven Brauch <svenbrauch at googlemail.com>:
>> I'd like to create a member variable declaration for a class if an
>> attribute is written at any instance of that class, for example (A is
>> a class): "instance = A(); instance->x = 3" -- then I'd like to create
>> a class member declaration for x, if none exists yet.
>> Isn't this the use case injectContext is meant for? I would like to
>
> No, at least in C++, injectContext is mainly just used for to
> temporarily change the visibility to have the same visibility as from
> within the given context (at least from what I remember).
>
> The deletion of the declarations most probably happens because the
> declaration cannot be matched properly during updating. Whenever the
> context is re-parsed, the declaration-builder notices that the
> declaration was not matched, and deletes it again. This happens
> because you don't create the declaration while the context is _really_
> built. The issue regarding the navigation-widget happens because the
> duchain does the sane assumption that each declaration happens within
> the range of the enclosing context.
>
> I have already thought about these issues for languages like python,
> and I think the solution would be:
> 1) Add a "isAutoDeclaration" 1-bit bool flag to Declaration, which
> says whether the declaration was created "automatically" through a use
> of it, and always declare this for python variables.
> 2) In the context-builder while closing a context: Instead of deleting
> all non-encountered declarations, leave alive non-encountered
> declarations with the "isAutoDeclaration" flag _only if_ it has uses.
> 3) When the declaration is used:
> 4.1) If the declaration does not exist yet, create it the way you're
> doing now, but with an empty range that is contained by the correct
> class-context, and with the isAutoDeclaration set.
> 4.2) Create a use of the declaration, with the correct range. This
> will make the navigation-widget work.
>
> I think this would be the most clean way to do it. Maybe the php
> developers also have something to add, as they might be dealing with
> similar issues.
yes, more or less the exact same thing would be needed for php. Davids solution
sounds sane.
Please try to implement this as generic as possible so it can be used
in php too.

And the same is needed for function declarations too, imho even more
importantly.
(in php there is a magic __call method that gets called for every
method call that has no
"real" method defined)

Niko




More information about the KDevelop-devel mailing list