1// Copyright (C) 2022 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 "qqmljsmetatypes_p.h"
5#include "qqmljstyperesolver_p.h"
6
7#include "QtQml/private/qqmltranslation_p.h"
8
9QT_BEGIN_NAMESPACE
10
11/*!
12 \internal
13 A binding is valid when it has both a target (m_propertyName is set)
14 and some content set (m_bindingType != Invalid).
15*/
16bool QQmlJSMetaPropertyBinding::isValid() const
17{
18 return !m_propertyName.isEmpty() && bindingType() != QQmlSA::BindingType::Invalid;
19}
20
21QString QQmlJSMetaPropertyBinding::literalTypeName() const
22{
23 if (std::holds_alternative<Content::BoolLiteral>(v: m_bindingContent))
24 return QLatin1String("bool");
25 else if (std::holds_alternative<Content::NumberLiteral>(v: m_bindingContent))
26 return QLatin1String("double");
27 else if (std::holds_alternative<Content::StringLiteral>(v: m_bindingContent))
28 return QLatin1String("string");
29 else if (std::holds_alternative<Content::RegexpLiteral>(v: m_bindingContent))
30 return QLatin1String("regexp");
31 else if (std::holds_alternative<Content::Null>(v: m_bindingContent))
32 return QLatin1String("$internal$.std::nullptr_t");
33 return {};
34}
35
36bool QQmlJSMetaPropertyBinding::boolValue() const
37{
38 if (auto boolLit = std::get_if<Content::BoolLiteral>(ptr: &m_bindingContent))
39 return boolLit->value;
40 // warn
41 return false;
42}
43
44double QQmlJSMetaPropertyBinding::numberValue() const
45{
46 if (auto numberLit = std::get_if<Content::NumberLiteral>(ptr: &m_bindingContent))
47 return numberLit->value;
48 // warn
49 return 0;
50}
51
52QString QQmlJSMetaPropertyBinding::stringValue() const
53{
54 if (auto stringLiteral = std::get_if<Content::StringLiteral>(ptr: &m_bindingContent))
55 return stringLiteral->value;
56 // warn
57 return {};
58}
59
60QString QQmlJSMetaPropertyBinding::regExpValue() const
61{
62 if (auto regexpLiteral = std::get_if<Content::RegexpLiteral>(ptr: &m_bindingContent))
63 return regexpLiteral->value;
64 // warn
65 return {};
66}
67
68 /*!
69 * Extracts the information about translations from a binding.
70 * An additional context string is needed for text based translation (e.g. with qsTr())
71 * and can be obtained from the name of the qml file.
72 *
73 * \sa QQmlTranslation
74 */
75QQmlTranslation QQmlJSMetaPropertyBinding::translationDataValue(QString qmlFileNameForContext) const
76{
77 QQmlTranslation::Data data;
78 if (auto translation = std::get_if<Content::TranslationById>(ptr: &m_bindingContent)) {
79 data = QQmlTranslation::QsTrIdData(translation->id, translation->number);
80 } else if (auto translation = std::get_if<Content::TranslationString>(ptr: &m_bindingContent)) {
81 const QString context = translation->context.isEmpty()
82 ? QQmlTranslation::contextFromQmlFilename(qmlFilename: qmlFileNameForContext)
83 : translation->context;
84 data = QQmlTranslation::QsTrData(context, translation->text, translation->comment,
85 translation->number);
86 }
87 return QQmlTranslation(data);
88}
89
90/*!
91 \internal
92 Uses \a resolver to return the correct type for the stored literal
93 and a null scope pointer if the binding does not contain a literal
94 */
95QSharedPointer<const QQmlJSScope> QQmlJSMetaPropertyBinding::literalType(const QQmlJSTypeResolver *resolver) const
96{
97 Q_ASSERT(resolver);
98 switch (bindingType()) {
99 case BindingType::BoolLiteral:
100 return resolver->boolType();
101 case BindingType::NumberLiteral:
102 return resolver->typeForName(name: QLatin1String("double"));
103 case BindingType::Translation: // translations are strings
104 case BindingType::TranslationById:
105 case BindingType::StringLiteral:
106 return resolver->stringType();
107 case BindingType::RegExpLiteral:
108 return resolver->typeForName(name: QLatin1String("regexp"));
109 case BindingType::Null:
110 return resolver->nullType();
111 case BindingType::Invalid:
112 case BindingType::Script:
113 case BindingType::Object:
114 case BindingType::Interceptor:
115 case BindingType::ValueSource:
116 case BindingType::AttachedProperty:
117 case BindingType::GroupProperty:
118 return {};
119 }
120 Q_UNREACHABLE_RETURN({});
121}
122
123QQmlJSMetaPropertyBinding::QQmlJSMetaPropertyBinding() = default;
124
125QT_END_NAMESPACE
126

source code of qtdeclarative/src/qmlcompiler/qqmljsmetatypes.cpp