DUChain and anonymous declarations

Denis Steckelmacher steckdenis at yahoo.fr
Thu Apr 10 09:11:29 UTC 2014


I'm currently trying to build a DUChain for Javascript code like this:

var f = function() { return 3; }

This snippet declares a variable whose type is a function. This variable 
can now be called using "f()".

The variable assignment needs to know the type of the function 
expression. In order to infer this type, I run DeclarationBuilder on the 
function expression, then an ExpressionVisitor that will return the type 
of the expression. The problem is that ExpressionVisitor can only find 
the type of literals and identifiers. As the anonymous function has no 
name, ExpressionVisitor cannot use DUContext::getDeclarations to get 
back the FunctionType build by DeclarationBuilder.

A dirty solution for this problem, that I currently use and that gives 
good results, is to "invent" a name for anonymous functions. I do that 
by taking the address of the QmlJS::AST::Node representing the 
FunctionExpression, and by converting it into hexadecimal. This way, 
DeclarationBuilder can build a declaration for the function, set its 
return type when it encounters a return statement, and ExpressionVisitor 
can use this name to get back the type of the function expression.

I played with Declaration::setInSymbolTable, but I was unable to remove 
the hexadecimal name of the anonymous function from the Outline view. 
Moreover, the "function" keyword in now highlighted in purple (I had to 
choose a range for the declaration and I took the function keyword). Is 
it possible to hide a declaration from the rest of KDevelop, while 
keeping it available to ExpressionVisitor? Is there another mean for 
DeclarationBuilder to build types and to store them somewhere where 
ExpressionVisitor can find them?

Denis Steckelmacher

More information about the KDevelop-devel mailing list