QmlCompiler: Move ScopeType and JavaScriptIdentifier into QQmlJSScope

They don't begin with 'Q' and they are not very useful outside of
QQmlJSScope.

Change-Id: I3363ac4d29be7a9cb5c9f7f3af1727c99e886825
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Ulf Hermann 2020-10-02 14:48:22 +02:00
parent 6bb5a51a9d
commit 7200da29a8
6 changed files with 82 additions and 72 deletions

View File

@ -42,9 +42,9 @@ QQmlJSScope::Ptr QQmlJSScope::create(ScopeType type, const QQmlJSScope::Ptr &par
{
QQmlJSScope::Ptr childScope(new QQmlJSScope{type, parentScope});
if (parentScope) {
Q_ASSERT(type != ScopeType::QMLScope
Q_ASSERT(type != QQmlJSScope::QMLScope
|| !parentScope->m_parentScope
|| parentScope->parentScope()->m_scopeType == ScopeType::QMLScope
|| parentScope->parentScope()->m_scopeType == QQmlJSScope::QMLScope
|| parentScope->parentScope()->m_internalName == QLatin1String("global"));
parentScope->m_childScopes.push_back(childScope);
}
@ -53,14 +53,14 @@ QQmlJSScope::Ptr QQmlJSScope::create(ScopeType type, const QQmlJSScope::Ptr &par
void QQmlJSScope::insertJSIdentifier(const QString &name, const JavaScriptIdentifier &identifier)
{
Q_ASSERT(m_scopeType != ScopeType::QMLScope);
Q_ASSERT(m_scopeType != QQmlJSScope::QMLScope);
if (identifier.kind == JavaScriptIdentifier::LexicalScoped
|| identifier.kind == JavaScriptIdentifier::Injected
|| m_scopeType == ScopeType::JSFunctionScope) {
|| m_scopeType == QQmlJSScope::JSFunctionScope) {
m_jsIdentifiers.insert(name, identifier);
} else {
auto targetScope = parentScope();
while (targetScope->m_scopeType != ScopeType::JSFunctionScope)
while (targetScope->m_scopeType != QQmlJSScope::JSFunctionScope)
targetScope = targetScope->parentScope();
targetScope->m_jsIdentifiers.insert(name, identifier);
}
@ -80,7 +80,7 @@ bool QQmlJSScope::isIdInCurrentScope(const QString &id) const
bool QQmlJSScope::isIdInCurrentQMlScopes(const QString &id) const
{
if (m_scopeType == ScopeType::QMLScope)
if (m_scopeType == QQmlJSScope::QMLScope)
return m_properties.contains(id) || m_methods.contains(id) || m_enums.contains(id);
const auto qmlScope = findCurrentQMLScope(parentScope());
@ -91,11 +91,11 @@ bool QQmlJSScope::isIdInCurrentQMlScopes(const QString &id) const
bool QQmlJSScope::isIdInCurrentJSScopes(const QString &id) const
{
if (m_scopeType != ScopeType::QMLScope && m_jsIdentifiers.contains(id))
if (m_scopeType != QQmlJSScope::QMLScope && m_jsIdentifiers.contains(id))
return true;
for (auto jsScope = parentScope(); jsScope; jsScope = jsScope->parentScope()) {
if (jsScope->m_scopeType != ScopeType::QMLScope && jsScope->m_jsIdentifiers.contains(id))
if (jsScope->m_scopeType != QQmlJSScope::QMLScope && jsScope->m_jsIdentifiers.contains(id))
return true;
}
@ -108,11 +108,12 @@ bool QQmlJSScope::isIdInjectedFromSignal(const QString &id) const
return found.has_value() && found->kind == JavaScriptIdentifier::Injected;
}
std::optional<JavaScriptIdentifier> QQmlJSScope::findJSIdentifier(const QString &id) const
std::optional<QQmlJSScope::JavaScriptIdentifier>
QQmlJSScope::findJSIdentifier(const QString &id) const
{
for (const auto *scope = this; scope; scope = scope->parentScope().data()) {
if (scope->m_scopeType == ScopeType::JSFunctionScope
|| scope->m_scopeType == ScopeType::JSLexicalScope) {
if (scope->m_scopeType == QQmlJSScope::JSFunctionScope
|| scope->m_scopeType == QQmlJSScope::JSLexicalScope) {
auto it = scope->m_jsIdentifiers.find(id);
if (it != scope->m_jsIdentifiers.end())
return *it;
@ -153,7 +154,7 @@ void QQmlJSScope::resolveTypes(const QHash<QString, QQmlJSScope::ConstPtr> &cont
QQmlJSScope::ConstPtr QQmlJSScope::findCurrentQMLScope(const QQmlJSScope::ConstPtr &scope)
{
auto qmlScope = scope;
while (qmlScope && qmlScope->m_scopeType != ScopeType::QMLScope)
while (qmlScope && qmlScope->m_scopeType != QQmlJSScope::QMLScope)
qmlScope = qmlScope->parentScope();
return qmlScope;
}

View File

@ -52,25 +52,6 @@
QT_BEGIN_NAMESPACE
enum class ScopeType
{
JSFunctionScope,
JSLexicalScope,
QMLScope
};
struct JavaScriptIdentifier
{
enum Kind {
Parameter,
FunctionScoped,
LexicalScoped,
Injected
};
Kind kind = FunctionScoped;
QQmlJS::SourceLocation location;
};
class QQmlJSScope
{
@ -81,6 +62,13 @@ public:
using ConstPtr = QSharedPointer<const QQmlJSScope>;
using WeakConstPtr = QWeakPointer<const QQmlJSScope>;
enum ScopeType
{
JSFunctionScope,
JSLexicalScope,
QMLScope
};
enum class AccessSemantics {
Reference,
Value,
@ -119,7 +107,20 @@ public:
int m_metaObjectRevision = 0;
};
static QQmlJSScope::Ptr create(ScopeType type = ScopeType::QMLScope,
struct JavaScriptIdentifier
{
enum Kind {
Parameter,
FunctionScoped,
LexicalScoped,
Injected
};
Kind kind = FunctionScoped;
QQmlJS::SourceLocation location;
};
static QQmlJSScope::Ptr create(ScopeType type = QQmlJSScope::QMLScope,
const QQmlJSScope::Ptr &parentScope = QQmlJSScope::Ptr());
static QQmlJSScope::ConstPtr findCurrentQMLScope(const QQmlJSScope::ConstPtr &scope);
@ -209,7 +210,7 @@ private:
QString m_baseTypeName;
QQmlJSScope::WeakConstPtr m_baseType;
ScopeType m_scopeType = ScopeType::QMLScope;
ScopeType m_scopeType = QMLScope;
QList<Export> m_exports;
QString m_defaultPropertyName;

View File

@ -71,7 +71,7 @@ static QList<QQmlJSTypeReader::Import> parseHeaders(QQmlJS::AST::UiHeaderItemLis
static QQmlJSScope::Ptr parseProgram(QQmlJS::AST::Program *program, const QString &name)
{
using namespace QQmlJS::AST;
QQmlJSScope::Ptr result = QQmlJSScope::create(ScopeType::JSLexicalScope);
QQmlJSScope::Ptr result = QQmlJSScope::create(QQmlJSScope::JSLexicalScope);
result->setInternalName(name);
for (auto *statement = program->statements; statement; statement = statement->next) {
if (auto *function = cast<FunctionDeclaration *>(statement->statement)) {
@ -103,7 +103,7 @@ QQmlJSScope::Ptr QQmlJSTypeReader::operator()()
QFile file(m_file);
if (!file.open(QFile::ReadOnly)) {
QQmlJSScope::Ptr result = QQmlJSScope::create(
isJavaScript ? ScopeType::JSLexicalScope : ScopeType::QMLScope);
isJavaScript ? QQmlJSScope::JSLexicalScope : QQmlJSScope::QMLScope);
result->setInternalName(scopeName);
return result;
}
@ -119,7 +119,7 @@ QQmlJSScope::Ptr QQmlJSTypeReader::operator()()
: parser.parse();
if (!success) {
QQmlJSScope::Ptr result = QQmlJSScope::create(
isJavaScript ? ScopeType::JSLexicalScope : ScopeType::QMLScope);
isJavaScript ? QQmlJSScope::JSLexicalScope : QQmlJSScope::QMLScope);
result->setInternalName(scopeName);
return result;
}

View File

@ -259,7 +259,7 @@ bool CheckIdentifiers::checkMemberAccess(const QVector<FieldMember> &members,
if (typeFound)
continue;
if (access.m_name.front().isUpper() && scope->scopeType() == ScopeType::QMLScope) {
if (access.m_name.front().isUpper() && scope->scopeType() == QQmlJSScope::QMLScope) {
// may be an attached type
const auto it = m_types.find(access.m_name);
if (it != m_types.end() && !(*it)->attachedTypeName().isEmpty()) {
@ -307,7 +307,7 @@ bool CheckIdentifiers::operator()(
const auto memberAccessBase = memberAccessChain.takeFirst();
const auto jsId = currentScope->findJSIdentifier(memberAccessBase.m_name);
if (jsId.has_value() && jsId->kind != JavaScriptIdentifier::Injected)
if (jsId.has_value() && jsId->kind != QQmlJSScope::JavaScriptIdentifier::Injected)
continue;
auto it = qmlIDs.find(memberAccessBase.m_name);
@ -400,8 +400,9 @@ bool CheckIdentifiers::operator()(
m_colorOut->write(rootId + QLatin1Char('.'), Hint);
m_colorOut->write(issueLocationWithContext.issueText().toString(), Normal);
m_colorOut->write(issueLocationWithContext.afterText() + QLatin1Char('\n'), Normal);
} else if (jsId.has_value() && jsId->kind == JavaScriptIdentifier::Injected) {
const JavaScriptIdentifier id = jsId.value();
} else if (jsId.has_value()
&& jsId->kind == QQmlJSScope::JavaScriptIdentifier::Injected) {
const QQmlJSScope::JavaScriptIdentifier id = jsId.value();
m_colorOut->write(QLatin1String("Note: "), Info);
m_colorOut->write(
memberAccessBase.m_name + QString::fromLatin1(

View File

@ -43,7 +43,7 @@
#include <QtCore/qdiriterator.h>
#include <QtCore/qscopedvaluerollback.h>
void FindWarningVisitor::enterEnvironment(ScopeType type, const QString &name)
void FindWarningVisitor::enterEnvironment(QQmlJSScope::ScopeType type, const QString &name)
{
m_currentScope = QQmlJSScope::create(type, m_currentScope);
m_currentScope->setBaseTypeName(name);
@ -108,7 +108,10 @@ void FindWarningVisitor::flushPendingSignalParameters()
const SignalHandler handler = m_signalHandlers[m_pendingSingalHandler];
for (const QString &parameter : handler.signal.parameterNames()) {
m_currentScope->insertJSIdentifier(
parameter, { JavaScriptIdentifier::Injected, m_pendingSingalHandler});
parameter, {
QQmlJSScope::JavaScriptIdentifier::Injected,
m_pendingSingalHandler
});
}
m_pendingSingalHandler = QQmlJS::SourceLocation();
}
@ -122,7 +125,7 @@ void FindWarningVisitor::throwRecursionDepthError()
bool FindWarningVisitor::visit(QQmlJS::AST::UiProgram *)
{
enterEnvironment(ScopeType::QMLScope, "program");
enterEnvironment(QQmlJSScope::QMLScope, "program");
m_rootScopeImports = m_importer.importBuiltins();
if (!m_qmltypesFiles.isEmpty()) {
@ -153,7 +156,7 @@ void FindWarningVisitor::endVisit(QQmlJS::AST::UiProgram *)
bool FindWarningVisitor::visit(QQmlJS::AST::ClassExpression *ast)
{
enterEnvironment(ScopeType::JSFunctionScope, ast->name.toString());
enterEnvironment(QQmlJSScope::JSFunctionScope, ast->name.toString());
return true;
}
@ -164,7 +167,7 @@ void FindWarningVisitor::endVisit(QQmlJS::AST::ClassExpression *)
bool FindWarningVisitor::visit(QQmlJS::AST::ClassDeclaration *ast)
{
enterEnvironment(ScopeType::JSFunctionScope, ast->name.toString());
enterEnvironment(QQmlJSScope::JSFunctionScope, ast->name.toString());
return true;
}
@ -175,7 +178,7 @@ void FindWarningVisitor::endVisit(QQmlJS::AST::ClassDeclaration *)
bool FindWarningVisitor::visit(QQmlJS::AST::ForStatement *)
{
enterEnvironment(ScopeType::JSLexicalScope, "forloop");
enterEnvironment(QQmlJSScope::JSLexicalScope, "forloop");
return true;
}
@ -186,7 +189,7 @@ void FindWarningVisitor::endVisit(QQmlJS::AST::ForStatement *)
bool FindWarningVisitor::visit(QQmlJS::AST::ForEachStatement *)
{
enterEnvironment(ScopeType::JSLexicalScope, "foreachloop");
enterEnvironment(QQmlJSScope::JSLexicalScope, "foreachloop");
return true;
}
@ -198,7 +201,7 @@ void FindWarningVisitor::endVisit(QQmlJS::AST::ForEachStatement *)
bool FindWarningVisitor::visit(QQmlJS::AST::ExpressionStatement *)
{
if (m_pendingSingalHandler.isValid()) {
enterEnvironment(ScopeType::JSFunctionScope, "signalhandler");
enterEnvironment(QQmlJSScope::JSFunctionScope, "signalhandler");
flushPendingSignalParameters();
}
return true;
@ -206,7 +209,7 @@ bool FindWarningVisitor::visit(QQmlJS::AST::ExpressionStatement *)
void FindWarningVisitor::endVisit(QQmlJS::AST::ExpressionStatement *)
{
if (m_currentScope->scopeType() == ScopeType::JSFunctionScope
if (m_currentScope->scopeType() == QQmlJSScope::JSFunctionScope
&& m_currentScope->baseTypeName() == "signalhandler") {
leaveEnvironment();
}
@ -214,7 +217,7 @@ void FindWarningVisitor::endVisit(QQmlJS::AST::ExpressionStatement *)
bool FindWarningVisitor::visit(QQmlJS::AST::Block *)
{
enterEnvironment(ScopeType::JSLexicalScope, "block");
enterEnvironment(QQmlJSScope::JSLexicalScope, "block");
if (m_pendingSingalHandler.isValid())
flushPendingSignalParameters();
return true;
@ -227,7 +230,7 @@ void FindWarningVisitor::endVisit(QQmlJS::AST::Block *)
bool FindWarningVisitor::visit(QQmlJS::AST::CaseBlock *)
{
enterEnvironment(ScopeType::JSLexicalScope, "case");
enterEnvironment(QQmlJSScope::JSLexicalScope, "case");
return true;
}
@ -238,10 +241,10 @@ void FindWarningVisitor::endVisit(QQmlJS::AST::CaseBlock *)
bool FindWarningVisitor::visit(QQmlJS::AST::Catch *catchStatement)
{
enterEnvironment(ScopeType::JSLexicalScope, "catch");
enterEnvironment(QQmlJSScope::JSLexicalScope, "catch");
m_currentScope->insertJSIdentifier(
catchStatement->patternElement->bindingIdentifier.toString(), {
JavaScriptIdentifier::LexicalScoped,
QQmlJSScope::JavaScriptIdentifier::LexicalScoped,
catchStatement->patternElement->firstSourceLocation()
});
return true;
@ -264,7 +267,7 @@ bool FindWarningVisitor::visit(QQmlJS::AST::WithStatement *withStatement)
Normal);
}
enterEnvironment(ScopeType::JSLexicalScope, "with");
enterEnvironment(QQmlJSScope::JSLexicalScope, "with");
return true;
}
@ -389,7 +392,7 @@ bool FindWarningVisitor::visit(QQmlJS::AST::IdentifierExpression *idexp)
FindWarningVisitor::FindWarningVisitor(
QStringList qmlImportPaths, QStringList qmltypesFiles, QString code, QString fileName,
bool silent, bool warnUnqualified, bool warnWithStatement, bool warnInheritanceCycle)
: m_rootScope(QQmlJSScope::create(ScopeType::JSFunctionScope)),
: m_rootScope(QQmlJSScope::create(QQmlJSScope::JSFunctionScope)),
m_qmltypesFiles(std::move(qmltypesFiles)),
m_code(std::move(code)),
m_rootId(QLatin1String("<id>")),
@ -422,8 +425,8 @@ FindWarningVisitor::FindWarningVisitor(
QLatin1String("XMLHttpRequest")
};
JavaScriptIdentifier globalJavaScript = {
JavaScriptIdentifier::LexicalScoped,
QQmlJSScope::JavaScriptIdentifier globalJavaScript = {
QQmlJSScope::JavaScriptIdentifier::LexicalScoped,
QQmlJS::SourceLocation()
};
for (const char **globalName = QV4::Compiler::Codegen::s_globalNames;
@ -463,8 +466,8 @@ bool FindWarningVisitor::visit(QQmlJS::AST::VariableDeclarationList *vdl)
vdl->declaration->bindingIdentifier.toString(),
{
(vdl->declaration->scope == QQmlJS::AST::VariableScope::Var)
? JavaScriptIdentifier::FunctionScoped
: JavaScriptIdentifier::LexicalScoped,
? QQmlJSScope::JavaScriptIdentifier::FunctionScoped
: QQmlJSScope::JavaScriptIdentifier::LexicalScoped,
vdl->declaration->firstSourceLocation()
});
vdl = vdl->next;
@ -477,16 +480,18 @@ void FindWarningVisitor::visitFunctionExpressionHelper(QQmlJS::AST::FunctionExpr
using namespace QQmlJS::AST;
auto name = fexpr->name.toString();
if (!name.isEmpty()) {
if (m_currentScope->scopeType() == ScopeType::QMLScope) {
if (m_currentScope->scopeType() == QQmlJSScope::QMLScope) {
m_currentScope->addMethod(QQmlJSMetaMethod(name, QLatin1String("void")));
} else {
m_currentScope->insertJSIdentifier(
name,
{ JavaScriptIdentifier::LexicalScoped, fexpr->firstSourceLocation() });
name, {
QQmlJSScope::JavaScriptIdentifier::LexicalScoped,
fexpr->firstSourceLocation()
});
}
enterEnvironment(ScopeType::JSFunctionScope, name);
enterEnvironment(QQmlJSScope::JSFunctionScope, name);
} else {
enterEnvironment(ScopeType::JSFunctionScope, QLatin1String("<anon>"));
enterEnvironment(QQmlJSScope::JSFunctionScope, QLatin1String("<anon>"));
}
}
@ -516,8 +521,10 @@ bool FindWarningVisitor::visit(QQmlJS::AST::FormalParameterList *fpl)
{
for (auto const &boundName : fpl->boundNames()) {
m_currentScope->insertJSIdentifier(
boundName.id,
{JavaScriptIdentifier::Parameter, fpl->firstSourceLocation() });
boundName.id, {
QQmlJSScope::JavaScriptIdentifier::Parameter,
fpl->firstSourceLocation()
});
}
return true;
}
@ -590,7 +597,7 @@ bool FindWarningVisitor::visit(QQmlJS::AST::UiObjectBinding *uiob)
prop.setType(m_rootScopeImports.value(uiob->qualifiedTypeNameId->name.toString()));
m_currentScope->addProperty(prop);
enterEnvironment(ScopeType::QMLScope, name);
enterEnvironment(QQmlJSScope::QMLScope, name);
m_currentScope->resolveTypes(m_rootScopeImports);
importExportedNames(m_currentScope);
return true;
@ -618,7 +625,7 @@ bool FindWarningVisitor::visit(QQmlJS::AST::UiObjectDefinition *uiod)
name += id->name.toString() + QLatin1Char('.');
name.chop(1);
enterEnvironment(ScopeType::QMLScope, name);
enterEnvironment(QQmlJSScope::QMLScope, name);
if (name.isLower())
return false; // Ignore grouped properties for now
@ -651,7 +658,7 @@ bool FindWarningVisitor::visit(QQmlJS::AST::UiObjectDefinition *uiod)
QQmlJSScope::Ptr scope = m_currentScope;
do {
scope = scope->parentScope(); // TODO: rename method
} while (scope->scopeType() != ScopeType::QMLScope);
} while (scope->scopeType() != QQmlJSScope::QMLScope);
targetScope = m_rootScopeImports.value(scope->baseTypeName());
} else {
// there was a target, check if we already can find it
@ -678,8 +685,8 @@ bool FindWarningVisitor::visit(QQmlJS::AST::PatternElement *element)
m_currentScope->insertJSIdentifier(
name.id, {
(element->scope == QQmlJS::AST::VariableScope::Var)
? JavaScriptIdentifier::FunctionScoped
: JavaScriptIdentifier::LexicalScoped,
? QQmlJSScope::JavaScriptIdentifier::FunctionScoped
: QQmlJSScope::JavaScriptIdentifier::LexicalScoped,
element->firstSourceLocation()
});
}

View File

@ -97,7 +97,7 @@ private:
QQmlJSImporter m_importer;
void enterEnvironment(ScopeType type, const QString &name);
void enterEnvironment(QQmlJSScope::ScopeType type, const QString &name);
void leaveEnvironment();
void importExportedNames(QQmlJSScope::ConstPtr scope);