| 1 | // Copyright (C) 2021 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | #include "qqmljsfunctioninitializer_p.h" |
| 5 | |
| 6 | #include <private/qqmljsmemorypool_p.h> |
| 7 | |
| 8 | #include <QtCore/qloggingcategory.h> |
| 9 | #include <QtCore/qfileinfo.h> |
| 10 | |
| 11 | #include <QtQml/private/qqmlsignalnames_p.h> |
| 12 | |
| 13 | QT_BEGIN_NAMESPACE |
| 14 | |
| 15 | using namespace Qt::StringLiterals; |
| 16 | |
| 17 | /*! |
| 18 | * \internal |
| 19 | * \class QQmlJSFunctionInitializer |
| 20 | * |
| 21 | * QQmlJSFunctionInitializer analyzes the IR to produce an initial |
| 22 | * QQmlJSCompilePass::Function for further analysis. It only looks for the |
| 23 | * signature and the QML scope and doesn't visit the byte code. |
| 24 | */ |
| 25 | |
| 26 | static QString bindingTypeDescription(QmlIR::Binding::Type type) |
| 27 | { |
| 28 | switch (type) { |
| 29 | case QmlIR::Binding::Type_Invalid: |
| 30 | return u"invalid"_s ; |
| 31 | case QmlIR::Binding::Type_Boolean: |
| 32 | return u"a boolean"_s ; |
| 33 | case QmlIR::Binding::Type_Number: |
| 34 | return u"a number"_s ; |
| 35 | case QmlIR::Binding::Type_String: |
| 36 | return u"a string"_s ; |
| 37 | case QmlIR::Binding::Type_Null: |
| 38 | return u"null"_s ; |
| 39 | case QmlIR::Binding::Type_Translation: |
| 40 | return u"a translation"_s ; |
| 41 | case QmlIR::Binding::Type_TranslationById: |
| 42 | return u"a translation by id"_s ; |
| 43 | case QmlIR::Binding::Type_Script: |
| 44 | return u"a script"_s ; |
| 45 | case QmlIR::Binding::Type_Object: |
| 46 | return u"an object"_s ; |
| 47 | case QmlIR::Binding::Type_AttachedProperty: |
| 48 | return u"an attached property"_s ; |
| 49 | case QmlIR::Binding::Type_GroupProperty: |
| 50 | return u"a grouped property"_s ; |
| 51 | } |
| 52 | |
| 53 | return u"nothing"_s ; |
| 54 | } |
| 55 | |
| 56 | void QQmlJSFunctionInitializer::populateSignature( |
| 57 | const QV4::Compiler::Context *context, QQmlJS::AST::FunctionExpression *ast, |
| 58 | QQmlJSCompilePass::Function *function, QQmlJS::DiagnosticMessage *error) |
| 59 | { |
| 60 | const auto signatureError = [&](const QString &message) { |
| 61 | error->type = QtWarningMsg; |
| 62 | error->loc = ast->firstSourceLocation(); |
| 63 | error->message = message; |
| 64 | function->isFullyTyped = false; |
| 65 | }; |
| 66 | |
| 67 | if (!m_typeResolver->canCallJSFunctions()) { |
| 68 | signatureError(u"Ignoring type annotations as requested " |
| 69 | "by pragma FunctionSignatureBehavior"_s ); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | QQmlJS::AST::BoundNames arguments; |
| 74 | if (ast->formals) |
| 75 | arguments = ast->formals->formals(); |
| 76 | |
| 77 | // If the function has no arguments and no return type annotation we assume it's untyped. |
| 78 | // You can annotate it to return void to make it typed. |
| 79 | // Otherwise we first assume it's typed and reset the flag if we detect a problem. |
| 80 | function->isFullyTyped = !arguments.isEmpty() || ast->typeAnnotation; |
| 81 | |
| 82 | if (function->argumentTypes.isEmpty()) { |
| 83 | for (const QQmlJS::AST::BoundName &argument : std::as_const(t&: arguments)) { |
| 84 | if (argument.typeAnnotation) { |
| 85 | if (const auto type = m_typeResolver->typeFromAST(type: argument.typeAnnotation->type)) { |
| 86 | function->argumentTypes.append( |
| 87 | t: m_typeResolver->tracked( |
| 88 | type: m_typeResolver->globalType(type))); |
| 89 | } else { |
| 90 | function->argumentTypes.append( |
| 91 | t: m_typeResolver->tracked( |
| 92 | type: m_typeResolver->globalType(type: m_typeResolver->varType()))); |
| 93 | signatureError(u"Cannot resolve the argument type %1."_s |
| 94 | .arg(a: argument.typeAnnotation->type->toString())); |
| 95 | } |
| 96 | } else { |
| 97 | function->argumentTypes.append( |
| 98 | t: m_typeResolver->tracked( |
| 99 | type: m_typeResolver->globalType(type: m_typeResolver->varType()))); |
| 100 | signatureError(u"Functions without type annotations won't be compiled"_s ); |
| 101 | } |
| 102 | } |
| 103 | } else { |
| 104 | for (qsizetype i = 0, end = arguments.size(); i != end; ++i) { |
| 105 | const QQmlJS::AST::BoundName &argument = arguments[i]; |
| 106 | if (argument.typeAnnotation) { |
| 107 | if (const auto type = m_typeResolver->typeFromAST(type: argument.typeAnnotation->type)) { |
| 108 | if (!m_typeResolver->registerContains(reg: function->argumentTypes[i], type)) { |
| 109 | signatureError(u"Type annotation %1 on signal handler " |
| 110 | "contradicts signal argument type %2"_s |
| 111 | .arg(args: argument.typeAnnotation->type->toString(), |
| 112 | args: function->argumentTypes[i].descriptiveName())); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if (!function->returnType.isValid()) { |
| 120 | if (ast->typeAnnotation) { |
| 121 | function->returnType = m_typeResolver->globalType( |
| 122 | type: m_typeResolver->typeFromAST(type: ast->typeAnnotation->type)); |
| 123 | if (!function->returnType.isValid()) |
| 124 | signatureError(u"Cannot resolve return type %1"_s .arg( |
| 125 | a: QmlIR::IRBuilder::asString(node: ast->typeAnnotation->type->typeId))); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | for (int i = QQmlJSCompilePass::FirstArgument + function->argumentTypes.size(); |
| 130 | i < context->registerCountInFunction; ++i) { |
| 131 | function->registerTypes.append(t: m_typeResolver->tracked( |
| 132 | type: m_typeResolver->globalType(type: m_typeResolver->voidType()))); |
| 133 | } |
| 134 | |
| 135 | function->addressableScopes = m_typeResolver->objectsById(); |
| 136 | function->code = context->code; |
| 137 | function->sourceLocations = context->sourceLocationTable.get(); |
| 138 | } |
| 139 | |
| 140 | static void diagnose( |
| 141 | const QString &message, QtMsgType type, const QQmlJS::SourceLocation &location, |
| 142 | QQmlJS::DiagnosticMessage *error) |
| 143 | { |
| 144 | *error = QQmlJS::DiagnosticMessage{ |
| 145 | .message: message, |
| 146 | .type: type, |
| 147 | .loc: location |
| 148 | }; |
| 149 | } |
| 150 | |
| 151 | QQmlJSCompilePass::Function QQmlJSFunctionInitializer::run( |
| 152 | const QV4::Compiler::Context *context, |
| 153 | const QString &propertyName, |
| 154 | QQmlJS::AST::Node *astNode, |
| 155 | const QmlIR::Binding &irBinding, |
| 156 | QQmlJS::DiagnosticMessage *error) |
| 157 | { |
| 158 | QQmlJS::SourceLocation bindingLocation; |
| 159 | bindingLocation.startLine = irBinding.location.line(); |
| 160 | bindingLocation.startColumn = irBinding.location.column(); |
| 161 | |
| 162 | QQmlJSCompilePass::Function function; |
| 163 | function.qmlScope = m_scopeType; |
| 164 | |
| 165 | if (irBinding.type() != QmlIR::Binding::Type_Script) { |
| 166 | diagnose(message: u"Binding is not a script binding, but %1."_s .arg( |
| 167 | a: bindingTypeDescription(type: QmlIR::Binding::Type(quint32(irBinding.type())))), |
| 168 | type: QtDebugMsg, location: bindingLocation, error); |
| 169 | } |
| 170 | |
| 171 | function.isProperty = m_objectType->hasProperty(name: propertyName); |
| 172 | if (function.isProperty) { |
| 173 | const auto property = m_objectType->property(name: propertyName); |
| 174 | if (const QQmlJSScope::ConstPtr propertyType = property.type()) { |
| 175 | function.returnType = m_typeResolver->globalType(type: propertyType->isListProperty() |
| 176 | ? m_typeResolver->qObjectListType() |
| 177 | : QQmlJSScope::ConstPtr(property.type())); |
| 178 | } else { |
| 179 | diagnose(message: u"Cannot resolve property type %1 for binding on %2."_s |
| 180 | .arg(args: property.typeName(), args: propertyName), |
| 181 | type: QtWarningMsg, location: bindingLocation, error); |
| 182 | } |
| 183 | |
| 184 | if (!property.bindable().isEmpty() && !property.isPrivate()) |
| 185 | function.isQPropertyBinding = true; |
| 186 | } else if (QQmlSignalNames::isHandlerName(signalName: propertyName)) { |
| 187 | if (auto actualPropertyName = |
| 188 | QQmlSignalNames::changedHandlerNameToPropertyName(handler: propertyName); |
| 189 | actualPropertyName && m_objectType->hasProperty(name: *actualPropertyName)) { |
| 190 | function.isSignalHandler = true; |
| 191 | } else { |
| 192 | auto signalName = QQmlSignalNames::handlerNameToSignalName(handler: propertyName); |
| 193 | const auto methods = m_objectType->methods(name: *signalName); |
| 194 | for (const auto &method : methods) { |
| 195 | if (method.isCloned()) |
| 196 | continue; |
| 197 | if (method.methodType() == QQmlJSMetaMethodType::Signal) { |
| 198 | function.isSignalHandler = true; |
| 199 | const auto arguments = method.parameters(); |
| 200 | for (qsizetype i = 0, end = arguments.size(); i < end; ++i) { |
| 201 | const auto &type = arguments[i].type(); |
| 202 | if (type.isNull()) { |
| 203 | diagnose(message: u"Cannot resolve the argument type %1."_s .arg( |
| 204 | a: arguments[i].typeName()), |
| 205 | type: QtDebugMsg, location: bindingLocation, error); |
| 206 | function.argumentTypes.append( |
| 207 | t: m_typeResolver->tracked( |
| 208 | type: m_typeResolver->globalType(type: m_typeResolver->varType()))); |
| 209 | } else { |
| 210 | function.argumentTypes.append( |
| 211 | t: m_typeResolver->tracked( |
| 212 | type: m_typeResolver->globalType(type))); |
| 213 | } |
| 214 | } |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | if (!function.isSignalHandler) { |
| 219 | diagnose(message: u"Could not find signal \"%1\"."_s .arg(a: *signalName), |
| 220 | type: QtWarningMsg, location: bindingLocation, error); |
| 221 | } |
| 222 | } |
| 223 | } else { |
| 224 | QString message = u"Could not find property \"%1\"."_s .arg(a: propertyName); |
| 225 | if (m_objectType->isNameDeferred(name: propertyName)) { |
| 226 | // If the property doesn't exist but the name is deferred, then |
| 227 | // it's deferred via the presence of immediate names. Those are |
| 228 | // most commonly used to enable generalized grouped properties. |
| 229 | message += u" You may want use ID-based grouped properties here." ; |
| 230 | } |
| 231 | |
| 232 | diagnose(message, type: QtWarningMsg, location: bindingLocation, error); |
| 233 | } |
| 234 | |
| 235 | QQmlJS::MemoryPool pool; |
| 236 | auto ast = astNode->asFunctionDefinition(); |
| 237 | if (!ast) { |
| 238 | QQmlJS::AST::Statement *stmt = astNode->statementCast(); |
| 239 | if (!stmt) { |
| 240 | Q_ASSERT(astNode->expressionCast()); |
| 241 | QQmlJS::AST::ExpressionNode *expr = astNode->expressionCast(); |
| 242 | stmt = new (&pool) QQmlJS::AST::ExpressionStatement(expr); |
| 243 | } |
| 244 | auto body = new (&pool) QQmlJS::AST::StatementList(stmt); |
| 245 | body = body->finish(); |
| 246 | |
| 247 | QString name = u"binding for "_s ; // #### |
| 248 | ast = new (&pool) QQmlJS::AST::FunctionDeclaration( |
| 249 | pool.newString(string: name), /*formals*/ nullptr, body); |
| 250 | ast->lbraceToken = astNode->firstSourceLocation(); |
| 251 | ast->functionToken = ast->lbraceToken; |
| 252 | ast->rbraceToken = astNode->lastSourceLocation(); |
| 253 | } |
| 254 | |
| 255 | populateSignature(context, ast, function: &function, error); |
| 256 | return function; |
| 257 | } |
| 258 | |
| 259 | QQmlJSCompilePass::Function QQmlJSFunctionInitializer::run( |
| 260 | const QV4::Compiler::Context *context, |
| 261 | const QString &functionName, QQmlJS::AST::Node *astNode, |
| 262 | QQmlJS::DiagnosticMessage *error) |
| 263 | { |
| 264 | Q_UNUSED(functionName); |
| 265 | |
| 266 | QQmlJSCompilePass::Function function; |
| 267 | function.qmlScope = m_scopeType; |
| 268 | |
| 269 | auto ast = astNode->asFunctionDefinition(); |
| 270 | Q_ASSERT(ast); |
| 271 | |
| 272 | populateSignature(context, ast, function: &function, error); |
| 273 | return function; |
| 274 | } |
| 275 | |
| 276 | QT_END_NAMESPACE |
| 277 | |