2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-07-01 13:51:53 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
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
|
2016-01-15 14:58:39 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2011-07-01 13:51:53 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-07-01 13:51:53 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-07-01 13:51:53 +02:00
|
|
|
|
|
|
|
|
#include "qmljscontext.h"
|
|
|
|
|
|
|
|
|
|
#include "parser/qmljsast_p.h"
|
2015-03-04 16:46:23 +01:00
|
|
|
#include "parser/qmljsengine_p.h"
|
|
|
|
|
#include "qmljsvalueowner.h"
|
2011-07-01 13:51:53 +02:00
|
|
|
|
|
|
|
|
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-17 12:29:37 +02:00
|
|
|
ViewerContext Context::viewerContext() const
|
2014-07-16 16:18:26 +02:00
|
|
|
{
|
|
|
|
|
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) {
|
2015-02-05 17:26:30 +01:00
|
|
|
const Value *value = objectValue->lookupMember(iter->name.toString(), this, 0, false);
|
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;
|
|
|
|
|
}
|