Commit 3286f485 authored by Christian Kamm's avatar Christian Kamm
Browse files

QmlJS: Remove Link::scopeChainAt, initialize scope chain in constructor.

Use ScopeBuilder.push(...) for the same functionality.

Reviewed-by: Erik Verbruggen
parent 91853c7c
......@@ -194,9 +194,6 @@ QList<DiagnosticMessage> Check::operator()()
bool Check::visit(UiProgram *)
{
// build the initial scope chain
_link.scopeChainAt(_doc);
return true;
}
......
......@@ -30,6 +30,7 @@
#include "qmljsinterpreter.h"
#include "qmljsevaluate.h"
#include "qmljslink.h"
#include "qmljsscopebuilder.h"
#include "parser/qmljsast_p.h"
#include <QtCore/QFile>
......@@ -1337,8 +1338,11 @@ void ScopeChain::update()
_all += globalScope;
foreach (QmlComponentChain *parent, qmlComponentScope.instantiatingComponents)
parent->add(&_all);
// the root scope in js files doesn't see instantiating components
if (jsScopes.count() != 1 || !qmlScopeObjects.isEmpty()) {
foreach (QmlComponentChain *parent, qmlComponentScope.instantiatingComponents)
parent->add(&_all);
}
if (qmlComponentScope.rootObject && ! qmlScopeObjects.contains(qmlComponentScope.rootObject))
_all += qmlComponentScope.rootObject;
......@@ -1373,7 +1377,7 @@ void Context::build(const QList<Node *> &astPath, QmlJS::Document::Ptr doc,
const QmlJS::Snapshot &snapshot, const QStringList &importPaths)
{
Link link(this, doc, snapshot, importPaths);
link.scopeChainAt(doc, astPath);
ScopeBuilder(doc, this).push(astPath);
}
Engine *Context::engine() const
......
......@@ -22,6 +22,7 @@ Link::Link(Context *context, const Document::Ptr &doc, const Snapshot &snapshot,
, _importPaths(importPaths)
{
linkImports();
initializeScopeChain();
}
Link::~Link()
......@@ -38,56 +39,48 @@ QList<DiagnosticMessage> Link::diagnosticMessages() const
return _diagnosticMessages;
}
void Link::scopeChainAt(Document::Ptr doc, const QList<Node *> &astPath)
void Link::initializeScopeChain()
{
ScopeChain &scopeChain = _context->scopeChain();
// ### TODO: This object ought to contain the global namespace additions by QML.
scopeChain.globalScope = engine()->globalObject();
if (! doc) {
if (! _doc) {
scopeChain.update();
return;
}
Bind *bind = doc->bind();
Bind *bind = _doc->bind();
QHash<Document *, ScopeChain::QmlComponentChain *> componentScopes;
if (doc->qmlProgram()) {
if (_doc->qmlProgram()) {
_context->setLookupMode(Context::QmlLookup);
scopeChain.qmlComponentScope.clear();
componentScopes.insert(doc.data(), &scopeChain.qmlComponentScope);
makeComponentChain(doc, &scopeChain.qmlComponentScope, &componentScopes);
componentScopes.insert(_doc.data(), &scopeChain.qmlComponentScope);
makeComponentChain(_doc, &scopeChain.qmlComponentScope, &componentScopes);
if (const ObjectValue *typeEnvironment = _context->typeEnvironment(doc.data()))
if (const ObjectValue *typeEnvironment = _context->typeEnvironment(_doc.data()))
scopeChain.qmlTypes = typeEnvironment;
} else {
// the global scope of a js file does not see the instantiating component
if (astPath.size() > 0) {
// add scope chains for all components that source this document
foreach (Document::Ptr otherDoc, _snapshot) {
if (otherDoc->bind()->includedScripts().contains(doc->fileName())) {
ScopeChain::QmlComponentChain *component = new ScopeChain::QmlComponentChain;
componentScopes.insert(otherDoc.data(), component);
scopeChain.qmlComponentScope.instantiatingComponents += component;
makeComponentChain(otherDoc, component, &componentScopes);
}
// add scope chains for all components that source this document
// ### TODO: This needs updates for the new way of importing scripts
foreach (Document::Ptr otherDoc, _snapshot) {
if (otherDoc->bind()->includedScripts().contains(_doc->fileName())) {
ScopeChain::QmlComponentChain *component = new ScopeChain::QmlComponentChain;
componentScopes.insert(otherDoc.data(), component);
scopeChain.qmlComponentScope.instantiatingComponents += component;
makeComponentChain(otherDoc, component, &componentScopes);
}
// ### TODO: Which type environment do scripts see?
}
// ### TODO: Which type environment do scripts see?
scopeChain.jsScopes += bind->rootObjectValue();
}
if (astPath.isEmpty()) {
scopeChain.update();
} else {
ScopeBuilder scopeBuilder(doc, _context);
foreach (Node *node, astPath)
scopeBuilder.push(node);
}
scopeChain.update();
}
void Link::makeComponentChain(
......
......@@ -24,9 +24,6 @@ public:
const QStringList &importPaths);
~Link();
// Get the scope chain for the currentObject inside doc.
void scopeChainAt(Document::Ptr doc, const QList<AST::Node *> &astPath = QList<AST::Node *>());
QList<DiagnosticMessage> diagnosticMessages() const;
private:
......@@ -42,6 +39,7 @@ private:
static AST::UiQualifiedId *qualifiedTypeNameId(AST::Node *node);
void linkImports();
void initializeScopeChain();
void populateImportedTypes(Interpreter::ObjectValue *typeEnv, Document::Ptr doc);
void importFile(Interpreter::ObjectValue *typeEnv, Document::Ptr doc,
......
......@@ -39,6 +39,12 @@ void ScopeBuilder::push(AST::Node *node)
_context->scopeChain().update();
}
void ScopeBuilder::push(const QList<AST::Node *> &nodes)
{
foreach (Node *node, nodes)
push(node);
}
void ScopeBuilder::pop()
{
Node *toRemove = _nodes.last();
......
......@@ -22,6 +22,7 @@ public:
~ScopeBuilder();
void push(AST::Node *node);
void push(const QList<AST::Node *> &nodes);
void pop();
private:
......
......@@ -192,8 +192,6 @@ public:
, m_link(m_context, doc, snapshot, importPaths)
, m_scopeBuilder(doc, m_context)
{
m_context->build(QList<Node *>(), doc, snapshot, importPaths);
m_link.scopeChainAt(doc);
}
~ReadingContext()
......
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