DUChain and anonymous declarations

Sven Brauch svenbrauch at googlemail.com
Thu Apr 10 10:28:55 UTC 2014


Hi!

On Thursday 10 April 2014 11:11:29 Denis Steckelmacher wrote:
> 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.
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?

> 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.
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?

> 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).
You can use a zero length range if necessary.

> Is
> it possible to hide a declaration from the rest of KDevelop, while
> keeping it available to ExpressionVisitor?
You can just create a declaration but not open it, I guess. But I'm not sure 
if that is a good idea.

Another approach would be to give the language plugin more control over what 
is considered a function declaration. That is the direction I think we need to 
move the duchain into long-term anyways: give the language plugins control 
over what data structures are used to represent certain elements in the code, 
instead of sticking the data structure (KDevelop::FunctionDeclaration) to what 
it means ("this is a function declaration in my language").

Greetings!


More information about the KDevelop-devel mailing list