contextbuilder.cpp 4.89 KB
Newer Older
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
1 2
/*************************************************************************************
 *  Copyright (C) 2012 by Aleix Pol <aleixpol@kde.org>                               *
3
 *  Copyright (C) 2012 by Milian Wolff <mail@milianw.de>                             *
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *                                                                                   *
 *  This program is free software; you can redistribute it and/or                    *
 *  modify it under the terms of the GNU General Public License                      *
 *  as published by the Free Software Foundation; either version 2                   *
 *  of the License, or (at your option) any later version.                           *
 *                                                                                   *
 *  This program is distributed in the hope that it will be useful,                  *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
 *  GNU General Public License for more details.                                     *
 *                                                                                   *
 *  You should have received a copy of the GNU General Public License                *
 *  along with this program; if not, write to the Free Software                      *
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
 *************************************************************************************/

#include "contextbuilder.h"
21
#include "expressionvisitor.h"
22 23
#include "parsesession.h"

24 25 26 27
using namespace KDevelop;

ContextBuilder::ContextBuilder()
: ContextBuilderBase()
28
, m_session(0)
29
, m_mapAst(false)
30
, m_editor(new Editor(&m_session))
31 32 33 34 35 36 37 38 39
{
}

RangeInRevision ContextBuilder::editorFindRange(QmlJS::AST::Node* fromNode, QmlJS::AST::Node* toNode)
{
    return m_session->editorFindRange(fromNode, toNode);
}

QualifiedIdentifier ContextBuilder::identifierForNode(QmlJS::AST::IdentifierPropertyName* node)
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
40
{
41
    return QualifiedIdentifier(node->id.toString());
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
42 43
}

44 45
AbstractType::Ptr ContextBuilder::findType(QmlJS::AST::Node* node)
{
46 47 48 49
    if (!node) {
        return AbstractType::Ptr(new IntegralType(IntegralType::TypeMixed));
    }

50
    ExpressionVisitor visitor(currentContext());
51 52 53 54 55

    // Build every needed declaration in node, and then try to guess its type
    node->accept(this);
    node->accept(&visitor);

56 57 58
    return visitor.lastType();
}

59
void ContextBuilder::setContextOnNode(QmlJS::AST::Node* node, DUContext* context)
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
60
{
61
    m_session->setContextOnNode(node, context);
62
}
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
63

64 65
DUContext* ContextBuilder::contextFromNode(QmlJS::AST::Node* node)
{
66
    return m_session->contextFromNode(node);
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
67 68 69 70
}

void ContextBuilder::startVisiting(QmlJS::AST::Node* node)
{
71 72
    QmlJS::AST::Node::accept(node, this);
}
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
73

74 75 76 77
TopDUContext* ContextBuilder::newTopContext(const RangeInRevision& range, ParsingEnvironmentFile* file)
{
    if (!file) {
        file = new ParsingEnvironmentFile(m_session->url());
Milian Wolff's avatar
Milian Wolff committed
78
        /// identify environment files from this language plugin
79 80 81
        file->setLanguage(m_session->languageString());
    }
    return ContextBuilderBase::newTopContext(range, file);
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
82
}
83 84 85 86 87

void ContextBuilder::setParseSession(ParseSession* session)
{
    m_session = session;
}
88

89 90 91 92 93
Editor* ContextBuilder::editor() const
{
    return m_editor.data();
}

94
bool ContextBuilder::visit(QmlJS::AST::FunctionDeclaration* node)
95 96 97 98 99 100 101 102 103 104
{
    return visitFunction(node);
}

bool ContextBuilder::visit(QmlJS::AST::FunctionExpression* node)
{
    return visitFunction(node);
}

bool ContextBuilder::visitFunction(QmlJS::AST::FunctionExpression* node)
105 106 107
{
    const QualifiedIdentifier functionName(node->name.toString());

108 109
    const RangeInRevision pRange(m_session->locationToRange(node->lparenToken).end,
                                 m_session->locationToRange(node->rparenToken).start);
110
    DUContext* parameters = openContext(node, pRange, DUContext::Function, functionName);
111
    QmlJS::AST::Node::accept(node->formals, this);
112 113
    closeContext();

114 115
    const RangeInRevision bRange(m_session->locationToRange(node->lbraceToken).end,
                                 m_session->locationToRange(node->rbraceToken).start);
116
    DUContext* body = openContext(node, bRange, DUContext::Other, functionName);
117 118 119 120
    if (compilingContexts()) {
        DUChainWriteLocker lock;
        body->addImportedParentContext(parameters);
    }
121
    QmlJS::AST::Node::accept(node->body, this);
122 123
    closeContext();

124 125
    // return false, we visited the children manually
    return false;
126
}
127

128
bool ContextBuilder::visit(QmlJS::AST::UiObjectInitializer* node)
129
{
130 131 132 133 134 135 136 137 138 139
    const RangeInRevision range(m_session->locationToRange(node->lbraceToken).end,
                                m_session->locationToRange(node->rbraceToken).start);
    openContext(node, range, DUContext::Class);

    return true;
}

void ContextBuilder::endVisit(QmlJS::AST::UiObjectInitializer* /*node*/)
{
    closeContext();
140
}