Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlpropertyvalidator.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
6
7#include <private/qqmlcustomparser_p.h>
8#include <private/qqmlglobal_p.h>
9#include <private/qqmlirbuilder_p.h>
10#include <private/qqmlpropertycachecreator_p.h>
11#include <private/qqmlpropertyresolver_p.h>
12#include <private/qqmlstringconverters_p.h>
13#include <private/qqmlsignalnames_p.h>
14
15#include <QtCore/qdatetime.h>
16
18
19static bool isPrimitiveType(QMetaType metaType)
20{
21 switch (metaType.id()) {
22#define HANDLE_PRIMITIVE(Type, id, T)
23 case QMetaType::Type:
24QT_FOR_EACH_STATIC_PRIMITIVE_TYPE(HANDLE_PRIMITIVE);
25#undef HANDLE_PRIMITIVE
26 return true;
27 default:
28 return false;
29 }
30}
31
32QQmlPropertyValidator::QQmlPropertyValidator(
33 QQmlTypeLoader *typeLoader, const QQmlImports *imports,
34 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit)
35 : m_typeLoader(typeLoader)
36 , compilationUnit(compilationUnit)
37 , imports(imports)
38 , qmlUnit(compilationUnit->unitData())
39 , propertyCaches(compilationUnit->propertyCaches)
40 , bindingPropertyDataPerObject(&compilationUnit->bindingPropertyDataPerObject)
41{
42 bindingPropertyDataPerObject->resize(compilationUnit->objectCount());
43}
44
45QList<QQmlError> QQmlPropertyValidator::validate()
46{
47 return validateObject(/*root object*/0, /*instantiatingBinding*/nullptr);
48}
49
51
53{
54 bool operator()(quint32 name, const QV4::CompiledData::Binding *binding) const
55 {
56 return name < binding->propertyNameIndex;
57 }
58 bool operator()(const QV4::CompiledData::Binding *binding, quint32 name) const
59 {
60 return binding->propertyNameIndex < name;
61 }
62 bool operator()(const QV4::CompiledData::Binding *lhs, const QV4::CompiledData::Binding *rhs) const
63 {
64 return lhs->propertyNameIndex < rhs->propertyNameIndex;
65 }
66};
67
68QList<QQmlError> QQmlPropertyValidator::validateObject(
69 int objectIndex, const QV4::CompiledData::Binding *instantiatingBinding,
70 bool populatingValueTypeGroupProperty,
71 QQmlPropertyResolver::RevisionCheck checkRevision) const
72{
73 // Implicit component wrappers (>= objectCount) have no CU object.
74 // Validate the child object instead.
75 if (objectIndex >= compilationUnit->objectCount()) {
76 return validateObject(
77 compilationUnit->resolvedIndex(objectIndex), instantiatingBinding,
78 populatingValueTypeGroupProperty, checkRevision);
79 }
80
81 const QV4::CompiledData::Object *obj = compilationUnit->objectAt(objectIndex);
82 for (auto it = obj->inlineComponentsBegin(); it != obj->inlineComponentsEnd(); ++it) {
83 const auto errors = validateObject(it->objectIndex, /* instantiatingBinding*/ nullptr);
84 if (!errors.isEmpty())
85 return errors;
86 }
87
88 if (obj->hasFlag(QV4::CompiledData::Object::IsComponent)
89 && !obj->hasFlag(QV4::CompiledData::Object::IsInlineComponentRoot)) {
90 Q_ASSERT(obj->nBindings == 1);
91 const QV4::CompiledData::Binding *componentBinding = obj->bindingTable();
92 Q_ASSERT(componentBinding->type() == QV4::CompiledData::Binding::Type_Object);
93 return validateObject(componentBinding->value.objectIndex, componentBinding);
94 }
95
96 QQmlPropertyCache::ConstPtr propertyCache = propertyCaches.at(objectIndex);
97 if (!propertyCache)
98 return QList<QQmlError>();
99
100 QQmlCustomParser *customParser = nullptr;
101 if (auto typeRef = resolvedType(obj->inheritedTypeNameIndex)) {
102
103 // This binding instantiates a separate object. The separate object can have an ID and its
104 // own group properties even if it's then assigned to a value type, for example a 'var', or
105 // anything with an invokable ctor taking a QObject*.
106 populatingValueTypeGroupProperty = false;
107
108 const auto type = typeRef->type();
109 if (type.isValid())
110 customParser = type.customParser();
111 }
112
113 QList<const QV4::CompiledData::Binding*> customBindings;
114
115 // Collect group properties first for sanity checking
116 // vector values are sorted by property name string index.
117 GroupPropertyVector groupProperties;
118 const QV4::CompiledData::Binding *binding = obj->bindingTable();
119 for (quint32 i = 0; i < obj->nBindings; ++i, ++binding) {
120 if (!binding->isGroupProperty())
121 continue;
122
123 if (binding->hasFlag(QV4::CompiledData::Binding::IsOnAssignment))
124 continue;
125
126 if (populatingValueTypeGroupProperty) {
127 return recordError(binding->location, tr("Property assignment expected"));
128 }
129
130 GroupPropertyVector::const_iterator pos = std::lower_bound(groupProperties.constBegin(), groupProperties.constEnd(), binding->propertyNameIndex, BindingFinder());
131 groupProperties.insert(pos, binding);
132 }
133
134 QQmlPropertyResolver propertyResolver(propertyCache);
135
136 QString defaultPropertyName;
137 const QQmlPropertyData *defaultProperty = nullptr;
138 if (obj->indexOfDefaultPropertyOrAlias != -1) {
139 const QQmlPropertyCache *cache = propertyCache->parent().data();
140 defaultPropertyName = cache->defaultPropertyName();
141 defaultProperty = cache->defaultProperty();
142 } else {
143 defaultPropertyName = propertyCache->defaultPropertyName();
144 defaultProperty = propertyCache->defaultProperty();
145 }
146
147 QV4::CompiledData::BindingPropertyData collectedBindingPropertyData(obj->nBindings);
148
149 binding = obj->bindingTable();
150 for (quint32 i = 0; i < obj->nBindings; ++i, ++binding) {
151 QString name = stringAt(binding->propertyNameIndex);
152 const QV4::CompiledData::Binding::Type bindingType = binding->type();
153 const QV4::CompiledData::Binding::Flags bindingFlags = binding->flags();
154
155 if (customParser) {
156 if (bindingType == QV4::CompiledData::Binding::Type_AttachedProperty) {
157 if (customParser->flags() & QQmlCustomParser::AcceptsAttachedProperties) {
158 customBindings << binding;
159 continue;
160 }
161 } else if (QQmlSignalNames::isHandlerName(name)
162 && !(customParser->flags() & QQmlCustomParser::AcceptsSignalHandlers)) {
163 customBindings << binding;
164 continue;
165 }
166 }
167
168 bool bindingToDefaultProperty = false;
169 bool isGroupProperty = instantiatingBinding
170 && instantiatingBinding->type() == QV4::CompiledData::Binding::Type_GroupProperty;
171
172 bool notInRevision = false;
173 const QQmlPropertyData *pd = nullptr;
174 if (!name.isEmpty()) {
175 if (bindingFlags & QV4::CompiledData::Binding::IsSignalHandlerExpression
176 || bindingFlags & QV4::CompiledData::Binding::IsSignalHandlerObject) {
177 pd = propertyResolver.signal(name, &notInRevision, checkRevision);
178 } else {
179 pd = propertyResolver.property(name, &notInRevision, checkRevision);
180 }
181
182 if (notInRevision) {
183 QString typeName = stringAt(obj->inheritedTypeNameIndex);
184 if (auto *objectType = resolvedType(obj->inheritedTypeNameIndex)) {
185 const auto type = objectType->type();
186 if (type.isValid()) {
187 const auto version = objectType->version();
188 return recordError(binding->location,
189 tr("\"%1.%2\" is not available in %3 %4.%5.")
190 .arg(typeName).arg(name).arg(type.module())
191 .arg(version.majorVersion())
192 .arg(version.minorVersion()));
193 }
194 } else {
195 return recordError(binding->location, tr("\"%1.%2\" is not available due to component versioning.").arg(typeName).arg(name));
196 }
197 }
198 } else {
199 if (isGroupProperty)
200 return recordError(binding->location, tr("Cannot assign a value directly to a grouped property"));
201
202 pd = defaultProperty;
203 name = defaultPropertyName;
204 bindingToDefaultProperty = true;
205 }
206
207 if (pd)
208 collectedBindingPropertyData[i] = pd;
209
210 if (name.constData()->isUpper() && !binding->isAttachedProperty()) {
211 QQmlType type;
212 QQmlImportNamespace *typeNamespace = nullptr;
213 imports->resolveType(
214 m_typeLoader, stringAt(binding->propertyNameIndex),
215 &type, nullptr, &typeNamespace);
216 if (typeNamespace)
217 return recordError(binding->location, tr("Invalid use of namespace"));
218 return recordError(binding->location, tr("Invalid attached object assignment"));
219 }
220
221 if (bindingType >= QV4::CompiledData::Binding::Type_Object
222 && (pd || binding->isAttachedProperty() || binding->isGroupProperty())) {
223 const bool populatingValueTypeGroupProperty
224 = pd
225 && QQmlMetaType::metaObjectForValueType(pd->propType())
226 && !binding->hasFlag(QV4::CompiledData::Binding::IsOnAssignment);
227
228 // As this is a sub-object, its properties are qualified. We can ignore revisions.
229 const QList<QQmlError> subObjectValidatorErrors = validateObject(
230 binding->value.objectIndex, binding, populatingValueTypeGroupProperty,
231 QQmlPropertyResolver::IgnoreRevision);
232
233 if (!subObjectValidatorErrors.isEmpty())
234 return subObjectValidatorErrors;
235 }
236
237 // Signal handlers were resolved and checked earlier in the signal handler conversion pass.
238 if (binding->flags() & (QV4::CompiledData::Binding::IsSignalHandlerExpression
239 | QV4::CompiledData::Binding::IsSignalHandlerObject
240 | QV4::CompiledData::Binding::IsPropertyObserver)) {
241 continue;
242 }
243
244 if ((pd && bindingType == QV4::CompiledData::Binding::Type_AttachedProperty)
245 || (!pd && bindingType == QV4::CompiledData::Binding::Type_GroupProperty)) {
246 if (instantiatingBinding && (instantiatingBinding->isAttachedProperty()
247 || instantiatingBinding->isGroupProperty())) {
248 return recordError(
249 binding->location, tr("%1 properties cannot be used here")
250 .arg(bindingType == QV4::CompiledData::Binding::Type_AttachedProperty
251 ? QStringLiteral("Attached")
252 : QStringLiteral("Group")));
253 }
254 continue;
255 } else if (bindingType == QV4::CompiledData::Binding::Type_AttachedProperty) {
256 continue;
257 }
258
259 if (pd) {
260 GroupPropertyVector::const_iterator assignedGroupProperty = std::lower_bound(groupProperties.constBegin(), groupProperties.constEnd(), binding->propertyNameIndex, BindingFinder());
261 const bool assigningToGroupProperty = assignedGroupProperty != groupProperties.constEnd() && !(binding->propertyNameIndex < (*assignedGroupProperty)->propertyNameIndex);
262
263 if (!pd->isWritable()
264 && !pd->isQList()
265 && !binding->isGroupProperty()
266 && !(bindingFlags & QV4::CompiledData::Binding::InitializerForReadOnlyDeclaration)
267 ) {
268
269 if (assigningToGroupProperty && bindingType < QV4::CompiledData::Binding::Type_Object)
270 return recordError(binding->valueLocation, tr("Cannot assign a value directly to a grouped property"));
271 return recordError(binding->valueLocation, tr("Invalid property assignment: \"%1\" is a read-only property").arg(name));
272 }
273
274 if (!pd->isQList() && (bindingFlags & QV4::CompiledData::Binding::IsListItem)) {
275 QString error;
276 if (pd->propType() == QMetaType::fromType<QQmlScriptString>())
277 error = tr( "Cannot assign multiple values to a script property");
278 else
279 error = tr( "Cannot assign multiple values to a singular property");
280 return recordError(binding->valueLocation, error);
281 }
282
283 if (!bindingToDefaultProperty
284 && !binding->isGroupProperty()
285 && !(bindingFlags & QV4::CompiledData::Binding::IsOnAssignment)
286 && assigningToGroupProperty) {
287 QV4::CompiledData::Location loc = binding->valueLocation;
288 if (loc < (*assignedGroupProperty)->valueLocation)
289 loc = (*assignedGroupProperty)->valueLocation;
290
291 if (pd && QQmlMetaType::isValueType(pd->propType()))
292 return recordError(loc, tr("Property has already been assigned a value"));
293 return recordError(loc, tr("Cannot assign a value directly to a grouped property"));
294 }
295
296 if (bindingType < QV4::CompiledData::Binding::Type_Script) {
297 QQmlError bindingError = validateLiteralBinding(propertyCache, pd, binding);
298 if (bindingError.isValid())
299 return recordError(bindingError);
300 } else if (bindingType == QV4::CompiledData::Binding::Type_Object) {
301 QQmlError bindingError = validateObjectBinding(pd, name, binding);
302 if (bindingError.isValid())
303 return recordError(bindingError);
304 } else if (binding->isGroupProperty()) {
305 if (QQmlMetaType::isValueType(pd->propType())) {
306 if (QQmlMetaType::metaObjectForValueType(pd->propType())) {
307 if (!pd->isWritable()) {
308 return recordError(binding->location, tr("Invalid property assignment: \"%1\" is a read-only property").arg(name));
309 }
310 } else {
311 return recordError(binding->location, tr("Invalid grouped property access"));
312 }
313 } else {
314 const QMetaType type = pd->propType();
315 if (isPrimitiveType(type)) {
316 return recordError(
317 binding->location,
318 tr("Invalid grouped property access: Property \"%1\" with primitive type \"%2\".")
319 .arg(name, QString::fromUtf8(type.name()))
320 );
321 }
322
323 if (!QQmlMetaType::propertyCacheForType(type)) {
324 auto mo = type.metaObject();
325 if (!mo) {
326 return recordError(binding->location,
327 tr("Invalid grouped property access: Property \"%1\" with type \"%2\", which is neither a value nor an object type")
328 .arg(name, QString::fromUtf8(type.name()))
329 );
330 }
331 if (QMetaObjectPrivate::get(mo)->flags & DynamicMetaObject) {
332 return recordError(binding->location,
333 tr("Unsupported grouped property access: Property \"%1\" with type \"%2\" has a dynamic meta-object.")
334 .arg(name, QString::fromUtf8(type.name()))
335 );
336 }
337 // fall through, this is okay
338 }
339 }
340 }
341 } else {
342 if (customParser) {
343 customBindings << binding;
344 continue;
345 }
346 if (bindingToDefaultProperty) {
347 return recordError(binding->location, tr("Cannot assign to non-existent default property"));
348 } else {
349 return recordError(binding->location, tr("Cannot assign to non-existent property \"%1\"").arg(name));
350 }
351 }
352 }
353
354 if (obj->idNameIndex) {
355 if (populatingValueTypeGroupProperty)
356 return recordError(obj->locationOfIdProperty, tr("Invalid use of id property with a value type"));
357
358 bool notInRevision = false;
359 collectedBindingPropertyData
360 << propertyResolver.property(QStringLiteral("id"), &notInRevision, checkRevision);
361 }
362
363 if (customParser && !customBindings.isEmpty()) {
364 customParser->clearErrors();
365 customParser->validator = this;
366 customParser->imports = imports;
367 customParser->verifyBindings(compilationUnit, customBindings);
368 customParser->validator = nullptr;
369 customParser->imports = (QQmlImports*)nullptr;
370 QList<QQmlError> parserErrors = customParser->errors();
371 if (!parserErrors.isEmpty())
372 return parserErrors;
373 }
374
375 (*bindingPropertyDataPerObject)[objectIndex] = collectedBindingPropertyData;
376
377 QList<QQmlError> noError;
378 return noError;
379}
380
381QQmlError QQmlPropertyValidator::validateLiteralBinding(
382 const QQmlPropertyCache::ConstPtr &propertyCache, const QQmlPropertyData *property,
383 const QV4::CompiledData::Binding *binding) const
384{
385 if (property->isQList()) {
386 return qQmlCompileError(binding->valueLocation, tr("Cannot assign primitives to lists"));
387 }
388
389 QQmlError noError;
390
391 if (property->isEnum()) {
392 if (binding->hasFlag(QV4::CompiledData::Binding::IsResolvedEnum))
393 return noError;
394
395 // TODO: For historical reasons you can assign any number to an enum property alias
396 // This can be fixed with an opt-out mechanism, for example a pragma.
397 if (property->isAlias() && binding->isNumberBinding())
398 return noError;
399
400 QString value = compilationUnit->bindingValueAsString(binding);
401 QMetaProperty p = propertyCache->firstCppMetaObject()->property(property->coreIndex());
402 bool ok;
403 if (p.isFlagType()) {
404 p.enumerator().keysToValue(value.toUtf8().constData(), &ok);
405 } else
406 p.enumerator().keyToValue(value.toUtf8().constData(), &ok);
407
408 if (!ok) {
409 return qQmlCompileError(binding->valueLocation, tr("Invalid property assignment: unknown enumeration"));
410 }
411 return noError;
412 }
413
414 auto warnOrError = [&](const QString &error) {
415 return qQmlCompileError(binding->valueLocation, error);
416 };
417
418 const QV4::CompiledData::Binding::Type bindingType = binding->type();
419 const auto isStringBinding = [&]() -> bool {
420 // validateLiteralBinding is not supposed to be used on scripts
421 Q_ASSERT(bindingType != QV4::CompiledData::Binding::Type_Script);
422 return bindingType == QV4::CompiledData::Binding::Type_String;
423 };
424
425 switch (property->propType().id()) {
426 case QMetaType::QVariant:
427 break;
428 case QMetaType::QString: {
429 if (!binding->evaluatesToString()) {
430 return warnOrError(tr("Invalid property assignment: string expected"));
431 }
432 }
433 break;
434 case QMetaType::QStringList: {
435 if (!binding->evaluatesToString()) {
436 return warnOrError(tr("Invalid property assignment: string or string list expected"));
437 }
438 }
439 break;
440 case QMetaType::QByteArray: {
441 if (bindingType != QV4::CompiledData::Binding::Type_String)
442 return warnOrError(tr("Invalid property assignment: byte array expected"));
443 }
444 break;
445 case QMetaType::QUrl: {
446 if (bindingType != QV4::CompiledData::Binding::Type_String)
447 return warnOrError(tr("Invalid property assignment: url expected"));
448 }
449 break;
450 case QMetaType::UInt: {
451 if (bindingType == QV4::CompiledData::Binding::Type_Number) {
452 double d = compilationUnit->bindingValueAsNumber(binding);
453 if (double(uint(d)) == d)
454 return noError;
455 }
456 return warnOrError(tr("Invalid property assignment: unsigned int expected"));
457 }
458 break;
459 case QMetaType::Int: {
460 if (bindingType == QV4::CompiledData::Binding::Type_Number) {
461 double d = compilationUnit->bindingValueAsNumber(binding);
462 if (double(int(d)) == d)
463 return noError;
464 }
465 return warnOrError(tr("Invalid property assignment: int expected"));
466 }
467 break;
468 case QMetaType::LongLong:
469 case QMetaType::ULongLong:
470 case QMetaType::Long:
471 case QMetaType::ULong:
472 case QMetaType::Float:
473 case QMetaType::Double: {
474 if (bindingType != QV4::CompiledData::Binding::Type_Number) {
475 return warnOrError(tr("Invalid property assignment: number expected"));
476 }
477 }
478 break;
479 case QMetaType::QColor: {
480 bool ok = false;
481 if (isStringBinding())
482 QQmlStringConverters::rgbaFromString(compilationUnit->bindingValueAsString(binding), &ok);
483 if (!ok) {
484 return warnOrError(tr("Invalid property assignment: color expected"));
485 }
486 }
487 break;
488#if QT_CONFIG(datestring)
489 case QMetaType::QDate: {
490 bool ok = false;
491 if (isStringBinding())
492 QQmlStringConverters::dateFromString(compilationUnit->bindingValueAsString(binding), &ok);
493 if (!ok) {
494 return warnOrError(tr("Invalid property assignment: date expected"));
495 }
496 }
497 break;
498 case QMetaType::QTime: {
499 bool ok = false;
500 if (isStringBinding())
501 QQmlStringConverters::timeFromString(compilationUnit->bindingValueAsString(binding), &ok);
502 if (!ok) {
503 return warnOrError(tr("Invalid property assignment: time expected"));
504 }
505 }
506 break;
507 case QMetaType::QDateTime: {
508 bool ok = false;
509 if (isStringBinding())
510 QQmlStringConverters::dateTimeFromString(compilationUnit->bindingValueAsString(binding), &ok);
511 if (!ok) {
512 return warnOrError(tr("Invalid property assignment: datetime expected"));
513 }
514 }
515 break;
516#endif // datestring
517 case QMetaType::QPoint: {
518 bool ok = false;
519 if (isStringBinding())
520 QQmlStringConverters::pointFFromString(compilationUnit->bindingValueAsString(binding), &ok);
521 if (!ok) {
522 return warnOrError(tr("Invalid property assignment: point expected"));
523 }
524 }
525 break;
526 case QMetaType::QPointF: {
527 bool ok = false;
528 if (isStringBinding())
529 QQmlStringConverters::pointFFromString(compilationUnit->bindingValueAsString(binding), &ok);
530 if (!ok) {
531 return warnOrError(tr("Invalid property assignment: point expected"));
532 }
533 }
534 break;
535 case QMetaType::QSize: {
536 bool ok = false;
537 if (isStringBinding())
538 QQmlStringConverters::sizeFFromString(compilationUnit->bindingValueAsString(binding), &ok);
539 if (!ok) {
540 return warnOrError(tr("Invalid property assignment: size expected"));
541 }
542 }
543 break;
544 case QMetaType::QSizeF: {
545 bool ok = false;
546 if (isStringBinding())
547 QQmlStringConverters::sizeFFromString(compilationUnit->bindingValueAsString(binding), &ok);
548 if (!ok) {
549 return warnOrError(tr("Invalid property assignment: size expected"));
550 }
551 }
552 break;
553 case QMetaType::QRect: {
554 bool ok = false;
555 if (isStringBinding())
556 QQmlStringConverters::rectFFromString(compilationUnit->bindingValueAsString(binding), &ok);
557 if (!ok) {
558 return warnOrError(tr("Invalid property assignment: rect expected"));
559 }
560 }
561 break;
562 case QMetaType::QRectF: {
563 bool ok = false;
564 if (isStringBinding())
565 QQmlStringConverters::rectFFromString(compilationUnit->bindingValueAsString(binding), &ok);
566 if (!ok) {
567 return warnOrError(tr("Invalid property assignment: point expected"));
568 }
569 }
570 break;
571 case QMetaType::Bool: {
572 if (bindingType != QV4::CompiledData::Binding::Type_Boolean) {
573 return warnOrError(tr("Invalid property assignment: boolean expected"));
574 }
575 }
576 break;
577 case QMetaType::QVector2D:
578 case QMetaType::QVector3D:
579 case QMetaType::QVector4D:
580 case QMetaType::QQuaternion: {
581 auto typeName = [&]() {
582 switch (property->propType().id()) {
583 case QMetaType::QVector2D: return QStringLiteral("2D vector");
584 case QMetaType::QVector3D: return QStringLiteral("3D vector");
585 case QMetaType::QVector4D: return QStringLiteral("4D vector");
586 case QMetaType::QQuaternion: return QStringLiteral("quaternion");
587 default: return QString();
588 }
589 };
590 const QVariant result = QQmlValueTypeProvider::createValueType(
591 compilationUnit->bindingValueAsString(binding),
592 property->propType());
593 if (!result.isValid()) {
594 return warnOrError(tr("Invalid property assignment: %1 expected")
595 .arg(typeName()));
596 }
597 }
598 break;
599 case QMetaType::QRegularExpression:
600 return warnOrError(tr("Invalid property assignment: regular expression expected; use /pattern/ syntax"));
601 default: {
602 // generate single literal value assignment to a list property if required
603 if (property->propType() == QMetaType::fromType<QList<qreal> >()) {
604 if (bindingType != QV4::CompiledData::Binding::Type_Number) {
605 return warnOrError(tr("Invalid property assignment: number or array of numbers expected"));
606 }
607 break;
608 } else if (property->propType() == QMetaType::fromType<QList<int> >()) {
609 bool ok = (bindingType == QV4::CompiledData::Binding::Type_Number);
610 if (ok) {
611 double n = compilationUnit->bindingValueAsNumber(binding);
612 if (double(int(n)) != n)
613 ok = false;
614 }
615 if (!ok)
616 return warnOrError(tr("Invalid property assignment: int or array of ints expected"));
617 break;
618 } else if (property->propType() == QMetaType::fromType<QList<bool> >()) {
619 if (bindingType != QV4::CompiledData::Binding::Type_Boolean) {
620 return warnOrError(tr("Invalid property assignment: bool or array of bools expected"));
621 }
622 break;
623 } else if (property->propType() == QMetaType::fromType<QList<QUrl> >()) {
624 if (bindingType != QV4::CompiledData::Binding::Type_String) {
625 return warnOrError(tr("Invalid property assignment: url or array of urls expected"));
626 }
627 break;
628 } else if (property->propType() == QMetaType::fromType<QList<QString> >()) {
629 if (!binding->evaluatesToString()) {
630 return warnOrError(tr("Invalid property assignment: string or array of strings expected"));
631 }
632 break;
633 } else if (property->propType() == QMetaType::fromType<QJSValue>()) {
634 break;
635 } else if (property->propType() == QMetaType::fromType<QQmlScriptString>()) {
636 break;
637 } else if (property->isQObject()
638 && bindingType == QV4::CompiledData::Binding::Type_Null) {
639 break;
640 } else if (QQmlMetaType::qmlType(property->propType()).canConstructValueType()) {
641 break;
642 }
643
644 return warnOrError(tr("Invalid property assignment: unsupported type \"%1\"").arg(QLatin1StringView(property->propType().name())));
645 }
646 break;
647 }
648 return noError;
649}
650
651/*!
652 Returns true if from can be assigned to a (QObject) property of type
653 to.
654*/
655bool QQmlPropertyValidator::canCoerce(QMetaType to, QQmlPropertyCache::ConstPtr fromMo) const
656{
657 if (QQmlMetaType::canConvert(fromMo, to))
658 return true;
659
660 // if we have an inline component from the current file,
661 // it is not properly registered at this point, as registration
662 // only occurs after the whole file has been validated
663 // Therefore we need to check the ICs here
664 for (const auto &icDatum : std::as_const(compilationUnit->inlineComponentData)) {
665 if (icDatum.qmlType.typeId() != to)
666 continue;
667
668 const auto toMo = compilationUnit->propertyCaches.at(icDatum.objectIndex);
669 for (QQmlPropertyCache::ConstPtr parent = fromMo; parent; parent = parent->parent()) {
670 if (parent == toMo)
671 return true;
672 }
673 return false;
674 }
675
676 return false;
677}
678
679QList<QQmlError> QQmlPropertyValidator::recordError(const QV4::CompiledData::Location &location, const QString &description) const
680{
681 QList<QQmlError> errors;
682 errors.append(qQmlCompileError(location, description));
683 return errors;
684}
685
686QList<QQmlError> QQmlPropertyValidator::recordError(const QQmlError &error) const
687{
688 QList<QQmlError> errors;
689 errors.append(error);
690 return errors;
691}
692
693QQmlError QQmlPropertyValidator::validateObjectBinding(const QQmlPropertyData *property, const QString &propertyName, const QV4::CompiledData::Binding *binding) const
694{
695 QQmlError noError;
696
697 if (binding->hasFlag(QV4::CompiledData::Binding::IsOnAssignment)) {
698 Q_ASSERT(binding->type() == QV4::CompiledData::Binding::Type_Object);
699
700 bool isValueSource = false;
701 bool isPropertyInterceptor = false;
702
703 const QV4::CompiledData::Object *targetObject = compilationUnit->objectAt(binding->value.objectIndex);
704 if (auto *typeRef = resolvedType(targetObject->inheritedTypeNameIndex)) {
705 QQmlPropertyCache::ConstPtr cache = typeRef->createPropertyCache();
706 const QMetaObject *mo = cache ? cache->firstCppMetaObject() : nullptr;
707 QQmlType qmlType;
708 while (mo && !qmlType.isValid()) {
709 qmlType = QQmlMetaType::qmlType(mo);
710 mo = mo->superClass();
711 }
712
713 isValueSource = qmlType.propertyValueSourceCast() != -1;
714 isPropertyInterceptor = qmlType.propertyValueInterceptorCast() != -1;
715 }
716
717 if (!isValueSource && !isPropertyInterceptor) {
718 return qQmlCompileError(binding->valueLocation, tr("\"%1\" cannot operate on \"%2\"").arg(stringAt(targetObject->inheritedTypeNameIndex)).arg(propertyName));
719 }
720
721 return noError;
722 }
723
724 const QMetaType propType = property->propType();
725 const auto rhsType = [&]() {
726 return stringAt(compilationUnit->objectAt(binding->value.objectIndex)
727 ->inheritedTypeNameIndex);
728 };
729
730 if (QQmlMetaType::isInterface(propType)) {
731 // Can only check at instantiation time if the created sub-object successfully casts to the
732 // target interface.
733 return noError;
734 } else if (propType == QMetaType::fromType<QVariant>()
735 || propType == QMetaType::fromType<QJSValue>()) {
736 // We can convert everything to QVariant :)
737 return noError;
738 } else if (property->isQList()) {
739 const QMetaType listType = QQmlMetaType::listValueType(property->propType());
740 if (!QQmlMetaType::isInterface(listType)) {
741 QQmlPropertyCache::ConstPtr source = propertyCaches.at(binding->value.objectIndex);
742
743 if (const int wrapper = compilationUnit->implicitComponentForObject(
744 binding->value.objectIndex); wrapper != -1) {
745 // If the child is wrapped in an implicit component, use the wrapper for coercion.
746 source = propertyCaches.at(wrapper);
747 }
748
749 if (!canCoerce(listType, source)) {
750 const QString expectedTypeName = QString::fromUtf8(listType.name()).remove(QLatin1Char('*'));
751 return qQmlCompileError(binding->valueLocation,
752 tr("Cannot assign object of type \"%1\" to list property \"%2\"; expected \"%3\"")
753 .arg(source->className(), propertyName, expectedTypeName));
754 }
755 }
756 return noError;
757 } else if (binding->hasFlag(QV4::CompiledData::Binding::IsSignalHandlerObject)
758 && property->isFunction()) {
759 return noError;
760 } else if (isPrimitiveType(propType)) {
761 auto typeName = QString::fromUtf8(QMetaType(propType).name());
762 return qQmlCompileError(binding->location, tr("Cannot assign value of type \"%1\" to property \"%2\", expecting \"%3\"")
763 .arg(rhsType())
764 .arg(propertyName)
765 .arg(typeName));
766 } else if (propType == QMetaType::fromType<QQmlScriptString>()) {
767 return qQmlCompileError(binding->valueLocation, tr("Invalid property assignment: script expected"));
768 } else if (!QQmlMetaType::isValueType(property->propType())) {
769 QQmlPropertyCache::ConstPtr propertyMetaObject;
770
771 // if we have an inline component from the current file,
772 // it is not properly registered at this point, as registration
773 // only occurs after the whole file has been validated
774 // Therefore we need to check the ICs here
775 for (const auto &icDatum: std::as_const(compilationUnit->inlineComponentData)) {
776 if (icDatum.qmlType.typeId() == property->propType()) {
777 propertyMetaObject = compilationUnit->propertyCaches.at(icDatum.objectIndex);
778 break;
779 }
780 }
781
782 if (!propertyMetaObject) {
783 // We want to use the raw metaObject here as the raw metaobject is the
784 // actual property type before we applied any extensions that might
785 // effect the properties on the type, but don't effect assignability
786 // Not passing a version ensures that we get the raw metaObject.
787 propertyMetaObject = QQmlMetaType::rawPropertyCacheForType(propType);
788 }
789
790 if (propertyMetaObject) {
791 // Will be true if the assigned type inherits propertyMetaObject
792 // Determine isAssignable value
793 bool isAssignable = false;
794 QQmlPropertyCache::ConstPtr c = propertyCaches.at(binding->value.objectIndex);
795
796 if (const int wrapper = compilationUnit->implicitComponentForObject(
797 binding->value.objectIndex); wrapper != -1) {
798 // If the child is wrapped in an implicit component, use the wrapper.
799 c = propertyCaches.at(wrapper);
800 }
801
802 while (c && !isAssignable) {
803 isAssignable |= c == propertyMetaObject;
804 c = c->parent();
805 }
806
807 if (!isAssignable) {
808 return qQmlCompileError(binding->valueLocation, tr("Cannot assign object of type \"%1\" to property of type \"%2\" as the former is neither the same as the latter nor a sub-class of it.")
809 .arg(rhsType()).arg(QLatin1String(property->propType().name())));
810 }
811 } else {
812 return qQmlCompileError(binding->valueLocation, tr("Cannot assign to property of unknown type \"%1\".")
813 .arg(QLatin1String(property->propType().name())));
814 }
815
816 }
817 return noError;
818}
819
820QT_END_NAMESPACE
Combined button and popup list for selecting options.
QVarLengthArray< const QV4::CompiledData::Binding *, 8 > GroupPropertyVector
static QT_BEGIN_NAMESPACE bool isPrimitiveType(QMetaType metaType)
bool operator()(quint32 name, const QV4::CompiledData::Binding *binding) const
bool operator()(const QV4::CompiledData::Binding *binding, quint32 name) const
bool operator()(const QV4::CompiledData::Binding *lhs, const QV4::CompiledData::Binding *rhs) const