1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
3 | |
4 | #ifndef MOC_H |
5 | #define MOC_H |
6 | |
7 | #include "parser.h" |
8 | #include <qstringlist.h> |
9 | #include <qmap.h> |
10 | #include <qpair.h> |
11 | #include <qjsondocument.h> |
12 | #include <qjsonarray.h> |
13 | #include <qjsonobject.h> |
14 | #include <qversionnumber.h> |
15 | #include <stdio.h> |
16 | |
17 | #include <private/qtools_p.h> |
18 | |
19 | QT_BEGIN_NAMESPACE |
20 | |
21 | struct QMetaObject; |
22 | |
23 | struct Type |
24 | { |
25 | enum ReferenceType { NoReference, Reference, RValueReference, Pointer }; |
26 | |
27 | inline Type() : isVolatile(false), isScoped(false), firstToken(NOTOKEN), referenceType(NoReference) {} |
28 | inline explicit Type(const QByteArray &_name) |
29 | : name(_name), rawName(name), isVolatile(false), isScoped(false), firstToken(NOTOKEN), referenceType(NoReference) {} |
30 | QByteArray name; |
31 | //When used as a return type, the type name may be modified to remove the references. |
32 | // rawName is the type as found in the function signature |
33 | QByteArray rawName; |
34 | uint isVolatile : 1; |
35 | uint isScoped : 1; |
36 | Token firstToken; |
37 | ReferenceType referenceType; |
38 | }; |
39 | Q_DECLARE_TYPEINFO(Type, Q_RELOCATABLE_TYPE); |
40 | |
41 | struct ClassDef; |
42 | struct EnumDef |
43 | { |
44 | QByteArray name; |
45 | QByteArray enumName; |
46 | QByteArray type; |
47 | QList<QByteArray> values; |
48 | bool isEnumClass; // c++11 enum class |
49 | EnumDef() : isEnumClass(false) {} |
50 | QJsonObject toJson(const ClassDef &cdef) const; |
51 | QByteArray qualifiedType(const ClassDef *cdef) const; |
52 | }; |
53 | Q_DECLARE_TYPEINFO(EnumDef, Q_RELOCATABLE_TYPE); |
54 | |
55 | struct ArgumentDef |
56 | { |
57 | ArgumentDef() : isDefault(false) {} |
58 | Type type; |
59 | QByteArray rightType, normalizedType, name; |
60 | QByteArray typeNameForCast; // type name to be used in cast from void * in metacall |
61 | bool isDefault; |
62 | |
63 | QJsonObject toJson() const; |
64 | }; |
65 | Q_DECLARE_TYPEINFO(ArgumentDef, Q_RELOCATABLE_TYPE); |
66 | |
67 | struct FunctionDef |
68 | { |
69 | Type type; |
70 | QList<ArgumentDef> arguments; |
71 | QByteArray normalizedType; |
72 | QByteArray tag; |
73 | QByteArray name; |
74 | QByteArray inPrivateClass; |
75 | |
76 | enum Access { Private, Protected, Public }; |
77 | Access access = Private; |
78 | int revision = 0; |
79 | |
80 | bool isConst = false; |
81 | bool isVirtual = false; |
82 | bool isStatic = false; |
83 | bool inlineCode = false; |
84 | bool wasCloned = false; |
85 | |
86 | bool returnTypeIsVolatile = false; |
87 | |
88 | bool isCompat = false; |
89 | bool isInvokable = false; |
90 | bool isScriptable = false; |
91 | bool isSlot = false; |
92 | bool isSignal = false; |
93 | bool isPrivateSignal = false; |
94 | bool isConstructor = false; |
95 | bool isDestructor = false; |
96 | bool isAbstract = false; |
97 | bool isRawSlot = false; |
98 | |
99 | QJsonObject toJson() const; |
100 | static void accessToJson(QJsonObject *obj, Access acs); |
101 | }; |
102 | Q_DECLARE_TYPEINFO(FunctionDef, Q_RELOCATABLE_TYPE); |
103 | |
104 | struct PropertyDef |
105 | { |
106 | bool stdCppSet() const { |
107 | if (name.isEmpty()) |
108 | return false; |
109 | QByteArray s("set" ); |
110 | s += QtMiscUtils::toAsciiUpper(ch: name[0]); |
111 | s += name.mid(index: 1); |
112 | return (s == write); |
113 | } |
114 | |
115 | QByteArray name, type, member, read, write, bind, reset, designable, scriptable, stored, user, notify, inPrivateClass; |
116 | int notifyId = -1; // -1 means no notifyId, >= 0 means signal defined in this class, < -1 means signal not defined in this class |
117 | enum Specification { ValueSpec, ReferenceSpec, PointerSpec }; |
118 | Specification gspec = ValueSpec; |
119 | int revision = 0; |
120 | bool constant = false; |
121 | bool final = false; |
122 | bool required = false; |
123 | int relativeIndex = -1; // property index in current metaobject |
124 | |
125 | qsizetype location = -1; // token index, used for error reporting |
126 | |
127 | QJsonObject toJson() const; |
128 | }; |
129 | Q_DECLARE_TYPEINFO(PropertyDef, Q_RELOCATABLE_TYPE); |
130 | |
131 | struct PrivateQPropertyDef |
132 | { |
133 | Type type; |
134 | QByteArray name; |
135 | QByteArray setter; |
136 | QByteArray accessor; |
137 | QByteArray storage; |
138 | }; |
139 | Q_DECLARE_TYPEINFO(PrivateQPropertyDef, Q_RELOCATABLE_TYPE); |
140 | |
141 | struct ClassInfoDef |
142 | { |
143 | QByteArray name; |
144 | QByteArray value; |
145 | }; |
146 | Q_DECLARE_TYPEINFO(ClassInfoDef, Q_RELOCATABLE_TYPE); |
147 | |
148 | struct BaseDef { |
149 | QByteArray classname; |
150 | QByteArray qualified; |
151 | QList<ClassInfoDef> classInfoList; |
152 | QMap<QByteArray, bool> enumDeclarations; |
153 | QList<EnumDef> enumList; |
154 | QMap<QByteArray, QByteArray> flagAliases; |
155 | qsizetype begin = 0; |
156 | qsizetype end = 0; |
157 | }; |
158 | |
159 | struct ClassDef : BaseDef { |
160 | QList<QPair<QByteArray, FunctionDef::Access>> superclassList; |
161 | |
162 | struct Interface |
163 | { |
164 | Interface() { } // for QList, don't use |
165 | inline explicit Interface(const QByteArray &_className) |
166 | : className(_className) {} |
167 | QByteArray className; |
168 | QByteArray interfaceId; |
169 | }; |
170 | QList<QList<Interface>> interfaceList; |
171 | |
172 | struct PluginData { |
173 | QByteArray iid; |
174 | QByteArray uri; |
175 | QMap<QString, QJsonArray> metaArgs; |
176 | QJsonDocument metaData; |
177 | } pluginData; |
178 | |
179 | QList<FunctionDef> constructorList; |
180 | QList<FunctionDef> signalList, slotList, methodList, publicList; |
181 | QList<QByteArray> nonClassSignalList; |
182 | QList<PropertyDef> propertyList; |
183 | int revisionedMethods = 0; |
184 | |
185 | bool hasQObject = false; |
186 | bool hasQGadget = false; |
187 | bool hasQNamespace = false; |
188 | bool requireCompleteMethodTypes = false; |
189 | |
190 | QJsonObject toJson() const; |
191 | }; |
192 | Q_DECLARE_TYPEINFO(ClassDef, Q_RELOCATABLE_TYPE); |
193 | Q_DECLARE_TYPEINFO(ClassDef::Interface, Q_RELOCATABLE_TYPE); |
194 | |
195 | struct NamespaceDef : BaseDef { |
196 | bool hasQNamespace = false; |
197 | bool doGenerate = false; |
198 | }; |
199 | Q_DECLARE_TYPEINFO(NamespaceDef, Q_RELOCATABLE_TYPE); |
200 | |
201 | class Moc : public Parser |
202 | { |
203 | public: |
204 | enum PropertyMode { Named, Anonymous }; |
205 | |
206 | Moc() |
207 | : noInclude(false), mustIncludeQPluginH(false), requireCompleteTypes(false) |
208 | {} |
209 | |
210 | QByteArray filename; |
211 | |
212 | bool noInclude; |
213 | bool mustIncludeQPluginH; |
214 | bool requireCompleteTypes; |
215 | QByteArray includePath; |
216 | QList<QByteArray> includeFiles; |
217 | QList<ClassDef> classList; |
218 | QMap<QByteArray, QByteArray> interface2IdMap; |
219 | QList<QByteArray> metaTypes; |
220 | // map from class name to fully qualified name |
221 | QHash<QByteArray, QByteArray> knownQObjectClasses; |
222 | QHash<QByteArray, QByteArray> knownGadgets; |
223 | QMap<QString, QJsonArray> metaArgs; |
224 | QList<QString> parsedPluginMetadataFiles; |
225 | |
226 | void parse(); |
227 | void generate(FILE *out, FILE *jsonOutput); |
228 | |
229 | bool parseClassHead(ClassDef *def); |
230 | inline bool inClass(const ClassDef *def) const { |
231 | return index > def->begin && index < def->end - 1; |
232 | } |
233 | |
234 | inline bool inNamespace(const NamespaceDef *def) const { |
235 | return index > def->begin && index < def->end - 1; |
236 | } |
237 | |
238 | void prependNamespaces(BaseDef &def, const QList<NamespaceDef> &namespaceList) const; |
239 | |
240 | Type parseType(); |
241 | |
242 | bool parseEnum(EnumDef *def); |
243 | |
244 | bool parseFunction(FunctionDef *def, bool inMacro = false); |
245 | bool parseMaybeFunction(const ClassDef *cdef, FunctionDef *def); |
246 | |
247 | void parseSlots(ClassDef *def, FunctionDef::Access access); |
248 | void parseSignals(ClassDef *def); |
249 | void parseProperty(ClassDef *def, PropertyMode mode); |
250 | void parsePluginData(ClassDef *def); |
251 | |
252 | void createPropertyDef(PropertyDef &def, int propertyIndex, PropertyMode mode); |
253 | |
254 | void parsePropertyAttributes(PropertyDef &propDef); |
255 | void parseEnumOrFlag(BaseDef *def, bool isFlag); |
256 | void parseFlag(BaseDef *def); |
257 | enum class EncounteredQmlMacro {Yes, No}; |
258 | EncounteredQmlMacro parseClassInfo(BaseDef *def); |
259 | void parseClassInfo(ClassDef *def); |
260 | void parseInterfaces(ClassDef *def); |
261 | void parseDeclareInterface(); |
262 | void parseDeclareMetatype(); |
263 | void parseMocInclude(); |
264 | void parseSlotInPrivate(ClassDef *def, FunctionDef::Access access); |
265 | QByteArray parsePropertyAccessor(); |
266 | void parsePrivateProperty(ClassDef *def, PropertyMode mode); |
267 | |
268 | void parseFunctionArguments(FunctionDef *def); |
269 | |
270 | QByteArray lexemUntil(Token); |
271 | bool until(Token); |
272 | |
273 | // test for Q_INVOCABLE, Q_SCRIPTABLE, etc. and set the flags |
274 | // in FunctionDef accordingly |
275 | bool testFunctionAttribute(FunctionDef *def); |
276 | bool testFunctionAttribute(Token tok, FunctionDef *def); |
277 | bool testFunctionRevision(FunctionDef *def); |
278 | QTypeRevision parseRevision(); |
279 | |
280 | bool skipCxxAttributes(); |
281 | |
282 | void checkSuperClasses(ClassDef *def); |
283 | void checkProperties(ClassDef* cdef); |
284 | bool testForFunctionModifiers(FunctionDef *def); |
285 | }; |
286 | |
287 | inline QByteArray noRef(const QByteArray &type) |
288 | { |
289 | if (type.endsWith(c: '&')) { |
290 | if (type.endsWith(bv: "&&" )) |
291 | return type.left(len: type.size()-2); |
292 | return type.left(len: type.size()-1); |
293 | } |
294 | return type; |
295 | } |
296 | |
297 | QT_END_NAMESPACE |
298 | |
299 | #endif // MOC_H |
300 | |