Commit 4b2f42cf authored by Christian Kamm's avatar Christian Kamm

QmlJS: Adjust existing code for updated QML parser.

Change-Id: I153723eeb9973be025daf47e317f7b9d076a3c72
Reviewed-on: http://codereview.qt-project.org/4733Reviewed-by: default avatarThomas Hartmann <Thomas.Hartmann@nokia.com>
parent b5312090
......@@ -124,9 +124,10 @@ bool Bind::usesQmlPrototype(ObjectValue *prototype,
// if there are no renamed imports and the document does not use
// the className string anywhere, it's out
if (possibleNames.isEmpty()) {
NameId nameId(componentName.data(), componentName.size());
if (!_doc->engine()->literals().contains(nameId))
return false;
// ### FIXME!
// NameId nameId(componentName.data(), componentName.size());
// if (!_doc->engine()->literals().contains(nameId))
// return false;
}
QHashIterator<Node *, ObjectValue *> it(_qmlObjects);
......@@ -148,12 +149,12 @@ bool Bind::usesQmlPrototype(ObjectValue *prototype,
// optimize the common case of no renamed imports
if (possibleNames.isEmpty()) {
for (UiQualifiedId *idIt = id; idIt; idIt = idIt->next) {
if (!idIt->next && idIt->name->asString() != componentName)
if (!idIt->next && idIt->name != componentName)
skip = true;
}
} else {
for (UiQualifiedId *idIt = id; idIt; idIt = idIt->next) {
if (!idIt->next && !possibleNames.contains(idIt->name->asString()))
if (!idIt->next && !possibleNames.contains(idIt->name.toString()))
skip = true;
}
}
......@@ -195,8 +196,7 @@ QString Bind::toString(UiQualifiedId *qualifiedId, QChar delimiter)
if (iter != qualifiedId)
result += delimiter;
if (iter->name)
result += iter->name->asString();
result += iter->name;
}
return result;
......@@ -267,10 +267,11 @@ bool Bind::visit(UiImport *ast)
_diagnosticMessages->append(
errorMessage(ast, tr("package import requires a version number")));
}
} else if (ast->fileName) {
QFileInfo importFileInfo(ast->fileName->asString());
} else if (!ast->fileName.isEmpty()) {
const QString &fileName = ast->fileName.toString();
QFileInfo importFileInfo(fileName);
if (!importFileInfo.isAbsolute()) {
importFileInfo=QFileInfo(_doc->path() + QDir::separator() + ast->fileName->asString());
importFileInfo=QFileInfo(_doc->path() + QDir::separator() + fileName);
}
name = importFileInfo.absoluteFilePath();
if (importFileInfo.isFile())
......@@ -306,8 +307,8 @@ bool Bind::visit(UiObjectDefinition *ast)
// an UiObjectDefinition may be used to group property bindings
// think anchors { ... }
bool isGroupedBinding = ast->qualifiedTypeNameId
&& ast->qualifiedTypeNameId->name
&& ast->qualifiedTypeNameId->name->asString().at(0).isLower();
&& !ast->qualifiedTypeNameId->name.isEmpty()
&& ast->qualifiedTypeNameId->name.at(0).isLower();
if (!isGroupedBinding) {
ObjectValue *value = bindObject(ast->qualifiedTypeNameId, ast->initializer);
......@@ -338,8 +339,8 @@ bool Bind::visit(UiScriptBinding *ast)
if (_currentObjectValue && toString(ast->qualifiedId) == QLatin1String("id")) {
if (ExpressionStatement *e = cast<ExpressionStatement*>(ast->statement))
if (IdentifierExpression *i = cast<IdentifierExpression*>(e->expression))
if (i->name)
_idEnvironment->setMember(i->name->asString(), _currentObjectValue);
if (!i->name.isEmpty())
_idEnvironment->setMember(i->name.toString(), _currentObjectValue);
}
const Block *block = AST::cast<const Block*>(ast->statement);
if (block) {
......@@ -363,12 +364,12 @@ bool Bind::visit(UiArrayBinding *)
bool Bind::visit(VariableDeclaration *ast)
{
if (! ast->name)
if (ast->name.isEmpty())
return false;
ASTVariableReference *ref = new ASTVariableReference(ast, _doc, &_valueOwner);
if (_currentObjectValue)
_currentObjectValue->setMember(ast->name->asString(), ref);
_currentObjectValue->setMember(ast->name.toString(), ref);
return true;
}
......@@ -379,8 +380,8 @@ bool Bind::visit(FunctionExpression *ast)
// return false;
ASTFunctionValue *function = new ASTFunctionValue(ast, _doc, &_valueOwner);
if (_currentObjectValue && ast->name && cast<FunctionDeclaration *>(ast))
_currentObjectValue->setMember(ast->name->asString(), function);
if (_currentObjectValue && !ast->name.isEmpty() && cast<FunctionDeclaration *>(ast))
_currentObjectValue->setMember(ast->name.toString(), function);
// build function scope
ObjectValue *functionScope = _valueOwner.newObject(/*prototype=*/0);
......@@ -392,8 +393,8 @@ bool Bind::visit(FunctionExpression *ast)
// 1. Function formal arguments
for (FormalParameterList *it = ast->formals; it; it = it->next) {
if (it->name)
functionScope->setMember(it->name->asString(), _valueOwner.undefinedValue());
if (!it->name.isEmpty())
functionScope->setMember(it->name.toString(), _valueOwner.undefinedValue());
}
// 2. Functions defined inside the function body
......
......@@ -80,7 +80,7 @@ SourceLocation QmlJS::fullLocationForQualifiedId(AST::UiQualifiedId *qualifiedId
SourceLocation end = qualifiedId->identifierToken;
for (UiQualifiedId *iter = qualifiedId; iter; iter = iter->next) {
if (iter->name)
if (iter->identifierToken.isValid())
end = iter->identifierToken;
}
......@@ -160,7 +160,7 @@ public:
{
if (const QmlEnumValue *enumValue = dynamic_cast<const QmlEnumValue *>(value)) {
if (StringLiteral *stringLiteral = cast<StringLiteral *>(_ast)) {
const QString valueName = stringLiteral->value->asString();
const QString valueName = stringLiteral->value.toString();
if (!enumValue->keys().contains(valueName)) {
_message.message = Check::tr("unknown value for enum");
......@@ -201,7 +201,7 @@ public:
if (value && value->asUrlValue()) {
if (StringLiteral *literal = cast<StringLiteral *>(_ast)) {
QUrl url(literal->value->asString());
QUrl url(literal->value.toString());
if (!url.isValid() && !url.isEmpty()) {
_message.message = Check::tr("not a valid url");
} else {
......@@ -224,7 +224,7 @@ public:
virtual void visit(const ColorValue *)
{
if (StringLiteral *stringLiteral = cast<StringLiteral *>(_ast)) {
if (!toQColor(stringLiteral->value->asString()).isValid())
if (!toQColor(stringLiteral->value.toString()).isValid())
_message.message = Check::tr("not a valid color");
} else {
visit((StringValue *)0);
......@@ -302,16 +302,16 @@ protected:
else
break;
}
if (ast->label)
_labels[ast->label->asString()] = end;
if (!ast->label.isEmpty())
_labels[ast->label.toString()] = end;
return true;
}
virtual bool visit(BreakStatement *ast)
{
_state = Break;
if (ast->label) {
if (Node *target = _labels.value(ast->label->asString()))
if (!ast->label.isEmpty()) {
if (Node *target = _labels.value(ast->label.toString()))
_labelledBreaks.insert(target);
}
return false;
......@@ -441,8 +441,8 @@ public:
clear();
_options = options;
for (FormalParameterList *plist = function->formals; plist; plist = plist->next) {
if (plist->name)
_formalParameterNames += plist->name->asString();
if (!plist->name.isEmpty())
_formalParameterNames += plist->name.toString();
}
Node::accept(function->body, this);
......@@ -478,9 +478,9 @@ protected:
bool visit(IdentifierExpression *ast)
{
if (!ast->name)
if (ast->name.isEmpty())
return false;
const QString name = ast->name->asString();
const QString &name = ast->name.toString();
if (!_declaredFunctions.contains(name) && !_declaredVariables.contains(name))
_possiblyUndeclaredUses[name].append(ast->identifierToken);
return false;
......@@ -496,9 +496,9 @@ protected:
bool visit(VariableDeclaration *ast)
{
if (!ast->name)
if (ast->name.isEmpty())
return true;
const QString name = ast->name->asString();
const QString &name = ast->name.toString();
if (_options & Check::WarnDuplicateDeclaration) {
if (_formalParameterNames.contains(name)) {
......@@ -534,9 +534,9 @@ protected:
bool visit(FunctionExpression *ast)
{
if (!ast->name)
if (ast->name.isEmpty())
return false;
const QString name = ast->name->asString();
const QString &name = ast->name.toString();
if (_options & Check::WarnDuplicateDeclaration) {
if (_formalParameterNames.contains(name)) {
......@@ -627,10 +627,10 @@ bool Check::visit(UiObjectInitializer *)
{
m_propertyStack.push(StringSet());
UiObjectDefinition *objectDefinition = cast<UiObjectDefinition *>(parent());
if (objectDefinition && objectDefinition->qualifiedTypeNameId->name->asString() == "Component")
if (objectDefinition && objectDefinition->qualifiedTypeNameId->name == "Component")
m_idStack.push(StringSet());
UiObjectBinding *objectBinding = cast<UiObjectBinding *>(parent());
if (objectBinding && objectBinding->qualifiedTypeNameId->name->asString() == "Component")
if (objectBinding && objectBinding->qualifiedTypeNameId->name == "Component")
m_idStack.push(StringSet());
if (m_idStack.isEmpty())
m_idStack.push(StringSet());
......@@ -641,10 +641,10 @@ void Check::endVisit(UiObjectInitializer *)
{
m_propertyStack.pop();
UiObjectDefinition *objectDenition = cast<UiObjectDefinition *>(parent());
if (objectDenition && objectDenition->qualifiedTypeNameId->name->asString() == "Component")
if (objectDenition && objectDenition->qualifiedTypeNameId->name == "Component")
m_idStack.pop();
UiObjectBinding *objectBinding = cast<UiObjectBinding *>(parent());
if (objectBinding && objectBinding->qualifiedTypeNameId->name->asString() == "Component")
if (objectBinding && objectBinding->qualifiedTypeNameId->name == "Component")
m_idStack.pop();
}
......@@ -738,7 +738,7 @@ void Check::visitQmlObject(Node *ast, UiQualifiedId *typeId,
bool Check::visit(UiScriptBinding *ast)
{
// special case for id property
if (ast->qualifiedId->name->asString() == QLatin1String("id") && ! ast->qualifiedId->next) {
if (ast->qualifiedId->name == QLatin1String("id") && ! ast->qualifiedId->next) {
if (! ast->statement)
return false;
......@@ -753,16 +753,16 @@ bool Check::visit(UiScriptBinding *ast)
QString id;
if (IdentifierExpression *idExp = cast<IdentifierExpression *>(expStmt->expression)) {
id = idExp->name->asString();
id = idExp->name.toString();
} else if (StringLiteral *strExp = cast<StringLiteral *>(expStmt->expression)) {
id = strExp->value->asString();
id = strExp->value.toString();
warning(loc, Check::tr("using string literals for ids is discouraged"));
} else {
error(loc, Check::tr("expected id"));
return false;
}
if (id.isEmpty() || (!id[0].isLower() && id[0] != '_')) {
if (id.isEmpty() || (!id.at(0).isLower() && id.at(0) != '_')) {
error(loc, Check::tr("ids must be lower case or start with underscore"));
return false;
}
......@@ -813,8 +813,8 @@ bool Check::visit(UiArrayBinding *ast)
bool Check::visit(UiPublicMember *ast)
{
// check if the member type is valid
if (ast->memberType) {
const QString name = ast->memberType->asString();
if (!ast->memberType.isEmpty()) {
const QString &name = ast->memberType.toString();
if (!name.isEmpty() && name.at(0).isLower()) {
if (!isValidBuiltinPropertyType(name))
error(ast->typeToken, tr("'%1' is not a valid property type").arg(name));
......@@ -837,7 +837,7 @@ bool Check::visit(IdentifierExpression *ast)
return true;
_lastValue = 0;
if (ast->name) {
if (!ast->name.isEmpty()) {
Evaluate evaluator(&_scopeChain);
_lastValue = evaluator.reference(ast);
if (!_lastValue)
......@@ -864,11 +864,11 @@ bool Check::visit(FieldMemberExpression *ast)
error(locationFromRange(ast->base->firstSourceLocation(), ast->base->lastSourceLocation()),
tr("does not have members"));
}
if (!obj || !ast->name) {
if (!obj || ast->name.isEmpty()) {
_lastValue = 0;
return false;
}
_lastValue = obj->lookupMember(ast->name->asString(), _context);
_lastValue = obj->lookupMember(ast->name.toString(), _context);
if (!_lastValue)
error(ast->identifierToken, tr("unknown member"));
return false;
......@@ -1093,14 +1093,14 @@ bool Check::visit(DefaultClause *ast)
static QString functionName(ExpressionNode *ast, SourceLocation *location)
{
if (IdentifierExpression *id = cast<IdentifierExpression *>(ast)) {
if (id->name) {
if (!id->name.isEmpty()) {
*location = id->identifierToken;
return id->name->asString();
return id->name.toString();
}
} else if (FieldMemberExpression *fme = cast<FieldMemberExpression *>(ast)) {
if (fme->name) {
if (!fme->name.isEmpty()) {
*location = fme->identifierToken;
return fme->name->asString();
return fme->name.toString();
}
}
return QString();
......@@ -1169,10 +1169,10 @@ const Value *Check::checkScopeObjectMember(const UiQualifiedId *id)
if (! id)
return 0; // ### error?
if (! id->name) // possible after error recovery
if (id->name.isEmpty()) // possible after error recovery
return 0;
QString propertyName = id->name->asString();
QString propertyName = id->name.toString();
if (propertyName == QLatin1String("id") && ! id->next)
return 0; // ### should probably be a special value
......@@ -1219,14 +1219,14 @@ const Value *Check::checkScopeObjectMember(const UiQualifiedId *id)
return 0;
}
if (! idPart->next->name) {
if (idPart->next->name.isEmpty()) {
// somebody typed "id." and error recovery still gave us a valid tree,
// so just bail out here.
return 0;
}
idPart = idPart->next;
propertyName = idPart->name->asString();
propertyName = idPart->name.toString();
value = objectValue->lookupMember(propertyName, _context);
if (! value) {
......
......@@ -90,10 +90,7 @@ const ObjectValue *Context::lookupType(const QmlJS::Document *doc, UiQualifiedId
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);
const Value *value = objectValue->lookupMember(iter->name.toString(), this);
if (!value)
return 0;
......
......@@ -83,11 +83,11 @@ static QString label(UiQualifiedId *id)
{
QString str;
for (; id ; id = id->next) {
if (!id->name)
if (id->name.isEmpty())
return QString();
if (!str.isEmpty())
str += QLatin1Char('.');
str += id->name->asString();
str += id->name;
}
return str;
}
......@@ -284,10 +284,10 @@ static QString _propertyName(UiQualifiedId *id)
QString s;
for (; id; id = id->next) {
if (! id->name)
if (id->name.isEmpty())
return QString();
s += id->name->asString();
s += id->name;
if (id->next)
s += QLatin1Char('.');
......@@ -300,7 +300,7 @@ static QString _methodName(UiSourceElement *source)
{
if (source) {
if (FunctionDeclaration *declaration = cast<FunctionDeclaration*>(source->sourceElement)) {
return declaration->name->asString();
return declaration->name.toString();
}
}
return QString();
......
......@@ -35,7 +35,6 @@
#include <qmljs/parser/qmljsast_p.h>
#include <qmljs/parser/qmljslexer_p.h>
#include <qmljs/parser/qmljsparser_p.h>
#include <qmljs/parser/qmljsnodepool_p.h>
#include <qmljs/parser/qmljsastfwd_p.h>
#include <QtCore/QDir>
......@@ -85,7 +84,6 @@ using namespace QmlJS::AST;
Document::Document(const QString &fileName, Language language)
: _engine(0)
, _pool(0)
, _ast(0)
, _bind(0)
, _fileName(QDir::cleanPath(fileName))
......@@ -115,9 +113,6 @@ Document::~Document()
if (_engine)
delete _engine;
if (_pool)
delete _pool;
}
Document::Ptr Document::create(const QString &fileName, Language language)
......@@ -219,12 +214,10 @@ QString Document::componentName() const
bool Document::parse_helper(int startToken)
{
Q_ASSERT(! _engine);
Q_ASSERT(! _pool);
Q_ASSERT(! _ast);
Q_ASSERT(! _bind);
_engine = new Engine();
_pool = new NodePool(_fileName, _engine);
Lexer lexer(_engine);
Parser parser(_engine);
......@@ -233,7 +226,7 @@ bool Document::parse_helper(int startToken)
if (startToken == QmlJSGrammar::T_FEED_JS_PROGRAM)
extractPragmas(&source);
lexer.setCode(source, /*line = */ 1);
lexer.setCode(source, /*line = */ 1, /*qmlMode = */_language == QmlLanguage);
switch (startToken) {
case QmlJSGrammar::T_FEED_UI_PROGRAM:
......
......@@ -112,7 +112,6 @@ private:
private:
QmlJS::Engine *_engine;
NodePool *_pool;
AST::Node *_ast;
Bind *_bind;
QList<QmlJS::DiagnosticMessage> _diagnosticMessages;
......
......@@ -168,10 +168,10 @@ bool Evaluate::visit(AST::UiArrayMemberList *)
bool Evaluate::visit(AST::UiQualifiedId *ast)
{
if (! ast->name)
if (ast->name.isEmpty())
return false;
const Value *value = _scopeChain->lookup(ast->name->asString());
const Value *value = _scopeChain->lookup(ast->name.toString());
if (! ast->next) {
_result = value;
......@@ -179,11 +179,11 @@ bool Evaluate::visit(AST::UiQualifiedId *ast)
const ObjectValue *base = value_cast<const ObjectValue *>(value);
for (AST::UiQualifiedId *it = ast->next; base && it; it = it->next) {
NameId *name = it->name;
if (! name)
const QString &name = it->name.toString();
if (name.isEmpty())
break;
const Value *value = base->lookupMember(name->asString(), _context);
const Value *value = base->lookupMember(name, _context);
if (! it->next)
_result = value;
else
......@@ -216,10 +216,10 @@ bool Evaluate::visit(AST::ThisExpression *)
bool Evaluate::visit(AST::IdentifierExpression *ast)
{
if (! ast->name)
if (ast->name.isEmpty())
return false;
_result = _scopeChain->lookup(ast->name->asString());
_result = _scopeChain->lookup(ast->name.toString());
return false;
}
......@@ -314,12 +314,12 @@ bool Evaluate::visit(AST::ArrayMemberExpression *)
bool Evaluate::visit(AST::FieldMemberExpression *ast)
{
if (! ast->name)
if (ast->name.isEmpty())
return false;
if (const Value *base = _valueOwner->convertToObject(value(ast->base))) {
if (const ObjectValue *obj = base->asObjectValue()) {
_result = obj->lookupMember(ast->name->asString(), _context);
_result = obj->lookupMember(ast->name.toString(), _context);
}
}
......
......@@ -1667,12 +1667,12 @@ ASTObjectValue::ASTObjectValue(UiQualifiedId *typeName,
for (UiObjectMemberList *it = _initializer->members; it; it = it->next) {
UiObjectMember *member = it->member;
if (UiPublicMember *def = cast<UiPublicMember *>(member)) {
if (def->type == UiPublicMember::Property && def->name && def->memberType) {
if (def->type == UiPublicMember::Property && !def->name.isEmpty() && !def->memberType.isEmpty()) {
ASTPropertyReference *ref = new ASTPropertyReference(def, _doc, valueOwner);
_properties.append(ref);
if (def->defaultToken.isValid())
_defaultPropertyRef = ref;
} else if (def->type == UiPublicMember::Signal && def->name) {
} else if (def->type == UiPublicMember::Signal && !def->name.isEmpty()) {
ASTSignalReference *ref = new ASTSignalReference(def, _doc, valueOwner);
_signals.append(ref);
}
......@@ -1696,12 +1696,12 @@ bool ASTObjectValue::getSourceLocation(QString *fileName, int *line, int *column
void ASTObjectValue::processMembers(MemberProcessor *processor) const
{
foreach (ASTPropertyReference *ref, _properties) {
processor->processProperty(ref->ast()->name->asString(), ref);
processor->processProperty(ref->ast()->name.toString(), ref);
// ### Should get a different value?
processor->processGeneratedSlot(ref->onChangedSlotName(), ref);
}
foreach (ASTSignalReference *ref, _signals) {
processor->processSignal(ref->ast()->name->asString(), ref);
processor->processSignal(ref->ast()->name.toString(), ref);
// ### Should get a different value?
processor->processGeneratedSlot(ref->slotName(), ref);
}
......@@ -1713,8 +1713,8 @@ QString ASTObjectValue::defaultPropertyName() const
{
if (_defaultPropertyRef) {
UiPublicMember *prop = _defaultPropertyRef->ast();
if (prop && prop->name)
return prop->name->asString();
if (prop)
return prop->name.toString();
}
return QString();
}
......@@ -1773,7 +1773,7 @@ ASTFunctionValue::ASTFunctionValue(FunctionExpression *ast, const Document *doc,
setPrototype(valueOwner->functionPrototype());
for (FormalParameterList *it = ast->formals; it; it = it->next)
_argumentNames.append(it->name);
_argumentNames.append(it->name.toString());
}
ASTFunctionValue::~ASTFunctionValue()
......@@ -1803,8 +1803,9 @@ const Value *ASTFunctionValue::argument(int) const
QString ASTFunctionValue::argumentName(int index) const
{
if (index < _argumentNames.size()) {
if (NameId *nameId = _argumentNames.at(index))
return nameId->asString();
const QString &name = _argumentNames.at(index);
if (!name.isEmpty())
return name;
}
return FunctionValue::argumentName(index);
......@@ -1848,7 +1849,7 @@ const Value *QmlPrototypeReference::value(ReferenceContext *referenceContext) co
ASTPropertyReference::ASTPropertyReference(UiPublicMember *ast, const Document *doc, ValueOwner *valueOwner)
: Reference(valueOwner), _ast(ast), _doc(doc)
{
const QString propertyName = ast->name->asString();
const QString &propertyName = ast->name.toString();
_onChangedSlotName = QLatin1String("on");
_onChangedSlotName += propertyName.at(0).toUpper();
_onChangedSlotName += propertyName.midRef(1);
......@@ -1870,8 +1871,8 @@ bool ASTPropertyReference::getSourceLocation(QString *fileName, int *line, int *
const Value *ASTPropertyReference::value(ReferenceContext *referenceContext) const
{
if (_ast->statement
&& (!_ast->memberType || _ast->memberType->asString() == QLatin1String("variant")
|| _ast->memberType->asString() == QLatin1String("alias"))) {
&& (_ast->memberType.isEmpty() || _ast->memberType == QLatin1String("variant")
|| _ast->memberType == QLatin1String("alias"))) {
// Adjust the context for the current location - expensive!
// ### Improve efficiency by caching the 'use chain' constructed in ScopeBuilder.
......@@ -1887,8 +1888,8 @@ const Value *ASTPropertyReference::value(ReferenceContext *referenceContext) con
return evaluator(_ast->statement);
}
if (_ast->memberType)
return valueOwner()->defaultValueForBuiltinType(_ast->memberType->asString());