QmlJS: Move Context to its own file.

Change-Id: I91331b7039e27e2f57cc19b985c8ed86c955ec9e
Reviewed-on: http://codereview.qt.nokia.com/1042
Reviewed-by: Fawzi Mohamed <fawzi.mohamed@nokia.com>
This commit is contained in:
Christian Kamm
2011-07-01 13:51:53 +02:00
parent 9ea0380a4d
commit 36ed6bce1d
23 changed files with 292 additions and 198 deletions

View File

@@ -29,7 +29,8 @@ HEADERS += \
$$PWD/qmljsdelta.h \
$$PWD/qmljstypedescriptionreader.h \
$$PWD/qmljsscopeastpath.h \
$$PWD/qmljsvalueowner.h
$$PWD/qmljsvalueowner.h \
$$PWD/qmljscontext.h
SOURCES += \
$$PWD/qmljsbind.cpp \
@@ -50,7 +51,8 @@ SOURCES += \
$$PWD/qmljsdelta.cpp \
$$PWD/qmljstypedescriptionreader.cpp \
$$PWD/qmljsscopeastpath.cpp \
$$PWD/qmljsvalueowner.cpp
$$PWD/qmljsvalueowner.cpp \
$$PWD/qmljscontext.cpp
RESOURCES += \
$$PWD/qmljs.qrc

View File

@@ -32,7 +32,7 @@
#include "qmljscheck.h"
#include "qmljsbind.h"
#include "qmljsinterpreter.h"
#include "qmljscontext.h"
#include "qmljsevaluate.h"
#include "parser/qmljsast_p.h"

View File

@@ -34,7 +34,7 @@
#define QMLJSCHECK_H
#include <qmljs/qmljsdocument.h>
#include <qmljs/qmljsinterpreter.h>
#include <qmljs/qmljscontext.h>
#include <qmljs/qmljsscopebuilder.h>
#include <qmljs/parser/qmljsastvisitor_p.h>

View File

@@ -0,0 +1,178 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
**
**************************************************************************/
#include "qmljscontext.h"
#include "parser/qmljsast_p.h"
using namespace QmlJS;
using namespace QmlJS::AST;
using namespace QmlJS::Interpreter;
Context::Context(const QmlJS::Snapshot &snapshot, ValueOwner *valueOwner, const ImportsPerDocument &imports)
: _snapshot(snapshot),
_valueOwner(valueOwner),
_imports(imports),
_qmlScopeObjectIndex(-1),
_qmlScopeObjectSet(false)
{
}
Context::~Context()
{
}
// the values is only guaranteed to live as long as the context
ValueOwner *Context::valueOwner() const
{
return _valueOwner.data();
}
QmlJS::Snapshot Context::snapshot() const
{
return _snapshot;
}
const ScopeChain &Context::scopeChain() const
{
return _scopeChain;
}
ScopeChain &Context::scopeChain()
{
return _scopeChain;
}
const Imports *Context::imports(const QmlJS::Document *doc) const
{
if (!doc)
return 0;
return _imports.value(doc).data();
}
const Value *Context::lookup(const QString &name, const ObjectValue **foundInScope) const
{
QList<const ObjectValue *> scopes = _scopeChain.all();
for (int index = scopes.size() - 1; index != -1; --index) {
const ObjectValue *scope = scopes.at(index);
if (const Value *member = scope->lookupMember(name, this)) {
if (foundInScope)
*foundInScope = scope;
return member;
}
}
if (foundInScope)
*foundInScope = 0;
return _valueOwner->undefinedValue();
}
const ObjectValue *Context::lookupType(const QmlJS::Document *doc, UiQualifiedId *qmlTypeName,
UiQualifiedId *qmlTypeNameEnd) const
{
const Imports *importsObj = imports(doc);
if (!importsObj)
return 0;
const ObjectValue *objectValue = importsObj->typeScope();
if (!objectValue)
return 0;
for (UiQualifiedId *iter = qmlTypeName; objectValue && iter && iter != qmlTypeNameEnd;
iter = iter->next) {
if (! iter->name)
return 0;
const Value *value = objectValue->lookupMember(iter->name->asString(), this);
if (!value)
return 0;
objectValue = value->asObjectValue();
}
return objectValue;
}
const ObjectValue *Context::lookupType(const QmlJS::Document *doc, const QStringList &qmlTypeName) const
{
const Imports *importsObj = imports(doc);
if (!importsObj)
return 0;
const ObjectValue *objectValue = importsObj->typeScope();
if (!objectValue)
return 0;
foreach (const QString &name, qmlTypeName) {
if (!objectValue)
return 0;
const Value *value = objectValue->lookupMember(name, this);
if (!value)
return 0;
objectValue = value->asObjectValue();
}
return objectValue;
}
const Value *Context::lookupReference(const Value *value) const
{
const Reference *reference = value_cast<const Reference *>(value);
if (!reference)
return value;
if (_referenceStack.contains(reference))
return 0;
_referenceStack.append(reference);
const Value *v = reference->value(this);
_referenceStack.removeLast();
return v;
}
QString Context::defaultPropertyName(const ObjectValue *object) const
{
PrototypeIterator iter(object, this);
while (iter.hasNext()) {
const ObjectValue *o = iter.next();
if (const ASTObjectValue *astObjValue = dynamic_cast<const ASTObjectValue *>(o)) {
QString defaultProperty = astObjValue->defaultPropertyName();
if (!defaultProperty.isEmpty())
return defaultProperty;
} else if (const QmlObjectValue *qmlValue = dynamic_cast<const QmlObjectValue *>(o)) {
return qmlValue->defaultPropertyName();
}
}
return QString();
}

View File

@@ -0,0 +1,90 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
**
**************************************************************************/
#ifndef QMLJS_CONTEXT_H
#define QMLJS_CONTEXT_H
#include "qmljs_global.h"
#include "qmljsvalueowner.h"
#include <QtCore/QSharedPointer>
namespace QmlJS {
class Document;
class Snapshot;
namespace Interpreter {
class QMLJS_EXPORT Context
{
public:
typedef QHash<const Document *, QSharedPointer<const Imports> > ImportsPerDocument;
// Context takes ownership of valueOwner
Context(const Snapshot &snapshot, ValueOwner *valueOwner, const ImportsPerDocument &imports);
~Context();
ValueOwner *valueOwner() const;
Snapshot snapshot() const;
const ScopeChain &scopeChain() const;
ScopeChain &scopeChain();
const Imports *imports(const Document *doc) const;
const Value *lookup(const QString &name, const ObjectValue **foundInScope = 0) const;
const ObjectValue *lookupType(const Document *doc, AST::UiQualifiedId *qmlTypeName,
AST::UiQualifiedId *qmlTypeNameEnd = 0) const;
const ObjectValue *lookupType(const Document *doc, const QStringList &qmlTypeName) const;
const Value *lookupReference(const Value *value) const;
QString defaultPropertyName(const ObjectValue *object) const;
private:
typedef QHash<QString, const Value *> Properties;
Snapshot _snapshot;
QSharedPointer<ValueOwner> _valueOwner;
ImportsPerDocument _imports;
ScopeChain _scopeChain;
int _qmlScopeObjectIndex;
bool _qmlScopeObjectSet;
// for avoiding reference cycles during lookup
mutable QList<const Reference *> _referenceStack;
};
} // namespace Interpreter
} // namespace QmlJS
#endif // QMLJS_CONTEXT_H

View File

@@ -31,7 +31,7 @@
**************************************************************************/
#include "qmljsevaluate.h"
#include "qmljsinterpreter.h"
#include "qmljscontext.h"
#include "qmljsvalueowner.h"
#include "parser/qmljsast_p.h"
#include <QtCore/QDebug>

View File

@@ -38,6 +38,7 @@
#include "qmljstypedescriptionreader.h"
#include "qmljsscopeastpath.h"
#include "qmljsvalueowner.h"
#include "qmljscontext.h"
#include "parser/qmljsast_p.h"
#include <languageutils/fakemetaobject.h>
@@ -778,146 +779,6 @@ QList<const ObjectValue *> ScopeChain::all() const
return _all;
}
Context::Context(const QmlJS::Snapshot &snapshot, ValueOwner *valueOwner, const ImportsPerDocument &imports)
: _snapshot(snapshot),
_valueOwner(valueOwner),
_imports(imports),
_qmlScopeObjectIndex(-1),
_qmlScopeObjectSet(false)
{
}
Context::~Context()
{
}
// the values is only guaranteed to live as long as the context
ValueOwner *Context::valueOwner() const
{
return _valueOwner.data();
}
QmlJS::Snapshot Context::snapshot() const
{
return _snapshot;
}
const ScopeChain &Context::scopeChain() const
{
return _scopeChain;
}
ScopeChain &Context::scopeChain()
{
return _scopeChain;
}
const Imports *Context::imports(const QmlJS::Document *doc) const
{
if (!doc)
return 0;
return _imports.value(doc).data();
}
const Value *Context::lookup(const QString &name, const ObjectValue **foundInScope) const
{
QList<const ObjectValue *> scopes = _scopeChain.all();
for (int index = scopes.size() - 1; index != -1; --index) {
const ObjectValue *scope = scopes.at(index);
if (const Value *member = scope->lookupMember(name, this)) {
if (foundInScope)
*foundInScope = scope;
return member;
}
}
if (foundInScope)
*foundInScope = 0;
return _valueOwner->undefinedValue();
}
const ObjectValue *Context::lookupType(const QmlJS::Document *doc, UiQualifiedId *qmlTypeName,
UiQualifiedId *qmlTypeNameEnd) const
{
const Imports *importsObj = imports(doc);
if (!importsObj)
return 0;
const ObjectValue *objectValue = importsObj->typeScope();
if (!objectValue)
return 0;
for (UiQualifiedId *iter = qmlTypeName; objectValue && iter && iter != qmlTypeNameEnd;
iter = iter->next) {
if (! iter->name)
return 0;
const Value *value = objectValue->lookupMember(iter->name->asString(), this);
if (!value)
return 0;
objectValue = value->asObjectValue();
}
return objectValue;
}
const ObjectValue *Context::lookupType(const QmlJS::Document *doc, const QStringList &qmlTypeName) const
{
const Imports *importsObj = imports(doc);
if (!importsObj)
return 0;
const ObjectValue *objectValue = importsObj->typeScope();
if (!objectValue)
return 0;
foreach (const QString &name, qmlTypeName) {
if (!objectValue)
return 0;
const Value *value = objectValue->lookupMember(name, this);
if (!value)
return 0;
objectValue = value->asObjectValue();
}
return objectValue;
}
const Value *Context::lookupReference(const Value *value) const
{
const Reference *reference = value_cast<const Reference *>(value);
if (!reference)
return value;
if (_referenceStack.contains(reference))
return 0;
_referenceStack.append(reference);
const Value *v = reference->value(this);
_referenceStack.removeLast();
return v;
}
QString Context::defaultPropertyName(const ObjectValue *object) const
{
PrototypeIterator iter(object, this);
while (iter.hasNext()) {
const ObjectValue *o = iter.next();
if (const ASTObjectValue *astObjValue = dynamic_cast<const ASTObjectValue *>(o)) {
QString defaultProperty = astObjValue->defaultPropertyName();
if (!defaultProperty.isEmpty())
return defaultProperty;
} else if (const QmlObjectValue *qmlValue = dynamic_cast<const QmlObjectValue *>(o)) {
return qmlValue->defaultPropertyName();
}
}
return QString();
}
Reference::Reference(ValueOwner *valueOwner)
: _valueOwner(valueOwner)
{

View File

@@ -74,6 +74,7 @@ class AnchorLineValue;
class Imports;
class TypeScope;
class JSImportScope;
class Context;
typedef QList<const Value *> ValueList;
@@ -316,45 +317,6 @@ private:
QList<const ObjectValue *> _all;
};
class QMLJS_EXPORT Context
{
public:
typedef QHash<const Document *, QSharedPointer<const Imports> > ImportsPerDocument;
// Context takes ownership of valueOwner
Context(const Snapshot &snapshot, ValueOwner *valueOwner, const ImportsPerDocument &imports);
~Context();
ValueOwner *valueOwner() const;
Snapshot snapshot() const;
const ScopeChain &scopeChain() const;
ScopeChain &scopeChain();
const Imports *imports(const Document *doc) const;
const Value *lookup(const QString &name, const ObjectValue **foundInScope = 0) const;
const ObjectValue *lookupType(const Document *doc, AST::UiQualifiedId *qmlTypeName,
AST::UiQualifiedId *qmlTypeNameEnd = 0) const;
const ObjectValue *lookupType(const Document *doc, const QStringList &qmlTypeName) const;
const Value *lookupReference(const Value *value) const;
QString defaultPropertyName(const ObjectValue *object) const;
private:
typedef QHash<QString, const Value *> Properties;
Snapshot _snapshot;
QSharedPointer<ValueOwner> _valueOwner;
ImportsPerDocument _imports;
ScopeChain _scopeChain;
int _qmlScopeObjectIndex;
bool _qmlScopeObjectSet;
// for avoiding reference cycles during lookup
mutable QList<const Reference *> _referenceStack;
};
class QMLJS_EXPORT Reference: public Value
{
public:

View File

@@ -31,7 +31,7 @@
**************************************************************************/
#include "qmljslookupcontext.h"
#include "qmljsinterpreter.h"
#include "qmljscontext.h"
#include "qmljslink.h"
#include "qmljsscopebuilder.h"
#include "qmljsmodelmanagerinterface.h"

View File

@@ -34,7 +34,7 @@
#define QMLJSLOOKUPCONTEXT_H
#include "qmljsdocument.h"
#include "qmljsinterpreter.h"
#include "qmljscontext.h"
#include "parser/qmljsastfwd_p.h"
#include <QtCore/QSharedPointer>

View File

@@ -33,7 +33,7 @@
#include "qmljsscopebuilder.h"
#include "qmljsbind.h"
#include "qmljsinterpreter.h"
#include "qmljscontext.h"
#include "qmljsevaluate.h"
#include "parser/qmljsast_p.h"