Commit 25e04d8e authored by Christian Kamm's avatar Christian Kamm
Browse files

Make QmlJS::Link private. Use Context::build to set up a context.

parent 14f483c6
......@@ -29,6 +29,7 @@
#include "qmljsinterpreter.h"
#include "qmljscheck.h"
#include "qmljslink.h"
#include "parser/qmljsast_p.h"
#include <QtCore/QMetaObject>
#include <QtCore/QMetaProperty>
......@@ -688,6 +689,12 @@ Context::~Context()
{
}
void Context::build(AST::Node *node, Document::Ptr doc, const Snapshot &snapshot)
{
Link link(this, doc, snapshot);
link.scopeChainAt(doc, node);
}
Engine *Context::engine() const
{
return _engine;
......
......@@ -30,6 +30,7 @@
#ifndef QMLJS_INTERPRETER_H
#define QMLJS_INTERPRETER_H
#include <qmljs/qmljsdocument.h>
#include <qmljs/qmljs_global.h>
#include <qmljs/qmljsmetatypesystem.h>
#include <qmljs/parser/qmljsastfwd_p.h>
......@@ -224,6 +225,8 @@ public:
Context(Engine *engine);
~Context();
void build(AST::Node *node, Document::Ptr doc, const Snapshot &snapshot);
Engine *engine() const;
ScopeChain scopeChain() const;
......
......@@ -12,12 +12,11 @@ using namespace QmlJS;
using namespace QmlJS::Interpreter;
using namespace QmlJS::AST;
Link::Link(Document::Ptr currentDoc, const Snapshot &snapshot, Interpreter::Engine *interp)
Link::Link(Context *context, Document::Ptr currentDoc, const Snapshot &snapshot)
: _snapshot(snapshot)
, _context(interp)
, _context(context)
{
_docs = reachableDocuments(currentDoc, snapshot);
linkImports();
}
......@@ -25,25 +24,20 @@ Link::~Link()
{
}
Context *Link::context()
{
return &_context;
}
Interpreter::Engine *Link::engine()
{
return _context.engine();
return _context->engine();
}
void Link::scopeChainAt(Document::Ptr doc, Node *currentObject)
{
_context.pushScope(engine()->globalObject());
_context->pushScope(engine()->globalObject());
if (! doc)
return;
if (doc->qmlProgram() != 0)
_context.setLookupMode(Context::QmlLookup);
_context->setLookupMode(Context::QmlLookup);
Bind *bind = doc->bind();
......@@ -52,7 +46,7 @@ void Link::scopeChainAt(Document::Ptr doc, Node *currentObject)
// ### FIXME: May want to link to instantiating components from here.
if (bind->_rootObjectValue)
_context.pushScope(bind->_rootObjectValue);
_context->pushScope(bind->_rootObjectValue);
ObjectValue *scopeObject = 0;
if (UiObjectDefinition *definition = cast<UiObjectDefinition *>(currentObject))
......@@ -61,7 +55,7 @@ void Link::scopeChainAt(Document::Ptr doc, Node *currentObject)
scopeObject = bind->_qmlObjects.value(binding);
if (scopeObject && scopeObject != bind->_rootObjectValue)
_context.pushScope(scopeObject);
_context->pushScope(scopeObject);
const QStringList &includedScripts = bind->includedScripts();
for (int index = includedScripts.size() - 1; index != -1; --index) {
......@@ -69,19 +63,19 @@ void Link::scopeChainAt(Document::Ptr doc, Node *currentObject)
if (Document::Ptr scriptDoc = _snapshot.document(scriptFile)) {
if (scriptDoc->jsProgram()) {
_context.pushScope(scriptDoc->bind()->_rootObjectValue);
_context->pushScope(scriptDoc->bind()->_rootObjectValue);
}
}
}
if (bind->_functionEnvironment)
_context.pushScope(bind->_functionEnvironment);
_context->pushScope(bind->_functionEnvironment);
if (bind->_idEnvironment)
_context.pushScope(bind->_idEnvironment);
_context->pushScope(bind->_idEnvironment);
if (const ObjectValue *typeEnvironment = _context.typeEnvironment(doc.data()))
_context.pushScope(typeEnvironment);
if (const ObjectValue *typeEnvironment = _context->typeEnvironment(doc.data()))
_context->pushScope(typeEnvironment);
}
void Link::linkImports()
......@@ -92,7 +86,7 @@ void Link::linkImports()
// Populate the _typeEnvironment with imports.
populateImportedTypes(typeEnv, doc);
_context.setTypeEnvironment(doc.data(), typeEnv);
_context->setTypeEnvironment(doc.data(), typeEnv);
}
}
......
......@@ -2,34 +2,32 @@
#define QMLJSLINK_H
#include <qmljs/qmljsdocument.h>
#include <qmljs/qmljsbind.h>
#include <qmljs/qmljsinterpreter.h>
#include <qmljs/parser/qmljsastfwd_p.h>
#include <qmljs/parser/qmljsengine_p.h>
#include <QtCore/QList>
#include <QtCore/QHash>
namespace QmlJS {
class NameId;
/*
Temporarily links a set of bound documents together to allow resolving cross-document
dependencies. The link is undone when this object is destoyed.
Helper for building a context.
*/
class QMLJS_EXPORT Link
class Link
{
public:
// Link all documents in snapshot reachable from doc.
Link(Document::Ptr doc, const Snapshot &snapshot, Interpreter::Engine *interp);
Link(Interpreter::Context *context, Document::Ptr doc, const Snapshot &snapshot);
~Link();
Interpreter::Context *context();
Interpreter::Engine *engine();
// Get the scope chain for the currentObject inside doc.
void scopeChainAt(Document::Ptr doc, AST::Node *currentObject);
private:
Interpreter::Engine *engine();
static QList<Document::Ptr> reachableDocuments(Document::Ptr startDoc, const Snapshot &snapshot);
static AST::UiQualifiedId *qualifiedTypeNameId(AST::Node *node);
......@@ -44,7 +42,7 @@ private:
private:
Snapshot _snapshot;
Interpreter::Context _context;
Interpreter::Context *_context;
QList<Document::Ptr> _docs;
};
......
......@@ -653,12 +653,11 @@ int QmlCodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
const QIcon symbolIcon = iconForColor(Qt::darkCyan);
Interpreter::Engine interp;
Interpreter::Context context(&interp);
// Set up the current scope chain.
Link link(document, snapshot, &interp);
AST::Node *declaringMember = semanticInfo.declaringMember(editor->position());
link.scopeChainAt(document, declaringMember);
context.build(declaringMember, document, snapshot);
// Search for the operator that triggered the completion.
QChar completionOperator;
......@@ -668,7 +667,7 @@ int QmlCodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
if (completionOperator.isSpace() || completionOperator.isNull() || isDelimiter(completionOperator) ||
(completionOperator == QLatin1Char('(') && m_startPosition != editor->position())) {
// It's a global completion.
EnumerateProperties enumerateProperties(link.context());
EnumerateProperties enumerateProperties(&context);
enumerateProperties.setGlobalCompletion(true);
QHashIterator<QString, const Interpreter::Value *> it(enumerateProperties());
while (it.hasNext()) {
......@@ -691,14 +690,14 @@ int QmlCodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
//qDebug() << "expression:" << expression;
if (expression != 0) {
Check evaluate(link.context());
Check evaluate(&context);
// Evaluate the expression under cursor.
const Interpreter::Value *value = interp.convertToObject(evaluate(expression));
//qDebug() << "type:" << interp.typeId(value);
if (value && completionOperator == QLatin1Char('.')) { // member completion
EnumerateProperties enumerateProperties(link.context());
EnumerateProperties enumerateProperties(&context);
QHashIterator<QString, const Interpreter::Value *> it(enumerateProperties(value));
while (it.hasNext()) {
it.next();
......
......@@ -172,14 +172,14 @@ void QmlHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in
AST::Node *declaringMember = semanticInfo.declaringMember(pos);
Interpreter::Engine interp;
Link link(qmlDocument, snapshot, &interp);
link.scopeChainAt(qmlDocument, declaringMember);
Interpreter::Context context(&interp);
context.build(declaringMember, qmlDocument, snapshot);
Check check(link.context());
Check check(&context);
const Interpreter::Value *value = check(node);
QStringList baseClasses;
m_toolTip = prettyPrint(value, link.context(), &baseClasses);
m_toolTip = prettyPrint(value, &context, &baseClasses);
foreach (const QString &baseClass, baseClasses) {
QString helpId = QLatin1String("QML.");
......
Supports Markdown
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