DUChain and anonymous declarations

Denis Steckelmacher steckdenis at yahoo.fr
Thu Apr 10 11:02:12 UTC 2014


Hi,

On 04/10/2014 12:28 PM, Sven Brauch wrote:
> First thing that comes to mind: What does ruby do? From what I understood
> Miquel solved the same problem, so it sounds like ruby has this already ;)
> Then, can you use the "store the declaration in the AST node temporarily"
> approach here maybe?

I have not yet found how Ruby handles that (if it does, because I've 
seen a comment saying that "a = -> { return 1 }" is unsupported).

> Inventing names for anonymous functions isn't that uncommon I guess, e.g. gcc
> does it too and you can see it in the compiler errors and backtraces. Also
> when you write f = function() { ... } the function isn't really anonymous any
> more ;)
> Can you maybe use something like the qualified-identifier of the left hand
> side expression (plus eventually a number if necessary) for the function name?
> Then I think it would even be okay (or desirable?) if it appears in the
> outline ...
> Can you just change the anonymous declaration's identifier as soon as it gets
> assigned to a variable?

Javascript likes corner cases, and the problem is that these corner 
cases are actually widely used:

function foo() {
     return function() { return 3; }
}

This function returns a new function, and the function returned really 
has no identifier (it doesn't get assigned to a variable). 
DeclarationBuilder still needs to build a FunctionType, and 
ExpressionVisitor still needs to see this type (to set the return type 
of foo).

There is also another potential use of anonymous declarations:

Item {
     id: foo
     property int bar: 3
}

Here, foo is an instance of an anonymous class inheriting Item and 
having a property named bar. The anonymous class needs to be identified 
in order to construct the type of foo.

> You can just create a declaration but not open it, I guess. But I'm not sure
> if that is a good idea.

The problem is that the declaration of anonymous functions must be 
"visible enough" for ExpressionVisitor to be able to find it. In fact, 
DeclarationBuilder does not need to declare anonymous functions, and my 
experimental branch does not do that. The only reason of why I need 
declarations is that ExpressionVisitor, given only a 
FunctionExpression*, must be able to find its type. As ExpressionVisitor 
is also used by the code completion model, I cannot store the type of 
anonymous functions outside the DUChain, the only thing (I think) that 
survives after DeclarationBuilder finishes its work.

Denis



More information about the KDevelop-devel mailing list