Review Request 117458: Support for function expressions
Denis Steckelmacher
steckdenis at yahoo.fr
Thu Apr 10 12:29:11 UTC 2014
-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
https://git.reviewboard.kde.org/r/117458/
-----------------------------------------------------------
(Updated April 10, 2014, 12:29 p.m.)
Review request for KDevelop.
Changes
-------
As suggested by Sven Brauch, contexts know their parent type and it is easy to find the context at a given position in the source. Anonymous functions can thus be found easily, without using bizarre names.
Repository: kdev-qmljs
Description
-------
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.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
Diff: https://git.reviewboard.kde.org/r/117458/diff/
Testing
-------
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.
Thanks,
Denis Steckelmacher
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kdevelop-devel/attachments/20140410/7c5f23f6/attachment-0001.html>
More information about the KDevelop-devel
mailing list