2010-02-16 10:36:09 +01:00
|
|
|
/**************************************************************************
|
|
|
|
**
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
**
|
|
|
|
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
**
|
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
|
|
**
|
|
|
|
** Commercial Usage
|
|
|
|
**
|
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and Nokia.
|
|
|
|
**
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
** If you are unsure which license is appropriate for your use, please
|
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
|
|
|
**
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
#include "qmljscheck.h"
|
|
|
|
#include "qmljsbind.h"
|
|
|
|
#include "qmljsinterpreter.h"
|
2010-02-16 11:53:21 +01:00
|
|
|
#include "qmljsevaluate.h"
|
2010-02-16 10:36:09 +01:00
|
|
|
#include "parser/qmljsast_p.h"
|
|
|
|
|
2010-02-18 14:24:46 +01:00
|
|
|
#include <QtGui/QApplication>
|
2010-02-16 10:36:09 +01:00
|
|
|
#include <QtCore/QDebug>
|
|
|
|
|
2010-02-18 14:24:46 +01:00
|
|
|
namespace QmlJS {
|
|
|
|
namespace Messages {
|
2010-02-18 15:15:33 +01:00
|
|
|
static const char *invalid_property_name = QT_TRANSLATE_NOOP("QmlJS::Check", "'%1' is not a valid property name");
|
|
|
|
static const char *unknown_type = QT_TRANSLATE_NOOP("QmlJS::Check", "unknown type");
|
|
|
|
static const char *has_no_members = QT_TRANSLATE_NOOP("QmlJS::Check", "'%1' does not have members");
|
|
|
|
static const char *is_not_a_member = QT_TRANSLATE_NOOP("QmlJS::Check", "'%1' is not a member of '%2'");
|
2010-02-19 15:10:39 +01:00
|
|
|
static const char *easing_curve_not_a_string = QT_TRANSLATE_NOOP("QmlJS::Check", "easing-curve name is not a string");
|
|
|
|
static const char *unknown_easing_curve_name = QT_TRANSLATE_NOOP("QmlJS::Check", "unknown easing-curve name");
|
|
|
|
static const char *value_might_be_undefined = QT_TRANSLATE_NOOP("QmlJS::Check", "value might be 'undefined'");
|
2010-02-18 14:24:46 +01:00
|
|
|
} // namespace Messages
|
|
|
|
|
|
|
|
static inline QString tr(const char *msg)
|
|
|
|
{ return qApp->translate("QmlJS::Check", msg); }
|
|
|
|
|
|
|
|
} // namespace QmlJS
|
|
|
|
|
2010-02-16 10:36:09 +01:00
|
|
|
using namespace QmlJS;
|
|
|
|
using namespace QmlJS::AST;
|
|
|
|
using namespace QmlJS::Interpreter;
|
|
|
|
|
|
|
|
Check::Check(Document::Ptr doc, const Snapshot &snapshot)
|
|
|
|
: _doc(doc)
|
|
|
|
, _snapshot(snapshot)
|
|
|
|
, _context(&_engine)
|
|
|
|
, _link(&_context, doc, snapshot)
|
2010-02-19 15:55:11 +01:00
|
|
|
, _scopeBuilder(doc, &_context)
|
2010-02-16 10:36:09 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Check::~Check()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<DiagnosticMessage> Check::operator()()
|
|
|
|
{
|
|
|
|
_messages.clear();
|
|
|
|
Node::accept(_doc->ast(), this);
|
|
|
|
return _messages;
|
|
|
|
}
|
|
|
|
|
2010-02-19 12:25:26 +01:00
|
|
|
bool Check::visit(UiProgram *)
|
2010-02-16 10:36:09 +01:00
|
|
|
{
|
|
|
|
// build the initial scope chain
|
2010-02-19 12:25:26 +01:00
|
|
|
_link.scopeChainAt(_doc);
|
2010-02-16 10:36:09 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(UiObjectDefinition *ast)
|
|
|
|
{
|
2010-02-16 13:28:43 +01:00
|
|
|
visitQmlObject(ast, ast->qualifiedTypeNameId, ast->initializer);
|
2010-02-16 10:36:09 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(UiObjectBinding *ast)
|
|
|
|
{
|
|
|
|
checkScopeObjectMember(ast->qualifiedId);
|
|
|
|
|
2010-02-16 13:28:43 +01:00
|
|
|
visitQmlObject(ast, ast->qualifiedTypeNameId, ast->initializer);
|
2010-02-16 11:53:21 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-16 13:28:43 +01:00
|
|
|
void Check::visitQmlObject(Node *ast, UiQualifiedId *typeId,
|
|
|
|
UiObjectInitializer *initializer)
|
2010-02-16 11:53:21 +01:00
|
|
|
{
|
2010-02-17 09:29:13 +01:00
|
|
|
// If the 'typeId' starts with a lower-case letter, it doesn't define
|
|
|
|
// a new object instance. For instance: anchors { ... }
|
|
|
|
if (typeId->name->asString().at(0).isLower() && ! typeId->next) {
|
|
|
|
checkScopeObjectMember(typeId);
|
|
|
|
// ### don't give up!
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-19 15:55:11 +01:00
|
|
|
_scopeBuilder.push(ast);
|
2010-02-18 14:24:46 +01:00
|
|
|
|
2010-02-16 13:28:43 +01:00
|
|
|
if (! _context.lookupType(_doc.data(), typeId)) {
|
2010-02-18 15:15:33 +01:00
|
|
|
warning(typeId->identifierToken, tr(Messages::unknown_type));
|
2010-02-19 15:55:11 +01:00
|
|
|
// suppress subsequent errors about scope object lookup by clearing
|
|
|
|
// the scope object list
|
|
|
|
// ### todo: better way?
|
|
|
|
_context.scopeChain().qmlScopeObjects.clear();
|
|
|
|
_context.scopeChain().update();
|
2010-02-16 13:28:43 +01:00
|
|
|
}
|
|
|
|
|
2010-02-16 11:53:21 +01:00
|
|
|
Node::accept(initializer, this);
|
2010-02-16 10:36:09 +01:00
|
|
|
|
2010-02-19 15:55:11 +01:00
|
|
|
_scopeBuilder.pop();
|
2010-02-16 10:36:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Check::visit(UiScriptBinding *ast)
|
|
|
|
{
|
2010-02-19 15:10:39 +01:00
|
|
|
const Value *lhsValue = checkScopeObjectMember(ast->qualifiedId);
|
|
|
|
if (lhsValue) {
|
|
|
|
// ### Fix the evaluator to accept statements!
|
|
|
|
if (ExpressionStatement *expStmt = cast<ExpressionStatement *>(ast->statement)) {
|
|
|
|
Evaluate evaluator(&_context);
|
|
|
|
const Value *rhsValue = evaluator(expStmt->expression);
|
|
|
|
|
|
|
|
const SourceLocation loc = locationFromRange(expStmt->firstSourceLocation(), expStmt->lastSourceLocation());
|
|
|
|
checkPropertyAssignment(loc, lhsValue, rhsValue, expStmt->expression);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2010-02-16 10:36:09 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-02-19 15:10:39 +01:00
|
|
|
void Check::checkPropertyAssignment(const SourceLocation &location,
|
|
|
|
const Interpreter::Value *lhsValue,
|
|
|
|
const Interpreter::Value *rhsValue,
|
|
|
|
ExpressionNode *ast)
|
|
|
|
{
|
|
|
|
if (lhsValue->asEasingCurveNameValue()) {
|
|
|
|
const StringValue *rhsStringValue = rhsValue->asStringValue();
|
|
|
|
if (!rhsStringValue) {
|
|
|
|
if (rhsValue->asUndefinedValue())
|
|
|
|
warning(location, tr(Messages::value_might_be_undefined));
|
|
|
|
else
|
|
|
|
error(location, tr(Messages::easing_curve_not_a_string));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StringLiteral *string = cast<StringLiteral *>(ast)) {
|
|
|
|
const QString value = string->value->asString();
|
|
|
|
// ### do something with easing-curve attributes.
|
|
|
|
// ### Incomplete documentation at: http://qt.nokia.com/doc/4.7-snapshot/qml-propertyanimation.html#easing-prop
|
|
|
|
// ### The implementation is at: src/declarative/util/qmlanimation.cpp
|
|
|
|
const QString curveName = value.left(value.indexOf(QLatin1Char('(')));
|
|
|
|
if (!EasingCurveNameValue::curveNames().contains(curveName)) {
|
|
|
|
error(location, tr(Messages::unknown_easing_curve_name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-16 10:36:09 +01:00
|
|
|
bool Check::visit(UiArrayBinding *ast)
|
|
|
|
{
|
|
|
|
checkScopeObjectMember(ast->qualifiedId);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-02-19 15:10:39 +01:00
|
|
|
const Value *Check::checkScopeObjectMember(const UiQualifiedId *id)
|
2010-02-16 10:36:09 +01:00
|
|
|
{
|
2010-02-19 10:14:34 +01:00
|
|
|
QList<const ObjectValue *> scopeObjects = _context.scopeChain().qmlScopeObjects;
|
2010-02-19 15:55:11 +01:00
|
|
|
if (scopeObjects.isEmpty())
|
|
|
|
return 0;
|
2010-02-16 10:36:09 +01:00
|
|
|
|
|
|
|
if (! id)
|
2010-02-19 15:10:39 +01:00
|
|
|
return 0; // ### error?
|
2010-02-16 10:36:09 +01:00
|
|
|
|
2010-02-18 14:21:53 +01:00
|
|
|
QString propertyName = id->name->asString();
|
2010-02-16 10:36:09 +01:00
|
|
|
|
|
|
|
if (propertyName == QLatin1String("id") && ! id->next)
|
2010-02-19 15:10:39 +01:00
|
|
|
return 0; // ### should probably be a special value
|
2010-02-16 10:36:09 +01:00
|
|
|
|
|
|
|
// attached properties
|
2010-02-18 15:01:26 +01:00
|
|
|
bool isAttachedProperty = false;
|
|
|
|
if (! propertyName.isEmpty() && propertyName[0].isUpper()) {
|
|
|
|
isAttachedProperty = true;
|
2010-02-19 10:14:34 +01:00
|
|
|
scopeObjects += _context.scopeChain().qmlTypes;
|
2010-02-18 15:01:26 +01:00
|
|
|
}
|
2010-02-18 10:42:15 +01:00
|
|
|
|
2010-02-19 10:14:34 +01:00
|
|
|
if (scopeObjects.isEmpty())
|
2010-02-19 15:10:39 +01:00
|
|
|
return 0;
|
2010-02-16 10:36:09 +01:00
|
|
|
|
2010-02-18 14:21:53 +01:00
|
|
|
// global lookup for first part of id
|
2010-02-19 10:14:34 +01:00
|
|
|
const Value *value = 0;
|
|
|
|
for (int i = scopeObjects.size() - 1; i >= 0; --i) {
|
|
|
|
value = scopeObjects[i]->lookupMember(propertyName, &_context);
|
|
|
|
if (value)
|
|
|
|
break;
|
|
|
|
}
|
2010-02-16 10:36:09 +01:00
|
|
|
if (!value) {
|
|
|
|
error(id->identifierToken,
|
2010-02-18 15:15:33 +01:00
|
|
|
tr(Messages::invalid_property_name).arg(propertyName));
|
2010-02-16 10:36:09 +01:00
|
|
|
}
|
|
|
|
|
2010-02-18 15:01:26 +01:00
|
|
|
// can't look up members for attached properties
|
|
|
|
if (isAttachedProperty)
|
2010-02-19 15:10:39 +01:00
|
|
|
return 0;
|
2010-02-18 15:01:26 +01:00
|
|
|
|
2010-02-18 14:21:53 +01:00
|
|
|
// member lookup
|
|
|
|
const UiQualifiedId *idPart = id;
|
|
|
|
while (idPart->next) {
|
|
|
|
const ObjectValue *objectValue = value_cast<const ObjectValue *>(value);
|
|
|
|
if (! objectValue) {
|
|
|
|
error(idPart->identifierToken,
|
2010-02-18 15:15:33 +01:00
|
|
|
tr(Messages::has_no_members).arg(propertyName));
|
2010-02-19 15:10:39 +01:00
|
|
|
return 0;
|
2010-02-18 14:21:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
idPart = idPart->next;
|
|
|
|
propertyName = idPart->name->asString();
|
|
|
|
|
|
|
|
value = objectValue->lookupMember(propertyName, &_context);
|
|
|
|
if (! value) {
|
|
|
|
error(idPart->identifierToken,
|
2010-02-18 15:15:33 +01:00
|
|
|
tr(Messages::is_not_a_member).arg(propertyName,
|
|
|
|
objectValue->className()));
|
2010-02-19 15:10:39 +01:00
|
|
|
return 0;
|
2010-02-18 14:21:53 +01:00
|
|
|
}
|
|
|
|
}
|
2010-02-19 15:10:39 +01:00
|
|
|
|
|
|
|
return value;
|
2010-02-16 10:36:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Check::error(const AST::SourceLocation &loc, const QString &message)
|
|
|
|
{
|
|
|
|
_messages.append(DiagnosticMessage(DiagnosticMessage::Error, loc, message));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Check::warning(const AST::SourceLocation &loc, const QString &message)
|
|
|
|
{
|
|
|
|
_messages.append(DiagnosticMessage(DiagnosticMessage::Warning, loc, message));
|
|
|
|
}
|
2010-02-19 15:10:39 +01:00
|
|
|
|
|
|
|
SourceLocation Check::locationFromRange(const SourceLocation &start,
|
|
|
|
const SourceLocation &end)
|
|
|
|
{
|
|
|
|
return SourceLocation(start.offset,
|
|
|
|
end.end() - start.begin(),
|
|
|
|
start.startLine,
|
|
|
|
start.startColumn);
|
|
|
|
}
|