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