Playing with the QML/JS plugin

Denis Steckelmacher steckdenis at yahoo.fr
Mon Apr 7 20:10:58 UTC 2014


Hi,

I've pushed an experimental branch of the QML/JS plugin in 
kde:scratch/dsteckelmacher/kdev-qmljs, branch "experiments". This branch 
contains one commit that is mostly the result of me playing with the 
plugin and the DUChain for a day.

My patch mostly moves code around (it removes expressionvisitor.cpp and 
merges its code into declarationbuilder.cpp, the commit message explains 
why) and adds some small type inference features. For instance, the 
plugin is now able to correctly guess the types of all these variables :

// Variables that were already guessed before my patch
var a = 5;	// int
var b = a;	// int
var f = function() { return "hello" };  // function string()
var c = f();	// string

// Variable not guessed correctly by ExpressionVisitor
var d = (a == b); // ExpressionVisitor::lastType guessed int

// Things that were not previously guessed
var e = (a > b);	// bool
var f = (aFunction() & 0x1)	// int
var g = (function() { return c; })(); // string

The whole testsuite of the plugin passes without failure, and I've added 
two tests (one for the variable "g" of my example, and one for "d" which 
was wrongly guessed).

I have pushed the code to allow more experimented KDevelop developers to 
see if I did not do utterly wrong things (for instance, I never know 
where I have to hold a DUChain read lock, therefore many of them may be 
missing). As the type inference system and the declaration builder are 
now intertwined, I also had to declare variables and QML fields in 
endVisit() instead of visit(), because the types get known only when 
endVisit() is called. I would appreciate any comment on what I've done :-) .

I end this email with two questions:

* Can I change the type of a declaration once it has been closed (for 
instance, by using getDeclaration(...)->setType(new_type))? This would 
allow me to guess that a in "var a; a = 5" is of type int, and it would 
be especially useful for setting the type of function parameters once 
they are used in a context were their type becomes evident.
* Is it possible for a declaration to have a source range outside the 
range of its parent context? It would be very useful to implement things 
like "var a = {}; a.b = 5;", with b being a field declared in the {} 
context created in the first statement.

Denis Steckelmacher


More information about the KDevelop-devel mailing list