1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include "languageutils_global.h"
29 #include "componentversion.h"
30 
31 #include <QString>
32 #include <QStringList>
33 #include <QList>
34 #include <QHash>
35 #include <QSharedPointer>
36 
37 QT_BEGIN_NAMESPACE
38 class QCryptographicHash;
39 QT_END_NAMESPACE
40 
41 namespace LanguageUtils {
42 
43 class LANGUAGEUTILS_EXPORT FakeMetaEnum {
44     QString m_name;
45     QStringList m_keys;
46     QList<int> m_values;
47 
48 public:
49     FakeMetaEnum();
50     explicit FakeMetaEnum(const QString &name);
51 
52     bool isValid() const;
53 
54     QString name() const;
55     void setName(const QString &name);
56 
57     void addKey(const QString &key, int value);
58     QString key(int index) const;
59     int keyCount() const;
60     QStringList keys() const;
61     bool hasKey(const QString &key) const;
62     void addToHash(QCryptographicHash &hash) const;
63 
64     QString describe(int baseIndent = 0) const;
65     QString toString() const;
66 };
67 
68 class LANGUAGEUTILS_EXPORT FakeMetaMethod {
69 public:
70     enum {
71         Signal,
72         Slot,
73         Method
74     };
75 
76     enum {
77         Private,
78         Protected,
79         Public
80     };
81 
82 public:
83     FakeMetaMethod();
84     explicit FakeMetaMethod(const QString &name, const QString &returnType = QString());
85 
86     QString methodName() const;
87     void setMethodName(const QString &name);
88 
89     void setReturnType(const QString &type);
90 
91     QStringList parameterNames() const;
92     QStringList parameterTypes() const;
93     void addParameter(const QString &name, const QString &type);
94 
95     int methodType() const;
96     void setMethodType(int methodType);
97 
98     int access() const;
99 
100     int revision() const;
101     void setRevision(int r);
102     void addToHash(QCryptographicHash &hash) const;
103 
104     QString describe(int baseIndent = 0) const;
105     QString toString() const;
106 private:
107     QString m_name;
108     QString m_returnType;
109     QStringList m_paramNames;
110     QStringList m_paramTypes;
111     int m_methodTy;
112     int m_methodAccess;
113     int m_revision;
114 };
115 
116 class LANGUAGEUTILS_EXPORT FakeMetaProperty {
117     QString m_propertyName;
118     QString m_type;
119     bool m_isList;
120     bool m_isWritable;
121     bool m_isPointer;
122     int m_revision;
123 
124 public:
125     FakeMetaProperty(const QString &name, const QString &type, bool isList, bool isWritable, bool isPointer, int revision);
126 
127     QString name() const;
128     QString typeName() const;
129 
130     bool isList() const;
131     bool isWritable() const;
132     bool isPointer() const;
133     int revision() const;
134     void addToHash(QCryptographicHash &hash) const;
135 
136     QString describe(int baseIndent = 0) const;
137     QString toString() const;
138 };
139 
140 class LANGUAGEUTILS_EXPORT FakeMetaObject {
141     Q_DISABLE_COPY(FakeMetaObject);
142 
143 public:
144     typedef QSharedPointer<FakeMetaObject> Ptr;
145     typedef QSharedPointer<const FakeMetaObject> ConstPtr;
146 
147     class LANGUAGEUTILS_EXPORT Export {
148     public:
149         Export();
150 
151         QString package;
152         QString type;
153         ComponentVersion version;
154         int metaObjectRevision;
155 
156         bool isValid() const;
157         void addToHash(QCryptographicHash &hash) const;
158 
159         QString describe(int baseIndent = 0) const;
160         QString toString() const;
161     };
162 
163 private:
164     QString m_className;
165     QList<Export> m_exports;
166     QString m_superName;
167     QList<FakeMetaEnum> m_enums;
168     QHash<QString, int> m_enumNameToIndex;
169     QList<FakeMetaProperty> m_props;
170     QHash<QString, int> m_propNameToIdx;
171     QList<FakeMetaMethod> m_methods;
172     QString m_defaultPropertyName;
173     QString m_attachedTypeName;
174     QByteArray m_fingerprint;
175     bool m_isSingleton;
176     bool m_isCreatable;
177     bool m_isComposite;
178 
179 public:
180     FakeMetaObject();
181 
182     QString className() const;
183     void setClassName(const QString &name);
184 
185     void addExport(const QString &name, const QString &package, ComponentVersion version);
186     void setExportMetaObjectRevision(int exportIndex, int metaObjectRevision);
187     QList<Export> exports() const;
188     Export exportInPackage(const QString &package) const;
189 
190     void setSuperclassName(const QString &superclass);
191     QString superclassName() const;
192 
193     void addEnum(const FakeMetaEnum &fakeEnum);
194     int enumeratorCount() const;
195     int enumeratorOffset() const;
196     FakeMetaEnum enumerator(int index) const;
197     int enumeratorIndex(const QString &name) const;
198 
199     void addProperty(const FakeMetaProperty &property);
200     int propertyCount() const;
201     int propertyOffset() const;
202     FakeMetaProperty property(int index) const;
203     int propertyIndex(const QString &name) const;
204 
205     void addMethod(const FakeMetaMethod &method);
206     int methodCount() const;
207     int methodOffset() const;
208     FakeMetaMethod method(int index) const;
209     int methodIndex(const QString &name) const; // Note: Returns any method with that name in case of overloads
210 
211     QString defaultPropertyName() const;
212     void setDefaultPropertyName(const QString &defaultPropertyName);
213 
214     QString attachedTypeName() const;
215     void setAttachedTypeName(const QString &name);
216     QByteArray calculateFingerprint() const;
217     void updateFingerprint();
218     QByteArray fingerprint() const;
219 
220     bool isSingleton() const;
221     bool isCreatable() const;
222     bool isComposite() const;
223     void setIsSingleton(bool value);
224     void setIsCreatable(bool value);
225     void setIsComposite(bool value);
226 
227     QString describe(bool printDetails = true, int baseIndent = 0) const;
228     QString toString() const;
229 };
230 
231 } // namespace LanguageUtils
232