| 1 | // Copyright (C) 2020 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 METATYPESJSONPROCESSOR_P_H | 
|---|
| 5 | #define METATYPESJSONPROCESSOR_P_H | 
|---|
| 6 |  | 
|---|
| 7 | // | 
|---|
| 8 | //  W A R N I N G | 
|---|
| 9 | //  ------------- | 
|---|
| 10 | // | 
|---|
| 11 | // This file is not part of the Qt API.  It exists purely as an | 
|---|
| 12 | // implementation detail.  This header file may change from version to | 
|---|
| 13 | // version without notice, or even be removed. | 
|---|
| 14 | // | 
|---|
| 15 | // We mean it. | 
|---|
| 16 | // | 
|---|
| 17 |  | 
|---|
| 18 | #include <QtCore/qcbormap.h> | 
|---|
| 19 | #include <QtCore/qstring.h> | 
|---|
| 20 | #include <QtCore/qtyperevision.h> | 
|---|
| 21 | #include <QtCore/qvarlengtharray.h> | 
|---|
| 22 | #include <QtCore/qvector.h> | 
|---|
| 23 |  | 
|---|
| 24 | QT_BEGIN_NAMESPACE | 
|---|
| 25 |  | 
|---|
| 26 | // With all the QAnyStringViews in this file we rely on the Cbor data to stay | 
|---|
| 27 | // in place if you don't change the Cbor contents. We assume that Cbor data | 
|---|
| 28 | // is implicitly shared so that merely copying const Cbor objects does not copy | 
|---|
| 29 | // the contents. | 
|---|
| 30 |  | 
|---|
| 31 | enum class Access { Public, Protected, Private }; | 
|---|
| 32 |  | 
|---|
| 33 | struct BaseType | 
|---|
| 34 | { | 
|---|
| 35 | using Container = QVarLengthArray<BaseType, 1>; | 
|---|
| 36 |  | 
|---|
| 37 | BaseType() = default; | 
|---|
| 38 | BaseType(const QCborMap &cbor); | 
|---|
| 39 |  | 
|---|
| 40 | QAnyStringView name; | 
|---|
| 41 | Access access; | 
|---|
| 42 | }; | 
|---|
| 43 |  | 
|---|
| 44 | struct ClassInfo | 
|---|
| 45 | { | 
|---|
| 46 | using Container = std::vector<ClassInfo>; | 
|---|
| 47 |  | 
|---|
| 48 | ClassInfo() = default; | 
|---|
| 49 | ClassInfo(const QCborMap &cbor); | 
|---|
| 50 |  | 
|---|
| 51 | QAnyStringView name; | 
|---|
| 52 | QAnyStringView value; | 
|---|
| 53 | }; | 
|---|
| 54 |  | 
|---|
| 55 | struct Interface | 
|---|
| 56 | { | 
|---|
| 57 | using Container = QVarLengthArray<Interface, 1>; | 
|---|
| 58 |  | 
|---|
| 59 | Interface() = default; | 
|---|
| 60 | Interface(const QCborValue &cbor); | 
|---|
| 61 |  | 
|---|
| 62 | QAnyStringView className; | 
|---|
| 63 | }; | 
|---|
| 64 |  | 
|---|
| 65 | struct Property | 
|---|
| 66 | { | 
|---|
| 67 | using Container = std::vector<Property>; | 
|---|
| 68 |  | 
|---|
| 69 | Property() = default; | 
|---|
| 70 | Property(const QCborMap &cbor); | 
|---|
| 71 |  | 
|---|
| 72 | QAnyStringView name; | 
|---|
| 73 | QAnyStringView type; | 
|---|
| 74 |  | 
|---|
| 75 | QAnyStringView member; | 
|---|
| 76 | QAnyStringView read; | 
|---|
| 77 | QAnyStringView write; | 
|---|
| 78 | QAnyStringView reset; | 
|---|
| 79 | QAnyStringView notify; | 
|---|
| 80 | QAnyStringView bindable; | 
|---|
| 81 |  | 
|---|
| 82 | QAnyStringView privateClass; | 
|---|
| 83 |  | 
|---|
| 84 | int index = -1; | 
|---|
| 85 |  | 
|---|
| 86 | QTypeRevision revision; | 
|---|
| 87 |  | 
|---|
| 88 | bool isFinal = false; | 
|---|
| 89 | bool isConstant = false; | 
|---|
| 90 | bool isRequired = false; | 
|---|
| 91 | }; | 
|---|
| 92 |  | 
|---|
| 93 | struct Argument | 
|---|
| 94 | { | 
|---|
| 95 | using Container = std::vector<Argument>; | 
|---|
| 96 |  | 
|---|
| 97 | Argument() = default; | 
|---|
| 98 | Argument(const QCborMap &cbor); | 
|---|
| 99 |  | 
|---|
| 100 | QAnyStringView name; | 
|---|
| 101 | QAnyStringView type; | 
|---|
| 102 | }; | 
|---|
| 103 |  | 
|---|
| 104 | struct Method | 
|---|
| 105 | { | 
|---|
| 106 | using Container = std::vector<Method>; | 
|---|
| 107 | static constexpr int InvalidIndex = std::numeric_limits<int>::min(); | 
|---|
| 108 |  | 
|---|
| 109 | Method() = default; | 
|---|
| 110 | Method(const QCborMap &cbor, bool isConstructor); | 
|---|
| 111 |  | 
|---|
| 112 | QAnyStringView name; | 
|---|
| 113 |  | 
|---|
| 114 | Argument::Container arguments; | 
|---|
| 115 | QAnyStringView returnType; | 
|---|
| 116 |  | 
|---|
| 117 | int index = InvalidIndex; | 
|---|
| 118 |  | 
|---|
| 119 | QTypeRevision revision; | 
|---|
| 120 |  | 
|---|
| 121 | Access access = Access::Public; | 
|---|
| 122 |  | 
|---|
| 123 | bool isCloned = false; | 
|---|
| 124 | bool isJavaScriptFunction = false; | 
|---|
| 125 | bool isConstructor = false; | 
|---|
| 126 | }; | 
|---|
| 127 |  | 
|---|
| 128 | struct Enum | 
|---|
| 129 | { | 
|---|
| 130 | using Container = std::vector<Enum>; | 
|---|
| 131 |  | 
|---|
| 132 | Enum() = default; | 
|---|
| 133 | Enum(const QCborMap &cbor); | 
|---|
| 134 |  | 
|---|
| 135 | QAnyStringView name; | 
|---|
| 136 | QAnyStringView alias; | 
|---|
| 137 | QAnyStringView type; | 
|---|
| 138 |  | 
|---|
| 139 | QList<QAnyStringView> values; | 
|---|
| 140 |  | 
|---|
| 141 | bool isFlag = false; | 
|---|
| 142 | bool isClass = false; | 
|---|
| 143 | }; | 
|---|
| 144 |  | 
|---|
| 145 | struct MetaTypePrivate | 
|---|
| 146 | { | 
|---|
| 147 | Q_DISABLE_COPY_MOVE(MetaTypePrivate) | 
|---|
| 148 |  | 
|---|
| 149 | enum Kind : quint8 { Object, Gadget, Namespace, Unknown }; | 
|---|
| 150 |  | 
|---|
| 151 | MetaTypePrivate() = default; | 
|---|
| 152 | MetaTypePrivate(const QCborMap &cbor, const QString &inputFile); | 
|---|
| 153 |  | 
|---|
| 154 | const QCborMap cbor; // need to keep this to hold on to the strings | 
|---|
| 155 | const QString inputFile; | 
|---|
| 156 |  | 
|---|
| 157 | QAnyStringView className; | 
|---|
| 158 | QAnyStringView qualifiedClassName; | 
|---|
| 159 | BaseType::Container superClasses; | 
|---|
| 160 | ClassInfo::Container classInfos; | 
|---|
| 161 | Interface::Container ifaces; | 
|---|
| 162 |  | 
|---|
| 163 | Property::Container properties; | 
|---|
| 164 |  | 
|---|
| 165 | Method::Container methods; | 
|---|
| 166 | Method::Container sigs; | 
|---|
| 167 | Method::Container constructors; | 
|---|
| 168 |  | 
|---|
| 169 | Enum::Container enums; | 
|---|
| 170 |  | 
|---|
| 171 | Kind kind = Unknown; | 
|---|
| 172 | int lineNumber = 0; | 
|---|
| 173 | }; | 
|---|
| 174 |  | 
|---|
| 175 | class MetaType | 
|---|
| 176 | { | 
|---|
| 177 | public: | 
|---|
| 178 | using Kind = MetaTypePrivate::Kind; | 
|---|
| 179 |  | 
|---|
| 180 | MetaType() = default; | 
|---|
| 181 | MetaType(const QCborMap &cbor, const QString &inputFile); | 
|---|
| 182 |  | 
|---|
| 183 | bool isEmpty() const { return d == &s_empty; } | 
|---|
| 184 |  | 
|---|
| 185 | QString inputFile() const { return d->inputFile; } | 
|---|
| 186 | int lineNumber() const { return d->lineNumber; } | 
|---|
| 187 | QAnyStringView className() const { return d->className; } | 
|---|
| 188 | QAnyStringView qualifiedClassName() const { return d->qualifiedClassName; } | 
|---|
| 189 | const BaseType::Container &superClasses() const { return d->superClasses; } | 
|---|
| 190 | const ClassInfo::Container &classInfos() const { return d->classInfos; } | 
|---|
| 191 | const Interface::Container &ifaces() const { return d->ifaces; } | 
|---|
| 192 |  | 
|---|
| 193 | const Property::Container &properties() const { return d->properties; } | 
|---|
| 194 | const Method::Container &methods() const { return d->methods; } | 
|---|
| 195 | const Method::Container &sigs() const { return d->sigs; } | 
|---|
| 196 | const Method::Container &constructors() const { return d->constructors; } | 
|---|
| 197 |  | 
|---|
| 198 | const Enum::Container &enums() const { return d->enums; } | 
|---|
| 199 |  | 
|---|
| 200 | Kind kind() const { return d->kind; } | 
|---|
| 201 |  | 
|---|
| 202 | private: | 
|---|
| 203 | friend bool operator==(const MetaType &a, const MetaType &b) noexcept | 
|---|
| 204 | { | 
|---|
| 205 | return a.d == b.d; | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | friend bool operator!=(const MetaType &a, const MetaType &b) noexcept | 
|---|
| 209 | { | 
|---|
| 210 | return !(a == b); | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | static const MetaTypePrivate s_empty; | 
|---|
| 214 | const MetaTypePrivate *d = &s_empty; | 
|---|
| 215 | }; | 
|---|
| 216 |  | 
|---|
| 217 | struct UsingDeclaration { | 
|---|
| 218 | QAnyStringView alias; | 
|---|
| 219 | QAnyStringView original; | 
|---|
| 220 |  | 
|---|
| 221 | bool isValid() const { return !alias.isEmpty() && !original.isEmpty(); } | 
|---|
| 222 | private: | 
|---|
| 223 | friend bool comparesEqual(const UsingDeclaration &a, const UsingDeclaration &b) noexcept | 
|---|
| 224 | { | 
|---|
| 225 | return std::tie(args: a.alias, args: a.original) == std::tie(args: b.alias, args: b.original); | 
|---|
| 226 | } | 
|---|
| 227 |  | 
|---|
| 228 | friend Qt::strong_ordering compareThreeWay( | 
|---|
| 229 | const UsingDeclaration &a, const UsingDeclaration &b) noexcept | 
|---|
| 230 | { | 
|---|
| 231 | return a.alias != b.alias | 
|---|
| 232 | ? compareThreeWay(lhs: a.alias, rhs: b.alias) | 
|---|
| 233 | : compareThreeWay(lhs: a.original, rhs: b.original); | 
|---|
| 234 | } | 
|---|
| 235 | Q_DECLARE_STRONGLY_ORDERED(UsingDeclaration); | 
|---|
| 236 | }; | 
|---|
| 237 |  | 
|---|
| 238 | class MetaTypesJsonProcessor | 
|---|
| 239 | { | 
|---|
| 240 | public: | 
|---|
| 241 | static QList<QAnyStringView> namespaces(const MetaType &classDef); | 
|---|
| 242 |  | 
|---|
| 243 | MetaTypesJsonProcessor(bool privateIncludes) : m_privateIncludes(privateIncludes) {} | 
|---|
| 244 |  | 
|---|
| 245 | bool processTypes(const QStringList &files); | 
|---|
| 246 |  | 
|---|
| 247 | bool processForeignTypes(const QString &foreignTypesFile); | 
|---|
| 248 | bool processForeignTypes(const QStringList &foreignTypesFiles); | 
|---|
| 249 |  | 
|---|
| 250 | void postProcessTypes(); | 
|---|
| 251 | void postProcessForeignTypes(); | 
|---|
| 252 |  | 
|---|
| 253 | QVector<MetaType> types() const { return m_types; } | 
|---|
| 254 | QVector<MetaType> foreignTypes() const { return m_foreignTypes; } | 
|---|
| 255 | QList<QAnyStringView> referencedTypes() const { return m_referencedTypes; } | 
|---|
| 256 | QList<UsingDeclaration> usingDeclarations() const { return m_usingDeclarations; } | 
|---|
| 257 | QList<QString> includes() const { return m_includes; } | 
|---|
| 258 |  | 
|---|
| 259 | QString () const; | 
|---|
| 260 |  | 
|---|
| 261 | private: | 
|---|
| 262 | enum RegistrationMode { | 
|---|
| 263 | NoRegistration, | 
|---|
| 264 | ObjectRegistration, | 
|---|
| 265 | GadgetRegistration, | 
|---|
| 266 | NamespaceRegistration | 
|---|
| 267 | }; | 
|---|
| 268 |  | 
|---|
| 269 | struct PreProcessResult { | 
|---|
| 270 | QList<QAnyStringView> primitiveAliases; | 
|---|
| 271 | UsingDeclaration usingDeclaration; | 
|---|
| 272 | QAnyStringView foreignPrimitive; | 
|---|
| 273 | RegistrationMode mode; | 
|---|
| 274 | }; | 
|---|
| 275 |  | 
|---|
| 276 | enum class PopulateMode { No, Yes }; | 
|---|
| 277 | static PreProcessResult preProcess(const MetaType &classDef, PopulateMode populateMode); | 
|---|
| 278 | void addRelatedTypes(); | 
|---|
| 279 |  | 
|---|
| 280 | void sortTypes(QVector<MetaType> &types); | 
|---|
| 281 | QString resolvedInclude(QAnyStringView include); | 
|---|
| 282 | void processTypes(const QCborMap &types); | 
|---|
| 283 | void processForeignTypes(const QCborMap &types); | 
|---|
| 284 |  | 
|---|
| 285 | bool isPrimitive(QAnyStringView type) const | 
|---|
| 286 | { | 
|---|
| 287 | return std::binary_search(first: m_primitiveTypes.begin(), last: m_primitiveTypes.end(), val: type); | 
|---|
| 288 | } | 
|---|
| 289 |  | 
|---|
| 290 | QList<QString> m_includes; | 
|---|
| 291 | QList<QAnyStringView> m_referencedTypes; | 
|---|
| 292 | QList<QAnyStringView> m_primitiveTypes; | 
|---|
| 293 | QList<UsingDeclaration> m_usingDeclarations; | 
|---|
| 294 | QVector<MetaType> m_types; | 
|---|
| 295 | QVector<MetaType> m_foreignTypes; | 
|---|
| 296 | bool m_privateIncludes = false; | 
|---|
| 297 | }; | 
|---|
| 298 |  | 
|---|
| 299 | QT_END_NAMESPACE | 
|---|
| 300 |  | 
|---|
| 301 | #endif // METATYPESJSONPROCESSOR_P_H | 
|---|
| 302 |  | 
|---|