2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-07-01 13:51:53 +02:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2011-07-01 13:51:53 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-07-01 13:51:53 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2011-07-01 13:51:53 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, 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, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2011-07-01 13:51:53 +02:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-07-01 13:51:53 +02:00
|
|
|
|
|
|
|
|
#include "qmljscontext.h"
|
|
|
|
|
|
|
|
|
|
#include "parser/qmljsast_p.h"
|
|
|
|
|
|
|
|
|
|
using namespace QmlJS;
|
|
|
|
|
using namespace QmlJS::AST;
|
|
|
|
|
|
2011-11-01 14:01:07 +01:00
|
|
|
/*!
|
|
|
|
|
\class QmlJS::Context
|
2013-06-05 14:29:24 +02:00
|
|
|
\brief The Context class holds information about relationships between
|
|
|
|
|
documents in a Snapshot.
|
2011-11-09 16:02:59 +01:00
|
|
|
\sa Document Link Snapshot
|
2011-11-01 14:01:07 +01:00
|
|
|
|
2011-11-24 12:07:49 +01:00
|
|
|
Contexts are usually created through Link. Once created, a Context is immutable
|
|
|
|
|
and can be freely shared between threads.
|
2011-11-01 14:01:07 +01:00
|
|
|
|
|
|
|
|
Their main purpose is to allow lookup of types with lookupType() and resolving
|
2011-11-24 12:07:49 +01:00
|
|
|
of references through lookupReference(). As such, they form the basis for creating
|
|
|
|
|
a ScopeChain.
|
|
|
|
|
|
|
|
|
|
Information about the imports of a Document can be accessed with imports().
|
|
|
|
|
|
2014-01-24 16:53:16 +01:00
|
|
|
When dealing with a QmlJSEditor::QmlJSEditorDocument it is unnecessary to
|
2011-11-24 12:07:49 +01:00
|
|
|
construct a new Context manually. Instead use
|
2014-01-24 16:53:16 +01:00
|
|
|
QmlJSEditorDocument::semanticInfo()::context.
|
2011-11-01 14:01:07 +01:00
|
|
|
*/
|
|
|
|
|
|
2013-10-16 15:08:27 +02:00
|
|
|
ContextPtr Context::create(const QmlJS::Snapshot &snapshot, ValueOwner *valueOwner,
|
|
|
|
|
const ImportsPerDocument &imports, const ViewerContext &vContext)
|
2011-07-13 15:04:27 +02:00
|
|
|
{
|
2013-10-16 15:08:27 +02:00
|
|
|
QSharedPointer<Context> result(new Context(snapshot, valueOwner, imports, vContext));
|
2011-07-13 15:04:27 +02:00
|
|
|
result->_ptr = result;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-16 15:08:27 +02:00
|
|
|
Context::Context(const QmlJS::Snapshot &snapshot, ValueOwner *valueOwner,
|
|
|
|
|
const ImportsPerDocument &imports, const ViewerContext &vContext)
|
2011-07-01 13:51:53 +02:00
|
|
|
: _snapshot(snapshot),
|
|
|
|
|
_valueOwner(valueOwner),
|
2013-10-16 15:08:27 +02:00
|
|
|
_imports(imports),
|
|
|
|
|
_vContext(vContext)
|
2011-07-01 13:51:53 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Context::~Context()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-13 15:04:27 +02:00
|
|
|
ContextPtr Context::ptr() const
|
|
|
|
|
{
|
|
|
|
|
return _ptr.toStrongRef();
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-01 13:51:53 +02:00
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-16 16:18:26 +02:00
|
|
|
ViewerContext Context::vContext() const
|
|
|
|
|
{
|
|
|
|
|
return _vContext;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-01 13:51:53 +02:00
|
|
|
const Imports *Context::imports(const QmlJS::Document *doc) const
|
|
|
|
|
{
|
|
|
|
|
if (!doc)
|
|
|
|
|
return 0;
|
|
|
|
|
return _imports.value(doc).data();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
2011-09-13 09:57:24 +02:00
|
|
|
const Value *value = objectValue->lookupMember(iter->name.toString(), this);
|
2011-07-01 13:51:53 +02:00
|
|
|
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
|
|
|
|
|
{
|
2011-07-13 15:04:27 +02:00
|
|
|
ReferenceContext refContext(ptr());
|
2011-07-12 14:55:27 +02:00
|
|
|
return refContext.lookupReference(value);
|
2011-07-01 13:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString Context::defaultPropertyName(const ObjectValue *object) const
|
|
|
|
|
{
|
|
|
|
|
PrototypeIterator iter(object, this);
|
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
|
const ObjectValue *o = iter.next();
|
2011-10-10 10:55:37 +02:00
|
|
|
if (const ASTObjectValue *astObjValue = value_cast<ASTObjectValue>(o)) {
|
2011-07-01 13:51:53 +02:00
|
|
|
QString defaultProperty = astObjValue->defaultPropertyName();
|
|
|
|
|
if (!defaultProperty.isEmpty())
|
|
|
|
|
return defaultProperty;
|
2011-10-10 10:55:37 +02:00
|
|
|
} else if (const CppComponentValue *qmlValue = value_cast<CppComponentValue>(o)) {
|
2011-07-01 13:51:53 +02:00
|
|
|
return qmlValue->defaultPropertyName();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
2011-07-12 14:55:27 +02:00
|
|
|
|
2011-07-13 15:04:27 +02:00
|
|
|
ReferenceContext::ReferenceContext(const ContextPtr &context)
|
2011-07-12 14:55:27 +02:00
|
|
|
: m_context(context)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
const Value *ReferenceContext::lookupReference(const Value *value)
|
|
|
|
|
{
|
2011-10-10 10:55:37 +02:00
|
|
|
const Reference *reference = value_cast<Reference>(value);
|
2011-07-12 14:55:27 +02:00
|
|
|
if (!reference)
|
|
|
|
|
return value;
|
|
|
|
|
|
|
|
|
|
if (m_references.contains(reference))
|
|
|
|
|
return reference; // ### error
|
|
|
|
|
|
|
|
|
|
m_references.append(reference);
|
|
|
|
|
const Value *v = reference->value(this);
|
|
|
|
|
m_references.removeLast();
|
|
|
|
|
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-13 15:04:27 +02:00
|
|
|
const ContextPtr &ReferenceContext::context() const
|
2011-07-12 14:55:27 +02:00
|
|
|
{
|
|
|
|
|
return m_context;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-13 15:04:27 +02:00
|
|
|
ReferenceContext::operator const ContextPtr &() const
|
2011-07-12 14:55:27 +02:00
|
|
|
{
|
|
|
|
|
return m_context;
|
|
|
|
|
}
|