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
moc.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// Copyright (C) 2019 Olivier Goffart <ogoffart@woboq.com>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
4
5#include "moc.h"
6#include "generator.h"
7#include "qdatetime.h"
8#include "utils.h"
10#include <QtCore/qfile.h>
11#include <QtCore/qfileinfo.h>
12#include <QtCore/qdir.h>
13#include <QtCore/qjsondocument.h>
14
15// for normalizeTypeInternal
16#include <private/qmetaobject_p.h>
17#include <private/qmetaobject_moc_p.h>
18#include <private/qduplicatetracker_p.h>
19
20QT_BEGIN_NAMESPACE
21
22using namespace Qt::StringLiterals;
23
24// only moc needs this function
25static QByteArray normalizeType(const QByteArray &ba)
26{
27 return ba.size() ? normalizeTypeInternal(ba.constBegin(), ba.constEnd()) : ba;
28}
29
30const QByteArray &Moc::toFullyQualified(const QByteArray &name) const noexcept
31{
32 if (auto it = knownQObjectClasses.find(name); it != knownQObjectClasses.end())
33 return it.value();
34 if (auto it = knownGadgets.find(name); it != knownGadgets.end())
35 return it.value();
36 return name;
37}
38
40{
41 // figure out whether this is a class declaration, or only a
42 // forward or variable declaration.
43 int i = 0;
44 Token token;
45 do {
46 token = lookup(i++);
47 if (token == COLON || token == LBRACE)
48 break;
49 if (token == SEMIC || token == RANGLE)
50 return false;
51 } while (token);
52
53 // support attributes like "class [[deprecated]]] name"
55
56 if (!test(IDENTIFIER)) // typedef struct { ... }
57 return false;
58 QByteArray name = lexem();
59
60 // support "class IDENT name" and "class IDENT(IDENT) name"
61 // also support "class IDENT name (final|sealed|Q_DECL_FINAL)"
62 if (test(LPAREN)) {
63 until(RPAREN);
64 if (!test(IDENTIFIER))
65 return false;
66 name = lexem();
67 } else if (test(IDENTIFIER)) {
68 const QByteArrayView lex = lexemView();
69 if (lex != "final" && lex != "sealed" && lex != "Q_DECL_FINAL")
70 name = lexem();
71 else
72 def->isFinal = true;
73 }
74
75 def->qualified += name;
76 while (test(SCOPE)) {
77 def->qualified += lexemView();
78 if (test(IDENTIFIER)) {
79 name = lexem();
80 def->qualified += name;
81 }
82 }
83 def->classname = name;
84 def->lineNumber = symbol().lineNum;
85
86 if (test(IDENTIFIER)) {
87 const QByteArrayView lex = lexemView();
88 if (lex != "final" && lex != "sealed" && lex != "Q_DECL_FINAL")
89 return false;
90 else
91 def->isFinal = true;
92 }
93
94 if (test(COLON)) {
95 do {
96 test(VIRTUAL);
98 if (test(PRIVATE))
99 access = FunctionDef::Private;
100 else if (test(PROTECTED))
101 access = FunctionDef::Protected;
102 else
103 test(PUBLIC);
104 test(VIRTUAL);
105 const Type type = parseType();
106 // ignore the 'class Foo : BAR(Baz)' case
107 if (test(LPAREN)) {
108 until(RPAREN);
109 } else {
110 def->superclassList.push_back({type.name, toFullyQualified(type.name), access});
111 }
112 } while (test(COMMA));
113
114 if (!def->superclassList.isEmpty()
115 && knownGadgets.contains(def->superclassList.constFirst().classname)) {
116 // Q_GADGET subclasses are treated as Q_GADGETs
117 knownGadgets.insert(def->classname, def->qualified);
118 knownGadgets.insert(def->qualified, def->qualified);
119 }
120 }
121 if (!test(LBRACE))
122 return false;
123 def->begin = index - 1;
124 bool foundRBrace = until(RBRACE);
125 def->end = index;
126 index = def->begin + 1;
127 return foundRBrace;
128}
129
131{
132 Type type;
133 bool hasSignedOrUnsigned = false;
134 bool isVoid = false;
135 type.firstToken = lookup();
136 for (;;) {
138 switch (next()) {
139 case SIGNED:
140 case UNSIGNED:
141 hasSignedOrUnsigned = true;
142 Q_FALLTHROUGH();
143 case CONST:
144 case VOLATILE:
145 type.name += lexemView();
146 type.name += ' ';
147 if (lookup(0) == VOLATILE)
148 type.isVolatile = true;
149 continue;
150 case Q_MOC_COMPAT_TOKEN:
151 case Q_INVOKABLE_TOKEN:
152 case Q_SCRIPTABLE_TOKEN:
153 case Q_SIGNALS_TOKEN:
154 case Q_SLOTS_TOKEN:
155 case Q_SIGNAL_TOKEN:
156 case Q_SLOT_TOKEN:
157 type.name += lexemView();
158 return type;
159 case NOTOKEN:
160 return type;
161 default:
162 prev();
163 break;
164 }
165 break;
166 }
167
169 if (test(ENUM))
170 type.typeTag = TypeTag::HasEnum;
171 if (test(CLASS))
172 type.typeTag |= TypeTag::HasClass;
173 if (test(STRUCT))
174 type.typeTag |= TypeTag::HasStruct;
175 for(;;) {
177 switch (next()) {
178 case IDENTIFIER:
179 // void mySlot(unsigned myArg)
180 if (hasSignedOrUnsigned) {
181 prev();
182 break;
183 }
184 Q_FALLTHROUGH();
185 case CHAR:
186 case SHORT:
187 case INT:
188 case LONG:
189 type.name += lexemView();
190 // preserve '[unsigned] long long', 'short int', 'long int', 'long double'
191 if (test(LONG) || test(INT) || test(DOUBLE)) {
192 type.name += ' ';
193 prev();
194 continue;
195 }
196 break;
197 case FLOAT:
198 case DOUBLE:
199 case VOID:
200 case BOOL:
201 case AUTO:
202 type.name += lexemView();
203 isVoid |= (lookup(0) == VOID);
204 break;
205 case NOTOKEN:
206 return type;
207 default:
208 prev();
209 ;
210 }
211 if (test(LANGLE)) {
212 if (type.name.isEmpty()) {
213 // '<' cannot start a type
214 return type;
215 }
216 type.name += lexemUntil(RANGLE);
217 }
218 if (test(SCOPE)) {
219 type.name += lexemView();
220 type.isScoped = true;
221 } else {
222 break;
223 }
224 }
225 while (test(CONST) || test(VOLATILE) || test(SIGNED) || test(UNSIGNED)
226 || test(STAR) || test(AND) || test(ANDAND)) {
227 type.name += ' ';
228 type.name += lexemView();
229 if (lookup(0) == AND)
230 type.referenceType = Type::Reference;
231 else if (lookup(0) == ANDAND)
232 type.referenceType = Type::RValueReference;
233 else if (lookup(0) == STAR)
234 type.referenceType = Type::Pointer;
235 }
236 type.rawName = type.name;
237 // transform stupid things like 'const void' or 'void const' into 'void'
238 if (isVoid && type.referenceType == Type::NoReference) {
239 type.name = "void";
240 }
241 return type;
242}
243
249
250bool Moc::parseEnum(EnumDef *def, ClassDef *containingClass)
251{
252 bool isTypdefEnum = false; // typedef enum { ... } Foo;
253
254 if (test(CLASS) || test(STRUCT))
255 def->flags |= EnumIsScoped;
256
257 if (test(IDENTIFIER)) {
258 def->name = lexem();
259 if (containingClass)
260 containingClass->allEnumNames.insert(def->name);
261 } else {
262 if (lookup(-1) != TYPEDEF)
263 return false; // anonymous enum
264 isTypdefEnum = true;
265 }
266 def->lineNumber = symbol().lineNum;
267 if (test(COLON)) { // C++11 strongly typed enum
268 // enum Foo : unsigned long { ... };
269 def->type = normalizeType(parseType().name);
270 }
271 if (!test(LBRACE))
272 return false;
273 auto handleInclude = [this]() -> IncludeState {
274 bool hadIncludeBegin = false;
275 if (test(MOC_INCLUDE_BEGIN)) {
276 currentFilenames.push(symbol().unquotedLexem());
277 // we do not return early to handle empty headers in one go
278 hadIncludeBegin = true;
279 }
280 if (test(NOTOKEN)) {
281 next(MOC_INCLUDE_END);
282 currentFilenames.pop();
284 }
285 if (hadIncludeBegin)
287 else
289 };
290 do {
291 handleInclude();
292 if (lookup() == RBRACE) // accept trailing comma
293 break;
294 next(IDENTIFIER);
295 def->values += lexem();
296 handleInclude();
298 } while (test(EQ) ? until(COMMA) : test(COMMA));
299 next(RBRACE);
300 if (isTypdefEnum) {
301 if (!test(IDENTIFIER))
302 return false;
303 def->name = lexem();
304 // used as the name for our enum, but we don't track it,
305 // because we only care about types that might conflict with members
306 }
307 return true;
308}
309
311{
312 Q_UNUSED(def);
313 while (hasNext()) {
314 ArgumentDef arg;
315 arg.type = parseType();
316 if (arg.type.name == "void")
317 break;
318 if (test(IDENTIFIER))
319 arg.name = lexem();
320 while (test(LBRACK)) {
321 arg.rightType += lexemUntil(RBRACK);
322 }
323 if (test(CONST) || test(VOLATILE)) {
324 arg.rightType += ' ';
325 arg.rightType += lexemView();
326 }
327 arg.normalizedType = normalizeType(QByteArray(arg.type.name + ' ' + arg.rightType));
328 if (test(EQ))
329 arg.isDefault = true;
330 def->arguments += arg;
331 if (!until(COMMA))
332 break;
333 }
334
335 if (!def->arguments.isEmpty()
336 && def->arguments.constLast().normalizedType == "QPrivateSignal") {
337 def->arguments.removeLast();
338 def->isPrivateSignal = true;
339 }
340 if (def->arguments.size() == 1
341 && def->arguments.constLast().normalizedType == "QMethodRawArguments") {
342 def->arguments.removeLast();
343 def->isRawSlot = true;
344 }
345
346 if (Q_UNLIKELY(def->arguments.size() >= std::numeric_limits<int>::max()))
347 error("number of function arguments exceeds std::numeric_limits<int>::max()");
348}
349
351{
352 if (index < symbols.size() && testFunctionAttribute(symbols.at(index).token, def)) {
353 ++index;
354 return true;
355 }
356 return false;
357}
358
360{
361 switch (tok) {
362 case Q_MOC_COMPAT_TOKEN:
363 def->isCompat = true;
364 return true;
365 case Q_INVOKABLE_TOKEN:
366 def->isInvokable = true;
367 return true;
368 case Q_SIGNAL_TOKEN:
369 def->isSignal = true;
370 return true;
371 case Q_SLOT_TOKEN:
372 def->isSlot = true;
373 return true;
374 case Q_SCRIPTABLE_TOKEN:
375 def->isInvokable = def->isScriptable = true;
376 return true;
377 default: break;
378 }
379 return false;
380}
381
383{
384 auto rewind = index;
385 if (test(LBRACK) && test(LBRACK) && until(RBRACK) && test(RBRACK))
386 return true;
387 index = rewind;
388 return false;
389}
390
392{
393 next(LPAREN);
394 QByteArray revisionString = lexemUntil(RPAREN);
395 revisionString.remove(0, 1);
396 revisionString.chop(1);
397 const QList<QByteArray> majorMinor = revisionString.split(',');
398 switch (majorMinor.size()) {
399 case 1: {
400 bool ok = false;
401 const int revision = revisionString.toInt(&ok);
402 if (!ok || !QTypeRevision::isValidSegment(revision))
403 error("Invalid revision");
404 return QTypeRevision::fromMinorVersion(revision);
405 }
406 case 2: { // major.minor
407 bool ok = false;
408 const int major = majorMinor[0].toInt(&ok);
409 if (!ok || !QTypeRevision::isValidSegment(major))
410 error("Invalid major version");
411 const int minor = majorMinor[1].toInt(&ok);
412 if (!ok || !QTypeRevision::isValidSegment(minor))
413 error("Invalid minor version");
414 return QTypeRevision::fromVersion(major, minor);
415 }
416 default:
417 error("Invalid revision");
418 return QTypeRevision();
419 }
420}
421
423{
424
425 if (test(Q_REVISION_TOKEN)) {
426 def->revision = parseRevision().toEncodedVersion<int>();
427 return true;
428 }
429
430 return false;
431}
432
433// returns false if the function should be ignored
434bool Moc::parseFunction(FunctionDef *def, bool inMacro)
435{
436 def->isVirtual = false;
437 def->isStatic = false;
438 //skip modifiers and attributes
441 bool templateFunction = (lookup() == TEMPLATE);
442 def->type = parseType();
443 if (def->type.name.isEmpty()) {
444 if (templateFunction)
445 error("Template function as signal or slot");
446 else
447 error();
448 }
449 bool scopedFunctionName = false;
450 // we might have modifiers and attributes after a tag
451 // note that testFunctionAttribute is handled further below,
452 // and revisions and attributes must come first
453 while (testForFunctionModifiers(def)) {}
454 Type tempType = parseType();
455 while (!tempType.name.isEmpty() && lookup() != LPAREN) {
456 if (testFunctionAttribute(def->type.firstToken, def))
457 ; // fine
458 else if (def->type.firstToken == Q_SIGNALS_TOKEN)
459 error();
460 else if (def->type.firstToken == Q_SLOTS_TOKEN)
461 error();
462 else {
463 if (!def->tag.isEmpty())
464 def->tag += ' ';
465 def->tag += def->type.name;
466 }
467 def->type = tempType;
468 tempType = parseType();
469 }
470 next(LPAREN, "Not a signal or slot declaration");
471 def->name = tempType.name;
472 def->lineNumber = symbol().lineNum;
473
474 scopedFunctionName = tempType.isScoped;
475
476 if (!test(RPAREN)) {
478 next(RPAREN);
479 }
480
481 // support optional macros with compiler specific options
482 while (test(IDENTIFIER))
483 ;
484
485 def->isConst = test(CONST);
486
487 while (test(IDENTIFIER))
488 ;
489
490 if (inMacro) {
491 next(RPAREN);
492 prev();
493 } else {
494 if (test(THROW)) {
495 next(LPAREN);
496 until(RPAREN);
497 }
498
499 if (def->type.name == "auto" && test(ARROW))
500 def->type = parseType(); // Parse trailing return-type
501
502 if (test(SEMIC))
503 ;
504 else if ((def->inlineCode = test(LBRACE)))
505 until(RBRACE);
506 else if ((def->isAbstract = test(EQ)))
507 until(SEMIC);
508 else if (skipCxxAttributes())
509 until(SEMIC);
510 else
511 error();
512 }
513 if (scopedFunctionName) {
514 const QByteArray msg = "Function declaration " + def->name
515 + " contains extra qualification. Ignoring as signal or slot.";
516 warning(msg.constData());
517 return false;
518 }
519
520 QList<QByteArray> typeNameParts = normalizeType(def->type.name).split(' ');
521 if (typeNameParts.contains("auto")) {
522 // We expected a trailing return type but we haven't seen one
523 error("Function declared with auto as return type but missing trailing return type. "
524 "Return type deduction is not supported.");
525 }
526
527 // we don't support references as return types, it's too dangerous
528 if (def->type.referenceType == Type::Reference) {
529 QByteArray rawName = def->type.rawName;
530 def->type = Type("void");
531 def->type.rawName = rawName;
532 }
533
534 def->normalizedType = normalizeType(def->type.name);
535 return true;
536}
537
539{
540 return test(EXPLICIT) || test(INLINE) || test(CONSTEXPR) ||
541 (test(STATIC) && (def->isStatic = true)) ||
542 (test(VIRTUAL) && (def->isVirtual = true));
543}
544
545// like parseFunction, but never aborts with an error
547{
548 def->isVirtual = false;
549 def->isStatic = false;
550 //skip modifiers and attributes
553 bool tilde = test(TILDE);
554 def->type = parseType();
555 if (def->type.name.isEmpty())
556 return false;
557 bool scopedFunctionName = false;
558 if (test(LPAREN)) {
559 def->name = def->type.name;
560 def->lineNumber = symbol().lineNum;
561 scopedFunctionName = def->type.isScoped;
562 if (def->name == cdef->classname) {
563 def->isDestructor = tilde;
564 def->isConstructor = !tilde;
565 def->type = Type();
566 } else {
567 // missing type name? => Skip
568 return false;
569 }
570 } else {
571 // ### TODO: The condition before testForFunctionModifiers shoulnd't be necessary,
572 // but otherwise we end up with misparses
573 if (def->isSlot || def->isSignal || def->isInvokable)
574 while (testForFunctionModifiers(def)) {}
575 Type tempType = parseType();
576 while (!tempType.name.isEmpty() && lookup() != LPAREN) {
577 if (testFunctionAttribute(def->type.firstToken, def))
578 ; // fine
579 else if (def->type.name == "Q_SIGNAL")
580 def->isSignal = true;
581 else if (def->type.name == "Q_SLOT")
582 def->isSlot = true;
583 else {
584 if (!def->tag.isEmpty())
585 def->tag += ' ';
586 def->tag += def->type.name;
587 }
588 def->type = tempType;
589 tempType = parseType();
590 }
591 if (!test(LPAREN))
592 return false;
593 def->name = tempType.name;
594 def->lineNumber = symbol().lineNum;
595 scopedFunctionName = tempType.isScoped;
596 }
597
598 if (!test(RPAREN)) {
600 if (!test(RPAREN))
601 return false;
602 }
603
604 def->isConst = test(CONST);
605
606 while (test(IDENTIFIER))
607 ;
608
609 if (test(THROW)) {
610 next(LPAREN);
611 until(RPAREN);
612 }
613
614 if (def->type.name == "auto" && test(ARROW))
615 def->type = parseType(); // Parse trailing return-type
616
617 if (scopedFunctionName
618 && (def->isSignal || def->isSlot || def->isInvokable)) {
619 const QByteArray msg = "parsemaybe: Function declaration " + def->name
620 + " contains extra qualification. Ignoring as signal or slot.";
621 warning(msg.constData());
622 return false;
623 }
624
625 if (def->isSlot || def->isSignal || def->isInvokable) {
626 QList<QByteArray> typeNameParts = normalizeType(def->type.name).split(' ');
627 if (typeNameParts.contains("auto")) {
628 // We expected a trailing return type but we haven't seen one
629 error("Function declared with auto as return type but missing trailing return type. "
630 "Return type deduction is not supported.");
631 }
632 }
633 // we don't support references as return types, it's too dangerous
634 if (def->type.referenceType == Type::Reference) {
635 QByteArray rawName = def->type.rawName;
636 def->type = Type("void");
637 def->type.rawName = rawName;
638 }
639
640 def->normalizedType = normalizeType(def->type.name);
641 return true;
642}
643
644inline void handleDefaultArguments(QList<FunctionDef> *functionList, FunctionDef &function)
645{
646 // support a function with a default argument by pretending there is an
647 // overload without the argument (the original function is the overload with
648 // all arguments present)
649 while (function.arguments.size() > 0 && function.arguments.constLast().isDefault) {
650 function.wasCloned = true;
651 function.arguments.removeLast();
652 *functionList += function;
653 }
654}
655
656void Moc::prependNamespaces(BaseDef &def, const QList<NamespaceDef> &namespaceList) const
657{
658 auto it = namespaceList.crbegin();
659 const auto rend = namespaceList.crend();
660 for (; it != rend; ++it) {
661 if (inNamespace(&*it))
662 def.qualified.prepend(it->classname + "::");
663 }
664}
665
666void Moc::checkListSizes(const ClassDef &def)
667{
668 if (Q_UNLIKELY(def.nonClassSignalList.size() > std::numeric_limits<int>::max()))
669 error("number of signals defined in parent class(es) exceeds "
670 "std::numeric_limits<int>::max().");
671
672 if (Q_UNLIKELY(def.propertyList.size() > std::numeric_limits<int>::max()))
673 error("number of bindable properties exceeds std::numeric_limits<int>::max().");
674
675 if (Q_UNLIKELY(def.classInfoList.size() > std::numeric_limits<int>::max()))
676 error("number of times Q_CLASSINFO macro is used exceeds "
677 "std::numeric_limits<int>::max().");
678
679 if (Q_UNLIKELY(def.enumList.size() > std::numeric_limits<int>::max()))
680 error("number of enumerations exceeds std::numeric_limits<int>::max().");
681
682 if (Q_UNLIKELY(def.superclassList.size() > std::numeric_limits<int>::max()))
683 error("number of super classes exceeds std::numeric_limits<int>::max().");
684
685 if (Q_UNLIKELY(def.constructorList.size() > std::numeric_limits<int>::max()))
686 error("number of constructor parameters exceeds std::numeric_limits<int>::max().");
687
688 if (Q_UNLIKELY(def.signalList.size() > std::numeric_limits<int>::max()))
689 error("number of signals exceeds std::numeric_limits<int>::max().");
690
691 if (Q_UNLIKELY(def.slotList.size() > std::numeric_limits<int>::max()))
692 error("number of declared slots exceeds std::numeric_limits<int>::max().");
693
694 if (Q_UNLIKELY(def.methodList.size() > std::numeric_limits<int>::max()))
695 error("number of methods exceeds std::numeric_limits<int>::max().");
696
697 if (Q_UNLIKELY(def.publicList.size() > std::numeric_limits<int>::max()))
698 error("number of public functions declared in this class exceeds "
699 "std::numeric_limits<int>::max().");
700}
701
702void Moc::parse()
703{
704 QList<NamespaceDef> namespaceList;
705 bool templateClass = false;
706 while (hasNext()) {
707 Token t = next();
708 switch (t) {
709 case NAMESPACE: {
710 qsizetype rewind = index;
711 if (test(IDENTIFIER)) {
712 QByteArray nsName = lexem();
713 QByteArrayList nested;
714 while (test(SCOPE)) {
715 /* treat (C++20's) namespace A::inline B {} as A::B
716 this is mostly to not break compilation when encountering such
717 a construct in a header; the interaction of Qt's meta-macros with
718 inline namespaces is still rather poor.
719 */
720 test(INLINE);
721 next(IDENTIFIER);
722 nested.append(nsName);
723 nsName = lexem();
724 }
725 if (test(EQ)) {
726 // namespace Foo = Bar::Baz;
727 until(SEMIC);
728 } else if (test(LPAREN)) {
729 // Ignore invalid code such as: 'namespace __identifier("x")' (QTBUG-56634)
730 until(RPAREN);
731 } else if (!test(SEMIC)) {
732 NamespaceDef def;
733 def.classname = nsName;
734 def.lineNumber = symbol().lineNum;
735 def.doGenerate = currentFilenames.size() <= 1;
736
737 next(LBRACE);
738 def.begin = index - 1;
739 until(RBRACE);
740 def.end = index;
741 index = def.begin + 1;
742
743 prependNamespaces(def, namespaceList);
744
745 for (const QByteArray &ns : nested) {
746 NamespaceDef parentNs;
747 parentNs.classname = ns;
748 parentNs.qualified = def.qualified;
749 def.qualified += ns + "::";
750 parentNs.begin = def.begin;
751 parentNs.end = def.end;
752 namespaceList += parentNs;
753 }
754
755 while (inNamespace(&def) && hasNext()) {
756 switch (next()) {
757 case NAMESPACE:
758 if (test(IDENTIFIER)) {
759 while (test(SCOPE)) {
760 test(INLINE); // ignore inline namespaces
761 next(IDENTIFIER);
762 }
763 if (test(EQ)) {
764 // namespace Foo = Bar::Baz;
765 until(SEMIC);
766 } else if (!test(SEMIC)) {
767 until(RBRACE);
768 }
769 }
770 break;
771 case Q_NAMESPACE_TOKEN:
772 def.hasQNamespace = true;
773 break;
774 case Q_NAMESPACE_EXPORT_TOKEN:
775 next(LPAREN);
776 while (test(IDENTIFIER))
777 {}
778 next(RPAREN);
779 def.hasQNamespace = true;
780 break;
781 case Q_ENUMS_TOKEN:
782 case Q_ENUM_NS_TOKEN:
783 parseEnumOrFlag(&def, {});
784 break;
785 case Q_ENUM_TOKEN:
786 error("Q_ENUM can't be used in a Q_NAMESPACE, use Q_ENUM_NS instead");
787 break;
788 case Q_FLAGS_TOKEN:
789 case Q_FLAG_NS_TOKEN:
790 parseEnumOrFlag(&def, EnumIsFlag);
791 break;
792 case Q_FLAG_TOKEN:
793 error("Q_FLAG can't be used in a Q_NAMESPACE, use Q_FLAG_NS instead");
794 break;
795 case Q_DECLARE_FLAGS_TOKEN:
796 parseFlag(&def);
797 break;
798 case Q_CLASSINFO_TOKEN:
799 parseClassInfo(&def);
800 break;
801 case Q_MOC_INCLUDE_TOKEN:
802 // skip it, the namespace is parsed twice
803 next(LPAREN);
804 lexemUntil(RPAREN);
805 break;
806 case ENUM: {
807 EnumDef enumDef;
808 if (parseEnum(&enumDef, nullptr))
809 def.enumList += enumDef;
810 } break;
811 case CLASS:
812 case STRUCT: {
813 ClassDef classdef;
814 if (!parseClassHead(&classdef))
815 continue;
816 while (inClass(&classdef) && hasNext())
817 next(); // consume all Q_XXXX macros from this class
818 } break;
819 default: break;
820 }
821 }
822 namespaceList += def;
823 index = rewind;
824 if (!def.hasQNamespace && (!def.classInfoList.isEmpty() || !def.enumDeclarations.isEmpty()))
825 error("Namespace declaration lacks Q_NAMESPACE macro.");
826 }
827 }
828 break;
829 }
830 case SEMIC:
831 case RBRACE:
832 templateClass = false;
833 break;
834 case TEMPLATE:
835 templateClass = true;
836 break;
837 case MOC_INCLUDE_BEGIN:
838 currentFilenames.push(symbol().unquotedLexem());
839 break;
840 case MOC_INCLUDE_END:
841 currentFilenames.pop();
842 break;
843 case Q_DECLARE_INTERFACE_TOKEN:
845 break;
846 case Q_DECLARE_METATYPE_TOKEN:
848 break;
849 case Q_MOC_INCLUDE_TOKEN:
851 break;
852 case USING:
853 if (test(NAMESPACE)) {
854 while (test(SCOPE) || test(IDENTIFIER))
855 ;
856 // Ignore invalid code such as: 'using namespace __identifier("x")' (QTBUG-63772)
857 if (test(LPAREN))
858 until(RPAREN);
859 next(SEMIC);
860 }
861 break;
862 case CLASS:
863 case STRUCT: {
864 if (currentFilenames.size() <= 1)
865 break;
866
867 ClassDef def;
868 if (!parseClassHead(&def))
869 continue;
870
871 while (inClass(&def) && hasNext()) {
872 switch (next()) {
873 case Q_OBJECT_TOKEN:
874 def.hasQObject = true;
875 break;
876 case Q_GADGET_EXPORT_TOKEN:
877 next(LPAREN);
878 while (test(IDENTIFIER))
879 {}
880 next(RPAREN);
881 Q_FALLTHROUGH();
882 case Q_GADGET_TOKEN:
883 def.hasQGadget = true;
884 break;
885 default: break;
886 }
887 }
888
889 if (!def.hasQObject && !def.hasQGadget)
890 continue;
891
892 prependNamespaces(def, namespaceList);
893
894 QHash<QByteArray, QByteArray> &classHash = def.hasQObject ? knownQObjectClasses : knownGadgets;
895 classHash.insert(def.classname, def.qualified);
896 classHash.insert(def.qualified, def.qualified);
897
898 continue; }
899 default: break;
900 }
901 if ((t != CLASS && t != STRUCT)|| currentFilenames.size() > 1)
902 continue;
903 ClassDef def;
904 if (parseClassHead(&def)) {
905 Symbol qmlRegistrationMacroSymbol = {};
906 prependNamespaces(def, namespaceList);
907
909 while (inClass(&def) && hasNext()) {
910 switch ((t = next())) {
911 case PRIVATE:
912 access = FunctionDef::Private;
913 if (test(Q_SIGNALS_TOKEN))
914 error("Signals cannot have access specifier");
915 break;
916 case PROTECTED:
917 access = FunctionDef::Protected;
918 if (test(Q_SIGNALS_TOKEN))
919 error("Signals cannot have access specifier");
920 break;
921 case PUBLIC:
922 access = FunctionDef::Public;
923 if (test(Q_SIGNALS_TOKEN))
924 error("Signals cannot have access specifier");
925 break;
926 case STRUCT:
927 case CLASS: {
928 ClassDef nestedDef;
929 if (parseClassHead(&nestedDef)) {
930 while (inClass(&nestedDef) && inClass(&def)) {
931 t = next();
932 if (t >= Q_META_TOKEN_BEGIN && t < Q_META_TOKEN_END)
933 error("Meta object features not supported for nested classes");
934 }
935 }
936 } break;
937 case Q_SIGNALS_TOKEN:
938 parseSignals(&def);
939 break;
940 case Q_SLOTS_TOKEN:
941 switch (lookup(-1)) {
942 case PUBLIC:
943 case PROTECTED:
944 case PRIVATE:
945 parseSlots(&def, access);
946 break;
947 default:
948 error("Missing access specifier for slots");
949 }
950 break;
951 case Q_OBJECT_TOKEN:
952 def.hasQObject = true;
953 if (templateClass)
954 error("Template classes not supported by Q_OBJECT");
955 if (def.classname != "Qt" && def.classname != "QObject" && def.superclassList.isEmpty())
956 error("Class contains Q_OBJECT macro but does not inherit from QObject");
957 break;
958 case Q_GADGET_EXPORT_TOKEN:
959 next(LPAREN);
960 while (test(IDENTIFIER))
961 {}
962 next(RPAREN);
963 Q_FALLTHROUGH();
964 case Q_GADGET_TOKEN:
965 def.hasQGadget = true;
966 if (templateClass)
967 error("Template classes not supported by Q_GADGET");
968 break;
969 case Q_PROPERTY_TOKEN:
971 break;
972 case QT_ANONYMOUS_PROPERTY_TOKEN:
974 break;
975 case Q_PLUGIN_METADATA_TOKEN:
977 break;
978 case Q_ENUMS_TOKEN:
979 case Q_ENUM_TOKEN:
980 parseEnumOrFlag(&def, {});
981 break;
982 case Q_ENUM_NS_TOKEN:
983 error("Q_ENUM_NS can't be used in a Q_OBJECT/Q_GADGET, use Q_ENUM instead");
984 break;
985 case Q_FLAGS_TOKEN:
986 case Q_FLAG_TOKEN:
987 parseEnumOrFlag(&def, EnumIsFlag);
988 break;
989 case Q_FLAG_NS_TOKEN:
990 error("Q_FLAG_NS can't be used in a Q_OBJECT/Q_GADGET, use Q_FLAG instead");
991 break;
992 case Q_DECLARE_FLAGS_TOKEN:
993 parseFlag(&def);
994 break;
995 case Q_CLASSINFO_TOKEN:
997 break;
998 case Q_MOC_INCLUDE_TOKEN:
1000 break;
1001 case Q_INTERFACES_TOKEN:
1003 break;
1004 case Q_PRIVATE_SLOT_TOKEN:
1005 parseSlotInPrivate(&def, access);
1006 break;
1007 case Q_PRIVATE_PROPERTY_TOKEN:
1009 break;
1010 case QT_ANONYMOUS_PRIVATE_PROPERTY_TOKEN:
1012 break;
1013 case ENUM: {
1014 EnumDef enumDef;
1015 if (parseEnum(&enumDef, &def))
1016 def.enumList += enumDef;
1017 } break;
1018 case SEMIC:
1019 case COLON:
1020 break;
1021 case IDENTIFIER:
1022 {
1023 const QByteArrayView lex = lexemView();
1024 if (lex.startsWith("QML_")) {
1025 if ( lex == "QML_ELEMENT" || lex == "QML_NAMED_ELEMENT"
1026 || lex == "QML_ANONYMOUS" || lex == "QML_VALUE_TYPE") {
1027 qmlRegistrationMacroSymbol = symbol();
1028 }
1029 }
1030 }
1031 Q_FALLTHROUGH();
1032 default:
1033 FunctionDef funcDef;
1034 funcDef.access = access;
1035 qsizetype rewind = index--;
1036 if (parseMaybeFunction(&def, &funcDef)) {
1037 if (funcDef.isConstructor) {
1038 if ((access == FunctionDef::Public) && funcDef.isInvokable) {
1039 def.constructorList += funcDef;
1040 handleDefaultArguments(&def.constructorList, funcDef);
1041 }
1042 } else if (funcDef.isDestructor) {
1043 // don't care about destructors
1044 } else {
1045 if (access == FunctionDef::Public)
1046 def.publicList += funcDef;
1047 if (funcDef.isSlot) {
1048 def.slotList += funcDef;
1049 handleDefaultArguments(&def.slotList, funcDef);
1050 if (funcDef.revision > 0)
1051 ++def.revisionedMethods;
1052 } else if (funcDef.isSignal) {
1053 def.signalList += funcDef;
1054 handleDefaultArguments(&def.signalList, funcDef);
1055 if (funcDef.revision > 0)
1056 ++def.revisionedMethods;
1057 } else if (funcDef.isInvokable) {
1058 def.methodList += funcDef;
1059 handleDefaultArguments(&def.methodList, funcDef);
1060 if (funcDef.revision > 0)
1061 ++def.revisionedMethods;
1062 }
1063 }
1064 } else {
1065 index = rewind;
1066 }
1067 }
1068 }
1069
1070 next(RBRACE);
1071
1072 /* if the header is available, moc will see a Q_CLASSINFO entry; the
1073 token is only visible if the header is missing
1074 To avoid false positives, we only warn when encountering the token in a QObject or gadget
1075 */
1076 if ((def.hasQObject || def.hasQGadget) && qmlRegistrationMacroSymbol.token != NOTOKEN) {
1077 QByteArray msg("Potential QML registration macro was found, but no header containing it was included.\n"
1078 "This might cause runtime errors in QML applications\n"
1079 "Include <QtQmlIntegration/qqmlintegration.h> or <QtQml/qqmlregistration.h> to fix this.");
1080 if (qmlMacroWarningIsFatal)
1081 error(qmlRegistrationMacroSymbol, msg.constData());
1082 else
1083 warning(qmlRegistrationMacroSymbol, msg.constData());
1084 }
1085
1086 if (!def.hasQObject && !def.hasQGadget && def.signalList.isEmpty() && def.slotList.isEmpty()
1087 && def.propertyList.isEmpty() && def.enumDeclarations.isEmpty())
1088 continue; // no meta object code required
1089
1090
1091 if (!def.hasQObject && !def.hasQGadget)
1092 error("Class declaration lacks Q_OBJECT macro.");
1093
1094 // Add meta tags to the plugin meta data:
1095 if (!def.pluginData.iid.isEmpty())
1096 def.pluginData.metaArgs = metaArgs;
1097
1098 if (def.hasQObject && !def.superclassList.isEmpty())
1100
1102
1104
1105 classList += def;
1106 QHash<QByteArray, QByteArray> &classHash = def.hasQObject ? knownQObjectClasses : knownGadgets;
1107 classHash.insert(def.classname, def.qualified);
1108 classHash.insert(def.qualified, def.qualified);
1109 }
1110 }
1111 for (const auto &n : std::as_const(namespaceList)) {
1112 if (!n.hasQNamespace)
1113 continue;
1114 ClassDef def;
1115 static_cast<BaseDef &>(def) = static_cast<BaseDef>(n);
1116 def.qualified += def.classname;
1117 def.hasQNamespace = true;
1118 auto it = std::find_if(classList.begin(), classList.end(), [&def](const ClassDef &val) {
1119 return def.classname == val.classname && def.qualified == val.qualified;
1120 });
1121
1122 if (it != classList.end()) {
1123 it->classInfoList += def.classInfoList;
1124 Q_ASSERT(it->classInfoList.size() <= std::numeric_limits<int>::max());
1125 it->enumDeclarations.insert(def.enumDeclarations);
1126 it->enumList += def.enumList;
1127 Q_ASSERT(it->enumList.size() <= std::numeric_limits<int>::max());
1128 it->flagAliases.insert(def.flagAliases);
1129 } else {
1130 knownGadgets.insert(def.classname, def.qualified);
1131 knownGadgets.insert(def.qualified, def.qualified);
1132 if (n.doGenerate)
1133 classList += def;
1134 }
1135 }
1136}
1137
1139{
1140 QByteArrayView fn = QByteArrayView(filename);
1141
1142 auto isSlash = [](char ch) { return ch == '/' || ch == '\\'; };
1143 auto rit = std::find_if(fn.crbegin(), fn.crend(), isSlash);
1144 if (rit != fn.crend())
1145 fn = fn.last(rit - fn.crbegin());
1146
1147 return fn;
1148}
1149
1150static bool any_type_contains(const QList<PropertyDef> &properties, const QByteArray &pattern)
1151{
1152 for (const auto &p : properties) {
1153 if (p.type.contains(pattern))
1154 return true;
1155 }
1156 return false;
1157}
1158
1159static bool any_arg_contains(const QList<FunctionDef> &functions, const QByteArray &pattern)
1160{
1161 for (const auto &f : functions) {
1162 for (const auto &arg : f.arguments) {
1163 if (arg.normalizedType.contains(pattern))
1164 return true;
1165 }
1166 }
1167 return false;
1168}
1169
1171{
1172 QByteArrayList result;
1173 result
1174#define STREAM_SMART_POINTER(SMART_POINTER) << #SMART_POINTER
1175 QT_FOR_EACH_AUTOMATIC_TEMPLATE_SMART_POINTER(STREAM_SMART_POINTER)
1176#undef STREAM_SMART_POINTER
1177#define STREAM_1ARG_TEMPLATE(TEMPLATENAME) << #TEMPLATENAME
1178 QT_FOR_EACH_AUTOMATIC_TEMPLATE_1ARG(STREAM_1ARG_TEMPLATE)
1179#undef STREAM_1ARG_TEMPLATE
1180 ;
1181 return result;
1182}
1183
1185{
1186 static const QByteArrayList candidates = make_candidates();
1187
1188 QByteArrayList required;
1189 required.reserve(candidates.size());
1190
1191 bool needsQProperty = false;
1192
1193 for (const auto &candidate : candidates) {
1194 const QByteArray pattern = candidate + '<';
1195
1196 for (const auto &c : classes) {
1197 for (const auto &p : c.propertyList)
1198 needsQProperty |= !p.bind.isEmpty();
1199 if (any_type_contains(c.propertyList, pattern) ||
1200 any_arg_contains(c.slotList, pattern) ||
1201 any_arg_contains(c.signalList, pattern) ||
1202 any_arg_contains(c.methodList, pattern)) {
1203 required.push_back(candidate);
1204 break;
1205 }
1206 }
1207 }
1208
1209 if (needsQProperty)
1210 required.push_back("QProperty");
1211
1212 return required;
1213}
1214
1215void Moc::generate(FILE *out, FILE *jsonOutput)
1216{
1217 QByteArrayView fn = strippedFileName();
1218
1219 fprintf(out, "/****************************************************************************\n"
1220 "** Meta object code from reading C++ file '%s'\n**\n" , fn.constData());
1221 fprintf(out, "** Created by: The Qt Meta Object Compiler version %d (Qt %s)\n**\n" , mocOutputRevision, QT_VERSION_STR);
1222 fprintf(out, "** WARNING! All changes made in this file will be lost!\n"
1223 "*****************************************************************************/\n\n");
1224
1225 // include header(s) of user class definitions at _first_ to allow
1226 // for preprocessor definitions possibly affecting standard headers.
1227 // see https://codereview.qt-project.org/c/qt/qtbase/+/445937
1228 if (!noInclude) {
1229 if (includePath.size() && !includePath.endsWith('/'))
1230 includePath += '/';
1231 for (QByteArray inc : std::as_const(includeFiles)) {
1232 if (!inc.isEmpty() && inc.at(0) != '<' && inc.at(0) != '"') {
1233 if (includePath.size() && includePath != "./")
1234 inc.prepend(includePath);
1235 inc = '\"' + inc + '\"';
1236 }
1237 fprintf(out, "#include %s\n", inc.constData());
1238 }
1239 }
1240 if (classList.size() && classList.constFirst().classname == "Qt")
1241 fprintf(out, "#include <QtCore/qobject.h>\n");
1242
1243 fprintf(out, "#include <QtCore/qmetatype.h>\n"); // For QMetaType::Type
1245 fprintf(out, "#include <QtCore/qplugin.h>\n");
1246
1247 const auto qtContainers = requiredQtContainers(classList);
1248 for (const QByteArray &qtContainer : qtContainers)
1249 fprintf(out, "#include <QtCore/%s>\n", qtContainer.constData());
1250
1251 fprintf(out, "\n#include <QtCore/qtmochelpers.h>\n");
1252
1253 fprintf(out, "\n#include <memory>\n\n"); // For std::addressof
1254 fprintf(out, "\n#include <QtCore/qxptype_traits.h>\n"); // is_detected
1255
1256 fprintf(out, "#if !defined(Q_MOC_OUTPUT_REVISION)\n"
1257 "#error \"The header file '%s' doesn't include <QObject>.\"\n", fn.constData());
1258 fprintf(out, "#elif Q_MOC_OUTPUT_REVISION != %d\n", mocOutputRevision);
1259 fprintf(out, "#error \"This file was generated using the moc from %s."
1260 " It\"\n#error \"cannot be used with the include files from"
1261 " this version of Qt.\"\n#error \"(The moc has changed too"
1262 " much.)\"\n", QT_VERSION_STR);
1263 fprintf(out, "#endif\n\n");
1264
1265#if QT_VERSION <= QT_VERSION_CHECK(7, 0, 0)
1266 fprintf(out, "#ifndef Q_CONSTINIT\n"
1267 "#define Q_CONSTINIT\n"
1268 "#endif\n\n");
1269#endif
1270
1271 // filter out undeclared enumerators and sets
1272 for (ClassDef &cdef : classList) {
1273 QList<EnumDef> enumList;
1274 for (EnumDef def : std::as_const(cdef.enumList)) {
1275 if (cdef.enumDeclarations.contains(def.name)) {
1276 enumList += def;
1277 }
1278 def.enumName = def.name;
1279 QByteArray alias = cdef.flagAliases.value(def.name);
1280 if (cdef.enumDeclarations.contains(alias)) {
1281 def.name = alias;
1282 def.flags |= cdef.enumDeclarations[alias];
1283 enumList += def;
1284 }
1285 }
1286 cdef.enumList = enumList;
1287 }
1288
1289 fprintf(out, "QT_WARNING_PUSH\n");
1290 fprintf(out, "QT_WARNING_DISABLE_DEPRECATED\n");
1291 fprintf(out, "QT_WARNING_DISABLE_GCC(\"-Wuseless-cast\")\n");
1292
1293 fputs("", out);
1294 for (const ClassDef &def : std::as_const(classList)) {
1295 Generator generator(this, &def, metaTypes, knownQObjectClasses, knownGadgets, out,
1296 requireCompleteTypes);
1297 generator.generateCode();
1298
1299 // generator.generateCode() should have already registered all strings
1300 if (Q_UNLIKELY(generator.registeredStringsCount() >= std::numeric_limits<int>::max())) {
1301 error("internal limit exceeded: number of parsed strings is too big.");
1302 exit(EXIT_FAILURE);
1303 }
1304 }
1305 fputs("", out);
1306
1307 fprintf(out, "QT_WARNING_POP\n");
1308
1309 if (jsonOutput) {
1310 QJsonObject mocData;
1311 mocData["outputRevision"_L1] = mocOutputRevision;
1312 mocData["inputFile"_L1] = QLatin1StringView(fn.constData());
1313
1314 QJsonArray classesJsonFormatted;
1315
1316 for (const ClassDef &cdef: std::as_const(classList))
1317 classesJsonFormatted.append(cdef.toJson());
1318
1319 if (!classesJsonFormatted.isEmpty())
1320 mocData["classes"_L1] = classesJsonFormatted;
1321
1322 QJsonDocument jsonDoc(mocData);
1323 fputs(jsonDoc.toJson().constData(), jsonOutput);
1324 }
1325}
1326
1328{
1329 QTypeRevision defaultRevision;
1330 if (test(Q_REVISION_TOKEN))
1331 defaultRevision = parseRevision();
1332
1333 next(COLON);
1334 while (inClass(def) && hasNext()) {
1335 switch (next()) {
1336 case PUBLIC:
1337 case PROTECTED:
1338 case PRIVATE:
1339 case Q_SIGNALS_TOKEN:
1340 case Q_SLOTS_TOKEN:
1341 prev();
1342 return;
1343 case SEMIC:
1344 continue;
1345 case FRIEND:
1346 until(SEMIC);
1347 continue;
1348 case USING:
1349 error("'using' directive not supported in 'slots' section");
1350 default:
1351 prev();
1352 }
1353
1354 FunctionDef funcDef;
1355 funcDef.access = access;
1356 if (!parseFunction(&funcDef))
1357 continue;
1358 if (funcDef.revision > 0) {
1360 } else if (defaultRevision.isValid()) {
1361 funcDef.revision = defaultRevision.toEncodedVersion<int>();
1363 }
1364 def->slotList += funcDef;
1365 handleDefaultArguments(&def->slotList, funcDef);
1366 }
1367}
1368
1370{
1371 QTypeRevision defaultRevision;
1372 if (test(Q_REVISION_TOKEN))
1373 defaultRevision = parseRevision();
1374
1375 next(COLON);
1376 while (inClass(def) && hasNext()) {
1377 switch (next()) {
1378 case PUBLIC:
1379 case PROTECTED:
1380 case PRIVATE:
1381 case Q_SIGNALS_TOKEN:
1382 case Q_SLOTS_TOKEN:
1383 prev();
1384 return;
1385 case SEMIC:
1386 continue;
1387 case FRIEND:
1388 until(SEMIC);
1389 continue;
1390 case USING:
1391 error("'using' directive not supported in 'signals' section");
1392 default:
1393 prev();
1394 }
1395 FunctionDef funcDef;
1397 parseFunction(&funcDef);
1398 if (funcDef.isVirtual)
1399 warning("Signals cannot be declared virtual");
1400 if (funcDef.inlineCode)
1401 error("Not a signal declaration");
1402 if (funcDef.revision > 0) {
1404 } else if (defaultRevision.isValid()) {
1405 funcDef.revision = defaultRevision.toEncodedVersion<int>();
1407 }
1408 def->signalList += funcDef;
1409 handleDefaultArguments(&def->signalList, funcDef);
1410 }
1411}
1412
1413void Moc::createPropertyDef(PropertyDef &propDef, int propertyIndex, Moc::PropertyMode mode)
1414{
1415 propDef.location = index;
1416 propDef.relativeIndex = propertyIndex;
1417 propDef.lineNumber = symbol().lineNum;
1418
1419 Type t = parseType();
1420 QByteArray type = t.name;
1421 if (type.isEmpty())
1422 error();
1423 propDef.typeTag = t.typeTag;
1424 propDef.designable = propDef.scriptable = propDef.stored = "true";
1425 propDef.user = "false";
1426 /*
1427 The Q_PROPERTY construct cannot contain any commas, since
1428 commas separate macro arguments. We therefore expect users
1429 to type "QMap" instead of "QMap<QString, QVariant>". For
1430 coherence, we also expect the same for
1431 QValueList<QVariant>, the other template class supported by
1432 QVariant.
1433 */
1434 type = normalizeType(type);
1435 if (type == "QMap")
1436 type = "QMap<QString,QVariant>";
1437 else if (type == "LongLong")
1438 type = "qlonglong";
1439 else if (type == "ULongLong")
1440 type = "qulonglong";
1441
1442 propDef.type = type;
1443
1444 if (mode == Moc::Named) {
1445 next();
1446 propDef.name = lexem();
1447 }
1448
1450}
1451
1453{
1454 auto checkIsFunction = [&](const QByteArray &def, const char *name) {
1455 if (def.endsWith(')')) {
1456 QByteArray msg = "Providing a function for ";
1457 msg += name;
1458 msg += " in a property declaration is not be supported in Qt 6.";
1459 error(msg.constData());
1460 }
1461 };
1462
1463 while (test(IDENTIFIER)) {
1464 const Symbol &lsym = symbol();
1465 const QByteArrayView l = lsym.lexemView();
1466 if (l[0] == 'C' && l == "CONSTANT") {
1467 propDef.constant = true;
1468 continue;
1469 } else if (l[0] == 'F' && l == "FINAL") {
1470 propDef.final = true;
1471 continue;
1472 } else if (l[0] == 'N' && l == "NAME") {
1473 next(IDENTIFIER);
1474 propDef.name = lexem();
1475 continue;
1476 } else if (l[0] == 'O' && l == "OVERRIDE") {
1477 propDef.override = true;
1478 continue;
1479 } else if (l[0] == 'R' && l == "REQUIRED") {
1480 propDef.required = true;
1481 continue;
1482 } else if (l[0] == 'R' && l == "REVISION" && test(LPAREN)) {
1483 prev();
1484 propDef.revision = parseRevision().toEncodedVersion<int>();
1485 continue;
1486 } else if (l[0] == 'V' && l == "VIRTUAL") {
1487 propDef.virtual_ = true;
1488 continue;
1489 }
1490
1491 QByteArray v, v2;
1492 if (test(LPAREN)) {
1493 v = lexemUntil(RPAREN);
1494 v = v.mid(1, v.size() - 2); // removes the '(' and ')'
1495 } else if (test(INTEGER_LITERAL)) {
1496 v = lexem();
1497 if (l != "REVISION")
1498 error(lsym);
1499 } else if (test(DEFAULT)) {
1500 v = lexem();
1501 if (l != "READ" && l != "WRITE")
1502 error(lsym);
1503 } else {
1504 next(IDENTIFIER);
1505 v = lexem();
1506 if (test(LPAREN))
1507 v2 = lexemUntil(RPAREN);
1508 else if (v != "true" && v != "false")
1509 v2 = "()";
1510 }
1511 switch (l[0]) {
1512 case 'M':
1513 if (l == "MEMBER")
1514 propDef.member = v;
1515 else
1516 error(lsym);
1517 break;
1518 case 'R':
1519 if (l == "READ")
1520 propDef.read = v;
1521 else if (l == "RESET")
1522 propDef.reset = v;
1523 else if (l == "REVISION") {
1524 bool ok = false;
1525 const int minor = v.toInt(&ok);
1526 if (!ok || !QTypeRevision::isValidSegment(minor))
1527 error(lsym);
1528 propDef.revision = QTypeRevision::fromMinorVersion(minor).toEncodedVersion<int>();
1529 } else
1530 error(lsym);
1531 break;
1532 case 'S':
1533 if (l == "SCRIPTABLE") {
1534 propDef.scriptable = v + v2;
1535 checkIsFunction(propDef.scriptable, "SCRIPTABLE");
1536 } else if (l == "STORED") {
1537 propDef.stored = v + v2;
1538 checkIsFunction(propDef.stored, "STORED");
1539 } else
1540 error(lsym);
1541 break;
1542 case 'W': if (l != "WRITE") error(lsym);
1543 propDef.write = v;
1544 break;
1545 case 'B': if (l != "BINDABLE") error(lsym);
1546 propDef.bind = v;
1547 break;
1548 case 'D': if (l != "DESIGNABLE") error(lsym);
1549 propDef.designable = v + v2;
1550 checkIsFunction(propDef.designable, "DESIGNABLE");
1551 break;
1552 case 'N': if (l != "NOTIFY") error(lsym);
1553 propDef.notify = v;
1554 break;
1555 case 'U': if (l != "USER") error(lsym);
1556 propDef.user = v + v2;
1557 checkIsFunction(propDef.user, "USER");
1558 break;
1559 default:
1560 error(lsym);
1561 }
1562 }
1563 if (propDef.constant && !propDef.write.isNull()) {
1564 const QByteArray msg = "Property declaration " + propDef.name
1565 + " is both WRITEable and CONSTANT. CONSTANT will be ignored.";
1566 propDef.constant = false;
1567 warning(msg.constData());
1568 }
1569 if (propDef.constant && !propDef.notify.isNull()) {
1570 const QByteArray msg = "Property declaration " + propDef.name
1571 + " is both NOTIFYable and CONSTANT. CONSTANT will be ignored.";
1572 propDef.constant = false;
1573 warning(msg.constData());
1574 }
1575 if (propDef.constant && !propDef.bind.isNull()) {
1576 const QByteArray msg = "Property declaration " + propDef.name
1577 + " is both BINDable and CONSTANT. CONSTANT will be ignored.";
1578 propDef.constant = false;
1579 warning(msg.constData());
1580 }
1581 if (propDef.read == "default" && propDef.bind.isNull()) {
1582 const QByteArray msg = "Property declaration " + propDef.name
1583 + " is not BINDable but default-READable. READ will be ignored.";
1584 propDef.read = "";
1585 warning(msg.constData());
1586 }
1587 if (propDef.write == "default" && propDef.bind.isNull()) {
1588 const QByteArray msg = "Property declaration " + propDef.name
1589 + " is not BINDable but default-WRITEable. WRITE will be ignored.";
1590 propDef.write = "";
1591 warning(msg.constData());
1592 }
1593 if (propDef.override && propDef.virtual_) {
1594 const QByteArray msg = "Issue with property declaration " + propDef.name
1595 + ": VIRTUAL is redundant when overriding a property. The OVERRIDE "
1596 "must only be used when actually overriding an existing property; using it on a "
1597 "new property is an error.";
1598 error(msg.constData());
1599 }
1600 if (propDef.override && propDef.final) {
1601 const QByteArray msg = "Issue with property declaration " + propDef.name
1602 + ": OVERRIDE is redundant when property is marked FINAL";
1603 error(msg.constData());
1604 }
1605 if (propDef.virtual_ && propDef.final) {
1606 const QByteArray msg = "Issue with property declaration " + propDef.name
1607 + ": The VIRTUAL cannot be combined with FINAL, as these attributes are mutually "
1608 "exclusive";
1609 error(msg.constData());
1610 }
1611}
1612
1614{
1615 next(LPAREN);
1616 PropertyDef propDef;
1617 createPropertyDef(propDef, int(def->propertyList.size()), mode);
1618 next(RPAREN);
1619
1620 def->propertyList += propDef;
1621}
1622
1624{
1625 next(LPAREN);
1626 QByteArray metaData;
1627 while (test(IDENTIFIER)) {
1628 QByteArray l = lexem();
1629 if (l == "IID") {
1630 next(STRING_LITERAL);
1631 def->pluginData.iid = unquotedLexem();
1632 } else if (l == "URI") {
1633 next(STRING_LITERAL);
1634 def->pluginData.uri = unquotedLexem();
1635 } else if (l == "FILE") {
1636 next(STRING_LITERAL);
1637 QByteArrayView metaDataFile = unquotedLexemView();
1638 QFileInfo fi(QFileInfo(QString::fromLocal8Bit(currentFilenames.top())).dir(),
1639 QString::fromLocal8Bit(metaDataFile));
1640 for (const IncludePath &p : std::as_const(includes)) {
1641 if (fi.exists())
1642 break;
1643 if (p.isFrameworkPath)
1644 continue;
1645
1646 fi.setFile(QString::fromLocal8Bit(p.path), QString::fromLocal8Bit(metaDataFile));
1647 // try again, maybe there's a file later in the include paths with the same name
1648 if (fi.isDir()) {
1649 fi = QFileInfo();
1650 continue;
1651 }
1652 }
1653 if (!fi.exists()) {
1654 const QByteArray msg = "Plugin Metadata file " + lexemView()
1655 + " does not exist. Declaration will be ignored";
1656 error(msg.constData());
1657 return;
1658 }
1659 QFile file(fi.canonicalFilePath());
1660 if (!file.open(QFile::ReadOnly)) {
1661 QByteArray msg = "Plugin Metadata file " + lexemView() + " could not be opened: "
1662 + file.errorString().toUtf8();
1663 error(msg.constData());
1664 return;
1665 }
1666 parsedPluginMetadataFiles.append(fi.canonicalFilePath());
1667 metaData = file.readAll();
1668 }
1669 }
1670
1671 if (!metaData.isEmpty()) {
1672 def->pluginData.metaData = QJsonDocument::fromJson(metaData);
1673 if (!def->pluginData.metaData.isObject()) {
1674 const QByteArray msg = "Plugin Metadata file " + lexemView()
1675 + " does not contain a valid JSON object. Declaration will be ignored";
1676 warning(msg.constData());
1677 def->pluginData.iid = QByteArray();
1678 def->pluginData.uri = QByteArray();
1679 return;
1680 }
1681 }
1682
1683 mustIncludeQPluginH = true;
1684 next(RPAREN);
1685}
1686
1688{
1689 int nesting = 0;
1690 QByteArray accessor;
1691 while (1) {
1692 Token t = peek();
1693 if (!nesting && (t == RPAREN || t == COMMA))
1694 break;
1695 t = next();
1696 if (t == LPAREN)
1697 ++nesting;
1698 if (t == RPAREN)
1699 --nesting;
1700 accessor += lexemView();
1701 }
1702 return accessor;
1703}
1704
1706{
1707 next(LPAREN);
1708 PropertyDef propDef;
1709 propDef.inPrivateClass = parsePropertyAccessor();
1710
1711 next(COMMA);
1712
1713 createPropertyDef(propDef, int(def->propertyList.size()), mode);
1714
1715 def->propertyList += propDef;
1716}
1717
1718void Moc::parseEnumOrFlag(BaseDef *def, EnumFlags flags)
1719{
1720 next(LPAREN);
1721 QByteArray identifier;
1722 while (test(IDENTIFIER)) {
1723 identifier = lexem();
1724 while (test(SCOPE) && test(IDENTIFIER)) {
1725 identifier += "::";
1726 identifier += lexemView();
1727 }
1728 def->enumDeclarations[identifier] = flags;
1729 }
1730 next(RPAREN);
1731}
1732
1734{
1735 next(LPAREN);
1736 QByteArray flagName, enumName;
1737 while (test(IDENTIFIER)) {
1738 flagName = lexem();
1739 while (test(SCOPE) && test(IDENTIFIER)) {
1740 flagName += "::";
1741 flagName += lexemView();
1742 }
1743 }
1744 next(COMMA);
1745 while (test(IDENTIFIER)) {
1746 enumName = lexem();
1747 while (test(SCOPE) && test(IDENTIFIER)) {
1748 enumName += "::";
1749 enumName += lexemView();
1750 }
1751 }
1752
1753 def->flagAliases.insert(enumName, flagName);
1754 next(RPAREN);
1755}
1756
1758{
1759 bool encounteredQmlMacro = false;
1760 next(LPAREN);
1761 ClassInfoDef infoDef;
1762 next(STRING_LITERAL);
1763 infoDef.name = symbol().unquotedLexem();
1764 if (infoDef.name.startsWith("QML."))
1765 encounteredQmlMacro = true;
1766 next(COMMA);
1767 if (test(STRING_LITERAL)) {
1768 infoDef.value = symbol().unquotedLexem();
1769 } else if (test(Q_REVISION_TOKEN)) {
1770 infoDef.value = QByteArray::number(parseRevision().toEncodedVersion<quint16>());
1771 } else {
1772 // support Q_CLASSINFO("help", QT_TR_NOOP("blah"))
1773 next(IDENTIFIER);
1774 next(LPAREN);
1775 next(STRING_LITERAL);
1776 infoDef.value = symbol().unquotedLexem();
1777 next(RPAREN);
1778 }
1779 next(RPAREN);
1780 def->classInfoList += infoDef;
1781 return encounteredQmlMacro ? EncounteredQmlMacro::Yes : EncounteredQmlMacro::No;
1782}
1783
1785{
1786 if (parseClassInfo(static_cast<BaseDef *>(def)) == EncounteredQmlMacro::Yes)
1788}
1789
1791{
1792 next(LPAREN);
1793 while (test(IDENTIFIER)) {
1794 QList<ClassDef::Interface> iface;
1795 iface += ClassDef::Interface(lexem());
1796 while (test(SCOPE)) {
1797 iface.last().className += lexemView();
1798 next(IDENTIFIER);
1799 iface.last().className += lexemView();
1800 }
1801 while (test(COLON)) {
1802 next(IDENTIFIER);
1803 iface += ClassDef::Interface(lexem());
1804 while (test(SCOPE)) {
1805 iface.last().className += lexemView();
1806 next(IDENTIFIER);
1807 iface.last().className += lexemView();
1808 }
1809 }
1810 // resolve from classnames to interface ids
1811 for (qsizetype i = 0; i < iface.size(); ++i) {
1812 const QByteArray iid = interface2IdMap.value(iface.at(i).className);
1813 if (iid.isEmpty())
1814 error("Undefined interface");
1815
1816 iface[i].interfaceId = iid;
1817 }
1818 def->interfaceList += iface;
1819 }
1820 next(RPAREN);
1821}
1822
1824{
1825 next(LPAREN);
1826 QByteArray interface;
1827 next(IDENTIFIER);
1828 interface += lexemView();
1829 while (test(SCOPE)) {
1830 interface += lexemView();
1831 next(IDENTIFIER);
1832 interface += lexemView();
1833 }
1834 next(COMMA);
1835 QByteArray iid;
1836 if (test(STRING_LITERAL)) {
1837 iid = lexem();
1838 } else {
1839 next(IDENTIFIER);
1840 iid = lexem();
1841 }
1842 interface2IdMap.insert(interface, iid);
1843 next(RPAREN);
1844}
1845
1847{
1848 next(LPAREN);
1849 QByteArray typeName = lexemUntil(RPAREN);
1850 typeName.remove(0, 1);
1851 typeName.chop(1);
1852 metaTypes.append(typeName);
1853}
1854
1856{
1857 next(LPAREN);
1858 QByteArray include = lexemUntil(RPAREN);
1859 // remove parentheses
1860 include.remove(0, 1);
1861 include.chop(1);
1862 includeFiles.append(include);
1863}
1864
1866{
1867 next(LPAREN);
1868 FunctionDef funcDef;
1869 next(IDENTIFIER);
1870 funcDef.inPrivateClass = lexem();
1871 // also allow void functions
1872 if (test(LPAREN)) {
1873 next(RPAREN);
1874 funcDef.inPrivateClass += "()";
1875 }
1876 next(COMMA);
1877 funcDef.access = access;
1878 parseFunction(&funcDef, true);
1879 def->slotList += funcDef;
1880 handleDefaultArguments(&def->slotList, funcDef);
1881 if (funcDef.revision > 0)
1883
1884}
1885
1887{
1888 qsizetype from = index;
1889 until(target);
1890 QByteArray s;
1891 while (from <= index) {
1892 QByteArray n = symbols.at(from++-1).lexem();
1893 if (s.size() && n.size()) {
1894 char prev = s.at(s.size()-1);
1895 char next = n.at(0);
1896 if ((is_ident_char(prev) && is_ident_char(next))
1897 || (prev == '<' && next == ':')
1898 || (prev == '>' && next == '>'))
1899 s += ' ';
1900 }
1901 s += n;
1902 }
1903 return s;
1904}
1905
1906bool Moc::until(Token target) {
1907 int braceCount = 0;
1908 int brackCount = 0;
1909 int parenCount = 0;
1910 int angleCount = 0;
1911 if (index) {
1912 switch(symbols.at(index-1).token) {
1913 case LBRACE: ++braceCount; break;
1914 case LBRACK: ++brackCount; break;
1915 case LPAREN: ++parenCount; break;
1916 case LANGLE: ++angleCount; break;
1917 default: break;
1918 }
1919 }
1920
1921 //when searching commas within the default argument, we should take care of template depth (anglecount)
1922 // unfortunately, we do not have enough semantic information to know if '<' is the operator< or
1923 // the beginning of a template type. so we just use heuristics.
1924 qsizetype possible = -1;
1925
1926 while (index < symbols.size()) {
1927 Token t = symbols.at(index++).token;
1928 switch (t) {
1929 case LBRACE: ++braceCount; break;
1930 case RBRACE: --braceCount; break;
1931 case LBRACK: ++brackCount; break;
1932 case RBRACK: --brackCount; break;
1933 case LPAREN: ++parenCount; break;
1934 case RPAREN: --parenCount; break;
1935 case LANGLE:
1936 if (parenCount == 0 && braceCount == 0)
1937 ++angleCount;
1938 break;
1939 case RANGLE:
1940 if (parenCount == 0 && braceCount == 0)
1941 --angleCount;
1942 break;
1943 case GTGT:
1944 if (parenCount == 0 && braceCount == 0) {
1945 angleCount -= 2;
1946 t = RANGLE;
1947 }
1948 break;
1949 default: break;
1950 }
1951 if (t == target
1952 && braceCount <= 0
1953 && brackCount <= 0
1954 && parenCount <= 0
1955 && (target != RANGLE || angleCount <= 0)) {
1956 if (target != COMMA || angleCount <= 0)
1957 return true;
1958 possible = index;
1959 }
1960
1961 if (target == COMMA && t == EQ && possible != -1) {
1962 index = possible;
1963 return true;
1964 }
1965
1966 if (braceCount < 0 || brackCount < 0 || parenCount < 0
1967 || (target == RANGLE && angleCount < 0)) {
1968 --index;
1969 break;
1970 }
1971
1972 if (braceCount <= 0 && t == SEMIC) {
1973 // Abort on semicolon. Allow recovering bad template parsing (QTBUG-31218)
1974 break;
1975 }
1976 }
1977
1978 if (target == COMMA && angleCount != 0 && possible != -1) {
1979 index = possible;
1980 return true;
1981 }
1982
1983 return false;
1984}
1985
1987{
1988 Q_ASSERT(!def->superclassList.isEmpty());
1989 const QByteArray &firstSuperclass = def->superclassList.at(0).classname;
1990
1991 if (!knownQObjectClasses.contains(firstSuperclass)) {
1992 // enable once we /require/ include paths
1993#if 0
1994 const QByteArray msg
1995 = "Class "
1996 + def->className
1997 + " contains the Q_OBJECT macro and inherits from "
1998 + def->superclassList.value(0)
1999 + " but that is not a known QObject subclass. You may get compilation errors.";
2000 warning(msg.constData());
2001#endif
2002 return;
2003 }
2004
2005 auto isRegisteredInterface = [&def](QByteArrayView super) {
2006 auto matchesSuperClass = [&super](const auto &ifaces) {
2007 return !ifaces.isEmpty() && ifaces.first().className == super;
2008 };
2009 return std::any_of(def->interfaceList.cbegin(), def->interfaceList.cend(), matchesSuperClass);
2010 };
2011
2012 const auto end = def->superclassList.cend();
2013 auto it = def->superclassList.cbegin() + 1;
2014 for (; it != end; ++it) {
2015 const QByteArray &superClass = it->classname;
2016 if (knownQObjectClasses.contains(superClass)) {
2017 const QByteArray msg
2018 = "Class "
2019 + def->classname
2020 + " inherits from two QObject subclasses "
2021 + firstSuperclass
2022 + " and "
2023 + superClass
2024 + ". This is not supported!";
2025 warning(msg.constData());
2026 }
2027
2028 if (interface2IdMap.contains(superClass)) {
2029 if (!isRegisteredInterface(superClass)) {
2030 const QByteArray msg
2031 = "Class "
2032 + def->classname
2033 + " implements the interface "
2034 + superClass
2035 + " but does not list it in Q_INTERFACES. qobject_cast to "
2036 + superClass
2037 + " will not work!";
2038 warning(msg.constData());
2039 }
2040 }
2041 }
2042}
2043
2045{
2046 //
2047 // specify get function, for compatibility we accept functions
2048 // returning pointers, or const char * for QByteArray.
2049 //
2050 QDuplicateTracker<QByteArray> definedProperties(cdef->propertyList.size());
2051 auto hasNoAttributes = [&](const PropertyDef &p) {
2052 if (definedProperties.hasSeen(p.name)) {
2053 QByteArray msg = "The property '" + p.name + "' is defined multiple times in class " + cdef->classname + ".";
2054 warning(msg.constData());
2055 }
2056
2057 if (p.read.isEmpty() && p.member.isEmpty() && p.bind.isEmpty()) {
2058 QByteArray msg = "Property declaration " + p.name + " has neither an associated QProperty<> member"
2059 ", nor a READ accessor function nor an associated MEMBER variable. The property will be invalid.";
2060 const auto &sym = p.location >= 0 ? symbolAt(p.location) : Symbol();
2061 warning(sym, msg.constData());
2062 if (p.write.isEmpty())
2063 return true;
2064 }
2065 return false;
2066 };
2067 cdef->propertyList.removeIf(hasNoAttributes);
2068
2069 for (PropertyDef &p : cdef->propertyList) {
2070 for (const FunctionDef &f : std::as_const(cdef->publicList)) {
2071 if (f.name != p.read)
2072 continue;
2073 if (!f.isConst) // get functions must be const
2074 continue;
2075 if (f.arguments.size()) // and must not take any arguments
2076 continue;
2077 PropertyDef::Specification spec = PropertyDef::ValueSpec;
2078 QByteArray tmp = f.normalizedType;
2079 if (p.type == "QByteArray" && tmp == "const char *")
2080 tmp = "QByteArray";
2081 if (tmp.left(6) == "const ")
2082 tmp = tmp.mid(6);
2083 if (p.type != tmp && tmp.endsWith('*')) {
2084 tmp.chop(1);
2085 spec = PropertyDef::PointerSpec;
2086 } else if (f.type.name.endsWith('&')) { // raw type, not normalized type
2087 spec = PropertyDef::ReferenceSpec;
2088 }
2089 if (p.type != tmp)
2090 continue;
2091 p.gspec = spec;
2092 break;
2093 }
2094 if (!p.notify.isEmpty()) {
2095 int notifyId = -1;
2096 for (int j = 0; j < int(cdef->signalList.size()); ++j) {
2097 const FunctionDef &f = cdef->signalList.at(j);
2098 if (f.name != p.notify) {
2099 continue;
2100 } else {
2101 notifyId = j /* Signal indexes start from 0 */;
2102 break;
2103 }
2104 }
2105 p.notifyId = notifyId;
2106 if (notifyId == -1) {
2107 const int index = int(cdef->nonClassSignalList.indexOf(p.notify));
2108 if (index == -1) {
2109 cdef->nonClassSignalList << p.notify;
2110 p.notifyId = int(-1 - cdef->nonClassSignalList.size());
2111 } else {
2112 p.notifyId = int(-2 - index);
2113 }
2114 }
2115 }
2116 }
2117}
2118
2119QJsonObject ClassDef::toJson() const
2120{
2121 QJsonObject cls;
2122 cls["className"_L1] = QString::fromUtf8(classname.constData());
2123 cls["qualifiedClassName"_L1] = QString::fromUtf8(qualified.constData());
2124 cls["lineNumber"_L1] = lineNumber;
2125 if (isFinal)
2126 cls["final"_L1] = true;
2127
2128 QJsonArray classInfos;
2129 for (const auto &info: std::as_const(classInfoList)) {
2130 QJsonObject infoJson;
2131 infoJson["name"_L1] = QString::fromUtf8(info.name);
2132 infoJson["value"_L1] = QString::fromUtf8(info.value);
2133 classInfos.append(infoJson);
2134 }
2135
2136 if (classInfos.size())
2137 cls["classInfos"_L1] = classInfos;
2138
2139 int methodIndex = 0;
2140 const auto appendFunctions
2141 = [&cls, &methodIndex](const QString &type, const QList<FunctionDef> &funcs) {
2142 QJsonArray jsonFuncs;
2143
2144 for (const FunctionDef &fdef: funcs)
2145 jsonFuncs.append(fdef.toJson(methodIndex++));
2146
2147 if (!jsonFuncs.isEmpty())
2148 cls[type] = jsonFuncs;
2149 };
2150
2151 // signals, slots, and methods, in this order, follow the same index
2152 appendFunctions("signals"_L1, signalList);
2153 appendFunctions("slots"_L1, slotList);
2154 appendFunctions("methods"_L1, methodList);
2155
2156 // constructors are indexed separately.
2157 methodIndex = 0;
2158 appendFunctions("constructors"_L1, constructorList);
2159
2160 QJsonArray props;
2161
2162 for (const PropertyDef &propDef: std::as_const(propertyList))
2163 props.append(propDef.toJson());
2164
2165 if (!props.isEmpty())
2166 cls["properties"_L1] = props;
2167
2168 if (hasQObject)
2169 cls["object"_L1] = true;
2170 if (hasQGadget)
2171 cls["gadget"_L1] = true;
2172 if (hasQNamespace)
2173 cls["namespace"_L1] = true;
2174
2175 QJsonArray superClasses;
2176
2177 for (const auto &super: std::as_const(superclassList)) {
2178 QJsonObject superCls;
2179 superCls["name"_L1] = QString::fromUtf8(super.classname);
2180 if (super.classname != super.qualified)
2181 superCls["fullyQualifiedName"_L1] = QString::fromUtf8(super.qualified);
2182 FunctionDef::accessToJson(&superCls, super.access);
2183 superClasses.append(superCls);
2184 }
2185
2186 if (!superClasses.isEmpty())
2187 cls["superClasses"_L1] = superClasses;
2188
2189 QJsonArray enums;
2190 for (const EnumDef &enumDef: std::as_const(enumList))
2191 enums.append(enumDef.toJson(*this));
2192 if (!enums.isEmpty())
2193 cls["enums"_L1] = enums;
2194
2195 QJsonArray ifaces;
2196 for (const QList<Interface> &ifaceList : interfaceList) {
2197 QJsonArray jsonList;
2198 for (const Interface &iface: ifaceList) {
2199 QJsonObject ifaceJson;
2200 ifaceJson["id"_L1] = QString::fromUtf8(iface.interfaceId);
2201 ifaceJson["className"_L1] = QString::fromUtf8(iface.className);
2202 jsonList.append(ifaceJson);
2203 }
2204 ifaces.append(jsonList);
2205 }
2206 if (!ifaces.isEmpty())
2207 cls["interfaces"_L1] = ifaces;
2208
2209 return cls;
2210}
2211
2212QJsonObject FunctionDef::toJson(int index) const
2213{
2214 QJsonObject fdef;
2215 fdef["name"_L1] = QString::fromUtf8(name);
2216 fdef["index"_L1] = index;
2217 if (!tag.isEmpty())
2218 fdef["tag"_L1] = QString::fromUtf8(tag);
2219 fdef["returnType"_L1] = QString::fromUtf8(normalizedType);
2220 if (isConst)
2221 fdef["isConst"_L1] = true;
2222
2223 QJsonArray args;
2224 for (const ArgumentDef &arg: arguments)
2225 args.append(arg.toJson());
2226
2227 if (!args.isEmpty())
2228 fdef["arguments"_L1] = args;
2229
2231
2232 if (revision > 0)
2233 fdef["revision"_L1] = revision;
2234 fdef["lineNumber"_L1] = lineNumber;
2235
2236 if (wasCloned)
2237 fdef["isCloned"_L1] = true;
2238
2239 return fdef;
2240}
2241
2242void FunctionDef::accessToJson(QJsonObject *obj, FunctionDef::Access acs)
2243{
2244 switch (acs) {
2245 case Private: (*obj)["access"_L1] = "private"_L1; break;
2246 case Public: (*obj)["access"_L1] = "public"_L1; break;
2247 case Protected: (*obj)["access"_L1] = "protected"_L1; break;
2248 }
2249}
2250
2251QJsonObject ArgumentDef::toJson() const
2252{
2253 QJsonObject arg;
2254 arg["type"_L1] = QString::fromUtf8(normalizedType);
2255 if (!name.isEmpty())
2256 arg["name"_L1] = QString::fromUtf8(name);
2257 return arg;
2258}
2259
2260QJsonObject PropertyDef::toJson() const
2261{
2262 QJsonObject prop;
2263 prop["name"_L1] = QString::fromUtf8(name);
2264 prop["type"_L1] = QString::fromUtf8(type);
2265
2266 const auto jsonify = [&prop](const char *str, const QByteArray &member) {
2267 if (!member.isEmpty())
2268 prop[QLatin1StringView(str)] = QString::fromUtf8(member);
2269 };
2270
2271 jsonify("member", member);
2272 jsonify("read", read);
2273 jsonify("write", write);
2274 jsonify("bindable", bind);
2275 jsonify("reset", reset);
2276 jsonify("notify", notify);
2277 jsonify("privateClass", inPrivateClass);
2278
2279 const auto jsonifyBoolOrString = [&prop](const char *str, const QByteArray &boolOrString) {
2280 QJsonValue value;
2281 if (boolOrString == "true")
2282 value = true;
2283 else if (boolOrString == "false")
2284 value = false;
2285 else
2286 value = QString::fromUtf8(boolOrString); // function name to query at run-time
2287 prop[QLatin1StringView(str)] = value;
2288 };
2289
2290 jsonifyBoolOrString("designable", designable);
2291 jsonifyBoolOrString("scriptable", scriptable);
2292 jsonifyBoolOrString("stored", stored);
2293 jsonifyBoolOrString("user", user);
2294
2295 prop["constant"_L1] = constant;
2296 prop["final"_L1] = final;
2297 prop["virtual"_L1] = virtual_;
2298 prop["override"_L1] = override;
2299 prop["required"_L1] = required;
2300 prop["index"_L1] = relativeIndex;
2301 prop["lineNumber"_L1] = lineNumber;
2302 if (revision > 0)
2303 prop["revision"_L1] = revision;
2304
2305 return prop;
2306}
2307
2308QJsonObject EnumDef::toJson(const ClassDef &cdef) const
2309{
2310 QJsonObject def;
2311 uint flags = this->flags | cdef.enumDeclarations.value(name);
2312 def["name"_L1] = QString::fromUtf8(name);
2313 def["lineNumber"_L1] = lineNumber;
2314 if (!enumName.isEmpty())
2315 def["alias"_L1] = QString::fromUtf8(enumName);
2316 if (!type.isEmpty())
2317 def["type"_L1] = QString::fromUtf8(type);
2318 def["isFlag"_L1] = (flags & EnumIsFlag) != 0;
2319 def["isClass"_L1] = (flags & EnumIsScoped) != 0;
2320
2321 QJsonArray valueArr;
2322 for (const QByteArray &value: values)
2323 valueArr.append(QString::fromUtf8(value));
2324 if (!valueArr.isEmpty())
2325 def["values"_L1] = valueArr;
2326
2327 return def;
2328}
2329
2331{
2332 if (name == cdef->classname) {
2333 // The name of the enclosing namespace is the same as the enum class name
2334 if (cdef->qualified.contains("::")) {
2335 // QTBUG-112996, fully qualify by using cdef->qualified to disambiguate enum
2336 // class name and enclosing namespace, e.g.:
2337 // namespace A { namespace B { Q_NAMESPACE; enum class B { }; Q_ENUM_NS(B) } }
2338 return cdef->qualified % "::" % name;
2339 } else {
2340 // Just "B"; otherwise the compiler complains about the type "B::B" inside
2341 // "B::staticMetaObject" in the generated code; e.g.:
2342 // namespace B { Q_NAMESPACE; enum class B { }; Q_ENUM_NS(B) }
2343 return name;
2344 }
2345 }
2346 return cdef->classname % "::" % name;
2347}
2348
2349QT_END_NAMESPACE
Definition moc.h:226
bool testFunctionAttribute(Token tok, FunctionDef *def)
Definition moc.cpp:359
void parseEnumOrFlag(BaseDef *def, QtMocConstants::EnumFlags flags)
Definition moc.cpp:1718
void checkSuperClasses(ClassDef *def)
Definition moc.cpp:1986
void parse()
Definition moc.cpp:702
bool parseEnum(EnumDef *def, ClassDef *containingClass)
Definition moc.cpp:250
void parseProperty(ClassDef *def, PropertyMode mode)
Definition moc.cpp:1613
QByteArray lexemUntil(Token)
Definition moc.cpp:1886
EncounteredQmlMacro parseClassInfo(BaseDef *def)
Definition moc.cpp:1757
bool until(Token)
Definition moc.cpp:1906
void createPropertyDef(PropertyDef &def, int propertyIndex, PropertyMode mode)
Definition moc.cpp:1413
void parseClassInfo(ClassDef *def)
Definition moc.cpp:1784
QByteArrayView strippedFileName() const
Definition moc.cpp:1138
bool parseClassHead(ClassDef *def)
Definition moc.cpp:39
void parsePrivateProperty(ClassDef *def, PropertyMode mode)
Definition moc.cpp:1705
void parseFlag(BaseDef *def)
Definition moc.cpp:1733
void parseSignals(ClassDef *def)
Definition moc.cpp:1369
void parseDeclareMetatype()
Definition moc.cpp:1846
void parsePropertyAttributes(PropertyDef &propDef)
Definition moc.cpp:1452
void parseInterfaces(ClassDef *def)
Definition moc.cpp:1790
bool testFunctionRevision(FunctionDef *def)
Definition moc.cpp:422
bool testFunctionAttribute(FunctionDef *def)
Definition moc.cpp:350
void prependNamespaces(BaseDef &def, const QList< NamespaceDef > &namespaceList) const
Definition moc.cpp:656
bool parseFunction(FunctionDef *def, bool inMacro=false)
Definition moc.cpp:434
QByteArray parsePropertyAccessor()
Definition moc.cpp:1687
bool testForFunctionModifiers(FunctionDef *def)
Definition moc.cpp:538
const QByteArray & toFullyQualified(const QByteArray &name) const noexcept
Definition moc.cpp:30
void parseSlots(ClassDef *def, FunctionDef::Access access)
Definition moc.cpp:1327
void parseDeclareInterface()
Definition moc.cpp:1823
void checkListSizes(const ClassDef &def)
Definition moc.cpp:666
QTypeRevision parseRevision()
Definition moc.cpp:391
void parseMocInclude()
Definition moc.cpp:1855
bool skipCxxAttributes()
Definition moc.cpp:382
PropertyMode
Definition moc.h:228
@ Anonymous
Definition moc.h:228
@ Named
Definition moc.h:228
bool mustIncludeQPluginH
Definition moc.h:237
bool parseMaybeFunction(const ClassDef *cdef, FunctionDef *def)
Definition moc.cpp:546
void checkProperties(ClassDef *cdef)
Definition moc.cpp:2044
Type parseType()
Definition moc.cpp:130
void parseFunctionArguments(FunctionDef *def)
Definition moc.cpp:310
void parseSlotInPrivate(ClassDef *def, FunctionDef::Access access)
Definition moc.cpp:1865
bool inClass(const ClassDef *def) const
Definition moc.h:255
void parsePluginData(ClassDef *def)
Definition moc.cpp:1623
void generate(FILE *out, FILE *jsonOutput)
Definition moc.cpp:1215
bool noInclude
Definition moc.h:236
EncounteredQmlMacro
Definition moc.h:284
bool inNamespace(const NamespaceDef *def) const
Definition moc.h:259
\inmodule QtCore\reentrant
Definition qlist.h:81
\inmodule QtCore
constexpr QTypeRevision()=default
Produces an invalid revision.
static bool any_arg_contains(const QList< FunctionDef > &functions, const QByteArray &pattern)
Definition moc.cpp:1159
static QByteArrayList requiredQtContainers(const QList< ClassDef > &classes)
Definition moc.cpp:1184
static QByteArray normalizeType(const QByteArray &ba)
Definition moc.cpp:25
IncludeState
Definition moc.cpp:244
@ IncludeBegin
Definition moc.cpp:245
static QByteArrayList make_candidates()
Definition moc.cpp:1170
static bool any_type_contains(const QList< PropertyDef > &properties, const QByteArray &pattern)
Definition moc.cpp:1150
void handleDefaultArguments(QList< FunctionDef > *functionList, FunctionDef &function)
Definition moc.cpp:644
TypeTag
Definition moc.h:23
@ HasEnum
Definition moc.h:27
@ HasClass
Definition moc.h:26
@ HasStruct
Definition moc.h:25
@ mocOutputRevision
bool is_ident_char(char s)
Definition utils.h:30
QJsonObject toJson() const
Definition moc.cpp:2251
bool isDefault
Definition moc.h:70
Definition moc.h:162
bool hasQObject
Definition moc.h:208
bool hasQGadget
Definition moc.h:209
bool isFinal
Definition moc.h:212
bool requireCompleteMethodTypes
Definition moc.h:211
int revisionedMethods
Definition moc.h:206
QJsonObject toJson() const
Definition moc.cpp:2119
Definition moc.h:53
QByteArray qualifiedType(const ClassDef *cdef) const
Definition moc.cpp:2330
QJsonObject toJson(const ClassDef &cdef) const
Definition moc.cpp:2308
int lineNumber
Definition moc.h:61
bool isVirtual
Definition moc.h:91
bool isScriptable
Definition moc.h:100
bool wasCloned
Definition moc.h:94
QJsonObject toJson(int index) const
Definition moc.cpp:2212
bool isCompat
Definition moc.h:98
bool inlineCode
Definition moc.h:93
bool isRawSlot
Definition moc.h:107
static void accessToJson(QJsonObject *obj, Access acs)
Definition moc.cpp:2242
Access access
Definition moc.h:86
bool isSignal
Definition moc.h:102
bool isSlot
Definition moc.h:101
bool isInvokable
Definition moc.h:99
bool isConst
Definition moc.h:90
int lineNumber
Definition moc.h:88
int revision
Definition moc.h:87
bool isAbstract
Definition moc.h:106
bool isPrivateSignal
Definition moc.h:103
bool isDestructor
Definition moc.h:105
@ Public
Definition moc.h:85
@ Protected
Definition moc.h:85
@ Private
Definition moc.h:85
bool isConstructor
Definition moc.h:104
bool isStatic
Definition moc.h:92
bool hasQNamespace
Definition moc.h:220
int lineNumber
Definition moc.h:137
bool final
Definition moc.h:132
QJsonObject toJson() const
Definition moc.cpp:2260
bool constant
Definition moc.h:131
bool override
Definition moc.h:134
bool virtual_
Definition moc.h:133
int revision
Definition moc.h:129
int relativeIndex
Definition moc.h:136
bool required
Definition moc.h:135
Token token
Definition symbols.h:58
Symbol()=default