Review Request 117458: Support for function expressions

Denis Steckelmacher steckdenis at
Fri Apr 11 14:38:24 UTC 2014

This is an automatically generated e-mail. To reply, visit:

(Updated April 11, 2014, 2:38 p.m.)

Review request for KDevelop.


Cleanup how integer and float values are recognized

Repository: kdev-qmljs


This patch adds support for function expressions to the QML/JS plugin. This support required the following changes:

* ExpressionVisitor stops visiting when it encounters a type (no need to traverse the AST down to its bottom if the top-most expression is enough to determine a type). This gives it a O(1) time complexity.
* When DeclarationBuilder wants to know the type of an expression, it first visits this expression (to build any needed declaration), then only starts an ExpressionVisitor. This way, ExpressionVisitor knows all the declarations that may occur in the expression and is able to return its complete type. Moreover, if DeclarationBuilder has called getType (and has visited an expression), then will not enter the node it is visiting (as it already did so). This way, DeclarationBuilder keeps an O(n) time complexity.
* These two changes are illustrated by the support of "var f = function() { return true; }" statements.
* This patch also adds support for binary operators ("a == b" is always a boolean, "a << 2" is always an int, etc). This fixes a bug where the type of "(a == 5)" was wrongly guessed as "int"

The support for function expressions is split between DeclarationBuilder and ExpressionVisitor. DeclarationBuilder, when it encounters a function expression, creates a declaration for it, giving it a name built from the hexadecimal representation of its FunctionExpression* node pointer. When DeclarationBuilder sees a return statement, it sets the type of the enclosing function (whether it is a function declaration or a function expression has no importance here). When ExpressionVisitor is used to find the type of an expression, and this expression is a function expression, then ExpressionVisitor will build the hexadecimal name of the function and use getDeclaration to find its type.

Diffs (updated)

  duchain/CMakeLists.txt 9fab69c 
  duchain/contextbuilder.h d53b2fe 
  duchain/contextbuilder.cpp d38da2d 
  duchain/declarationbuilder.h 001c3b2 
  duchain/declarationbuilder.cpp 98da341 
  duchain/expressionvisitor.h b4f0851 
  duchain/expressionvisitor.cpp 766435d 
  duchain/helper.h 23a6d0e 
  duchain/helper.cpp 3ffdd56 
  duchain/parsesession.h 0eb2762 
  duchain/parsesession.cpp dbc4d90 
  duchain/tests/testdeclarations.cpp 668efaa 
  tests/files/helloworld.js ff40f1c 



All the unit tests of the QML/JS plugin pass with this patch. Three tests have been added:

* The test for type inference is not expected to fail anymore, as the plugin is able to infer simple types
* New test for "(a == b)" expressions
* New test for "function() {}" expressions

Manual testing shows that syntax highlighting is correct, and also the "outline" view. Building the anonymous declarations by passing them an empty range (RangeInRevision()) removes the anonymous function from the outline view and disables any highlighting related to this declaration in the source view. The function can still have a name which can be freely chosen by the plugin.


Denis Steckelmacher

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the KDevelop-devel mailing list