Commit a048f702 authored by Andrea Scarpino's avatar Andrea Scarpino

Implement identifiers types, functions types, functions arguments types

parent 44481027
......@@ -18,7 +18,7 @@
*************************************************************************************/
#include "contextbuilder.h"
#include "expressionvisitor.h"
#include "parsesession.h"
using namespace KDevelop;
......@@ -41,6 +41,13 @@ QualifiedIdentifier ContextBuilder::identifierForNode(QmlJS::AST::IdentifierProp
return QualifiedIdentifier(node->id.toString());
}
AbstractType::Ptr ContextBuilder::findType(QmlJS::AST::Node* node)
{
ExpressionVisitor visitor(currentContext());
QmlJS::AST::Node::accept(node, &visitor);
return visitor.lastType();
}
void ContextBuilder::setContextOnNode(QmlJS::AST::Node* node, DUContext* context)
{
m_session->setContextOnNode(node, context);
......
......@@ -21,6 +21,7 @@
#define CONTEXTBUILDER_H
#include <language/duchain/builders/abstractcontextbuilder.h>
#include <language/duchain/types/abstracttype.h>
#include <qmljs/parser/qmljsast_p.h>
#include <qmljs/qmljsdocument.h>
......@@ -62,6 +63,7 @@ public:
virtual KDevelop::TopDUContext* newTopContext(const KDevelop::RangeInRevision& range,
KDevelop::ParsingEnvironmentFile* file = 0);
KDevelop::AbstractType::Ptr findType(QmlJS::AST::Node* node);
void setParseSession(ParseSession* session);
using Visitor::visit;
......
......@@ -116,11 +116,21 @@ bool DeclarationBuilder::visit(QmlJS::AST::FormalParameterList* node)
IntegralType* type = new IntegralType(IntegralType::TypeMixed);
dec->setType(IntegralType::Ptr(type));
closeDeclaration();
FunctionType::Ptr funType = currentType<FunctionType>();
funType->addArgument(findType(plist));
}
return DeclarationBuilderBase::visit(node);
}
void DeclarationBuilder::endVisit(QmlJS::AST::ReturnStatement* node)
{
DeclarationBuilderBase::endVisit(node);
FunctionType::Ptr type = currentType<FunctionType>();
type->setReturnType(findType(node->expression));
}
bool DeclarationBuilder::visit(QmlJS::AST::VariableDeclaration* node)
{
setComment(m_session->commentForLocation(node->firstSourceLocation()).toUtf8());
......
......@@ -47,8 +47,11 @@ protected:
virtual bool visit(QmlJS::AST::FormalParameterList* node);
virtual bool visit(QmlJS::AST::VariableDeclaration* node);
virtual void endVisit(QmlJS::AST::ReturnStatement* node);
virtual bool visit(QmlJS::AST::UiObjectDefinition* node);
virtual void endVisit(QmlJS::AST::UiObjectDefinition* node);
virtual bool visit(QmlJS::AST::UiObjectInitializer* node);
virtual bool visit(QmlJS::AST::UiScriptBinding* node);
......
......@@ -18,13 +18,15 @@
#include "expressionvisitor.h"
#include <language/duchain/declaration.h>
#include "helper.h"
using namespace KDevelop;
AbstractType::Ptr findType(QmlJS::AST::Node* node)
ExpressionVisitor::ExpressionVisitor(DUContext* context) :
m_context(context)
{
ExpressionVisitor visitor;
QmlJS::AST::Node::accept(node, &visitor);
return visitor.lastType();
}
void ExpressionVisitor::endVisit(QmlJS::AST::ArrayLiteral* node)
......@@ -39,6 +41,12 @@ void ExpressionVisitor::endVisit(QmlJS::AST::FalseLiteral* node)
m_lastType.push(AbstractType::Ptr(new IntegralType(IntegralType::TypeBoolean)));
}
void ExpressionVisitor::endVisit(QmlJS::AST::IdentifierExpression* node)
{
const QualifiedIdentifier name(node->name.toString());
m_lastType.push(QmlJS::getDeclaration(name, DUContextPointer(m_context))->abstractType());
}
void ExpressionVisitor::endVisit(QmlJS::AST::NumericLiteral* node)
{
if (QString::number(node->value).contains('.')) {
......
......@@ -22,30 +22,33 @@
#include <QStack>
#include <language/duchain/types/integraltype.h>
#include <language/duchain/ducontext.h>
#include <qmljs/parser/qmljsast_p.h>
#include "duchainexport.h"
KDevelop::AbstractType::Ptr findType(QmlJS::AST::Node* node);
class KDEVQMLJSDUCHAIN_EXPORT ExpressionVisitor : public QmlJS::AST::Visitor
{
public:
explicit ExpressionVisitor(KDevelop::DUContext* context);
using Visitor::visit;
using Visitor::endVisit;
KDevelop::AbstractType::Ptr lastType();
protected:
virtual void endVisit(QmlJS::AST::ArrayLiteral*);
virtual void endVisit(QmlJS::AST::FalseLiteral*);
virtual void endVisit(QmlJS::AST::NumericLiteral*);
virtual void endVisit(QmlJS::AST::StringLiteral*);
virtual void endVisit(QmlJS::AST::TrueLiteral*);
virtual void endVisit(QmlJS::AST::ArrayLiteral* node);
virtual void endVisit(QmlJS::AST::FalseLiteral* node);
virtual void endVisit(QmlJS::AST::IdentifierExpression* node);
virtual void endVisit(QmlJS::AST::NumericLiteral* node);
virtual void endVisit(QmlJS::AST::StringLiteral* node);
virtual void endVisit(QmlJS::AST::TrueLiteral* node);
private:
QStack<KDevelop::AbstractType::Ptr> m_lastType;
KDevelop::DUContext* m_context;
};
......
......@@ -12,6 +12,11 @@ function helloWorld()
*/
var a = 5;
/**
* "type" : { "toString" : "int" }
*/
var b = a;
/**
* "type" : { "toString" : "double" }
*/
......@@ -23,7 +28,7 @@ var d = 1.2;
var array = [1, 2, 3];
/**
* "EXPECT_FAIL" : { "returnType" : "return type is not properly deduced" },
* "type" : { "toString" : "function string ()" },
* "returnType" : { "toString" : "string" }
*/
function testVariables()
......@@ -36,8 +41,8 @@ function testVariables()
}
/**
* "EXPECT_FAIL" : { "type" : "neither arg type nor ret type is properly deduced" },
* "type": { "toString" : "function mixed (mixed)" }
* "type": { "toString" : "function mixed (mixed)" },
* "returnType" : { "toString" : "mixed" }
*/
function testReturnMixedArg(arg)
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment