1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 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 "projectstorageids.h"
29 
30 #include <utils/smallstring.h>
31 #include <utils/variant.h>
32 
33 #include <vector>
34 
35 namespace QmlDesigner::Storage {
36 
37 enum class TypeAccessSemantics : int { Invalid, Reference, Value, Sequence, IsEnum = 1 << 8 };
38 
39 enum class PropertyDeclarationTraits : unsigned int {
40     Non = 0,
41     IsReadOnly = 1 << 0,
42     IsPointer = 1 << 1,
43     IsList = 1 << 2
44 };
45 
46 constexpr PropertyDeclarationTraits operator|(PropertyDeclarationTraits first,
47                                               PropertyDeclarationTraits second)
48 {
49     return static_cast<PropertyDeclarationTraits>(static_cast<int>(first) | static_cast<int>(second));
50 }
51 
52 constexpr bool operator&(PropertyDeclarationTraits first, PropertyDeclarationTraits second)
53 {
54     return static_cast<int>(first) & static_cast<int>(second);
55 }
56 
57 class VersionNumber
58 {
59 public:
60     explicit VersionNumber() = default;
VersionNumber(int version)61     explicit VersionNumber(int version)
62         : version{version}
63     {}
64 
65     explicit operator bool() const { return version >= 0; }
66 
67     friend bool operator==(VersionNumber first, VersionNumber second) noexcept
68     {
69         return first.version == second.version;
70     }
71 
72 public:
73     int version = -1;
74 };
75 
76 class Version
77 {
78 public:
79     explicit Version() = default;
80     explicit Version(VersionNumber major, VersionNumber minor = VersionNumber{})
81         : major{major}
82         , minor{minor}
83     {}
84 
Version(int major,int minor)85     explicit Version(int major, int minor)
86         : major{major}
87         , minor{minor}
88     {}
89 
Version(int major)90     explicit Version(int major)
91         : major{major}
92     {}
93 
94     friend bool operator==(Version first, Version second) noexcept
95     {
96         return first.major == second.major && first.minor == second.minor;
97     }
98 
99     explicit operator bool() { return major && minor; }
100 
101 public:
102     VersionNumber major;
103     VersionNumber minor;
104 };
105 
106 class ExportedType
107 {
108 public:
109     explicit ExportedType() = default;
ExportedType(Utils::SmallStringView name)110     explicit ExportedType(Utils::SmallStringView name)
111         : name{name}
112     {}
113 
114 public:
115     Utils::SmallString name;
116 };
117 
118 class ExplicitExportedType
119 {
120 public:
121     explicit ExplicitExportedType() = default;
ExplicitExportedType(Utils::SmallStringView name,ImportId importId)122     explicit ExplicitExportedType(Utils::SmallStringView name, ImportId importId)
123         : name{name}
124         , importId{importId}
125     {}
126 
127 public:
128     Utils::SmallString name;
129     ImportId importId;
130 };
131 
132 using ExportedTypes = std::vector<ExportedType>;
133 
134 class NativeType
135 {
136 public:
137     explicit NativeType() = default;
NativeType(Utils::SmallStringView name)138     explicit NativeType(Utils::SmallStringView name)
139         : name{name}
140     {}
141 
142 public:
143     Utils::SmallString name;
144 };
145 
146 using TypeName = Utils::variant<NativeType, ExportedType, ExplicitExportedType>;
147 
148 class EnumeratorDeclaration
149 {
150 public:
151     explicit EnumeratorDeclaration() = default;
152     explicit EnumeratorDeclaration(Utils::SmallStringView name, long long value, int hasValue = true)
153         : name{name}
154         , value{value}
155         , hasValue{bool(hasValue)}
156     {}
157 
EnumeratorDeclaration(Utils::SmallStringView name)158     explicit EnumeratorDeclaration(Utils::SmallStringView name)
159         : name{name}
160     {}
161 
162     friend bool operator==(const EnumeratorDeclaration &first, const EnumeratorDeclaration &second)
163     {
164         return first.name == second.name && first.value == second.value
165                && first.hasValue == second.hasValue;
166     }
167 
168 public:
169     Utils::SmallString name;
170     long long value = 0;
171     bool hasValue = false;
172 };
173 
174 using EnumeratorDeclarations = std::vector<EnumeratorDeclaration>;
175 
176 class EnumerationDeclaration
177 {
178 public:
179     explicit EnumerationDeclaration() = default;
EnumerationDeclaration(Utils::SmallStringView name,EnumeratorDeclarations enumeratorDeclarations)180     explicit EnumerationDeclaration(Utils::SmallStringView name,
181                                     EnumeratorDeclarations enumeratorDeclarations)
182         : name{name}
183         , enumeratorDeclarations{std::move(enumeratorDeclarations)}
184     {}
185 
186     friend bool operator==(const EnumerationDeclaration &first, const EnumerationDeclaration &second)
187     {
188         return first.name == second.name
189                && first.enumeratorDeclarations == second.enumeratorDeclarations;
190     }
191 
192 public:
193     Utils::SmallString name;
194     EnumeratorDeclarations enumeratorDeclarations;
195 };
196 
197 using EnumerationDeclarations = std::vector<EnumerationDeclaration>;
198 
199 class EnumerationDeclarationView
200 {
201 public:
202     explicit EnumerationDeclarationView() = default;
EnumerationDeclarationView(Utils::SmallStringView name,Utils::SmallStringView enumeratorDeclarations,long long id)203     explicit EnumerationDeclarationView(Utils::SmallStringView name,
204                                         Utils::SmallStringView enumeratorDeclarations,
205                                         long long id)
206         : name{name}
207         , enumeratorDeclarations{std::move(enumeratorDeclarations)}
208         , id{id}
209     {}
210 
211 public:
212     Utils::SmallStringView name;
213     Utils::SmallStringView enumeratorDeclarations;
214     EnumerationDeclarationId id;
215 };
216 
217 class ParameterDeclaration
218 {
219 public:
220     explicit ParameterDeclaration() = default;
221     explicit ParameterDeclaration(Utils::SmallStringView name,
222                                   Utils::SmallStringView typeName,
223                                   PropertyDeclarationTraits traits = {})
224         : name{name}
225         , typeName{typeName}
226         , traits{traits}
227     {}
228 
ParameterDeclaration(Utils::SmallStringView name,Utils::SmallStringView typeName,int traits)229     explicit ParameterDeclaration(Utils::SmallStringView name, Utils::SmallStringView typeName, int traits)
230         : name{name}
231         , typeName{typeName}
232         , traits{static_cast<PropertyDeclarationTraits>(traits)}
233     {}
234 
235     friend bool operator==(const ParameterDeclaration &first, const ParameterDeclaration &second)
236     {
237         return first.name == second.name && first.typeName == second.typeName
238                && first.traits == second.traits;
239     }
240 
241 public:
242     Utils::SmallString name;
243     Utils::SmallString typeName;
244     PropertyDeclarationTraits traits = {};
245 };
246 
247 using ParameterDeclarations = std::vector<ParameterDeclaration>;
248 
249 class SignalDeclaration
250 {
251 public:
252     explicit SignalDeclaration() = default;
SignalDeclaration(Utils::SmallString name,ParameterDeclarations parameters)253     explicit SignalDeclaration(Utils::SmallString name, ParameterDeclarations parameters)
254         : name{name}
255         , parameters{std::move(parameters)}
256     {}
257 
SignalDeclaration(Utils::SmallString name)258     explicit SignalDeclaration(Utils::SmallString name)
259         : name{name}
260     {}
261 
262     friend bool operator==(const SignalDeclaration &first, const SignalDeclaration &second)
263     {
264         return first.name == second.name && first.parameters == second.parameters;
265     }
266 
267 public:
268     Utils::SmallString name;
269     ParameterDeclarations parameters;
270 };
271 
272 using SignalDeclarations = std::vector<SignalDeclaration>;
273 
274 class SignalDeclarationView
275 {
276 public:
277     explicit SignalDeclarationView() = default;
SignalDeclarationView(Utils::SmallStringView name,Utils::SmallStringView signature,long long id)278     explicit SignalDeclarationView(Utils::SmallStringView name,
279                                    Utils::SmallStringView signature,
280                                    long long id)
281         : name{name}
282         , signature{signature}
283         , id{id}
284     {}
285 
286 public:
287     Utils::SmallStringView name;
288     Utils::SmallStringView signature;
289     SignalDeclarationId id;
290 };
291 
292 class FunctionDeclaration
293 {
294 public:
295     explicit FunctionDeclaration() = default;
FunctionDeclaration(Utils::SmallStringView name,Utils::SmallStringView returnTypeName,ParameterDeclarations parameters)296     explicit FunctionDeclaration(Utils::SmallStringView name,
297                                  Utils::SmallStringView returnTypeName,
298                                  ParameterDeclarations parameters)
299         : name{name}
300         , returnTypeName{returnTypeName}
301         , parameters{std::move(parameters)}
302     {}
303 
304     explicit FunctionDeclaration(Utils::SmallStringView name,
305                                  Utils::SmallStringView returnTypeName = {})
306         : name{name}
307         , returnTypeName{returnTypeName}
308     {}
309 
310     friend bool operator==(const FunctionDeclaration &first, const FunctionDeclaration &second)
311     {
312         return first.name == second.name && first.returnTypeName == second.returnTypeName
313                && first.parameters == second.parameters;
314     }
315 
316 public:
317     Utils::SmallString name;
318     Utils::SmallString returnTypeName;
319     ParameterDeclarations parameters;
320 };
321 
322 using FunctionDeclarations = std::vector<FunctionDeclaration>;
323 
324 class FunctionDeclarationView
325 {
326 public:
327     explicit FunctionDeclarationView() = default;
FunctionDeclarationView(Utils::SmallStringView name,Utils::SmallStringView returnTypeName,Utils::SmallStringView signature,long long id)328     explicit FunctionDeclarationView(Utils::SmallStringView name,
329                                      Utils::SmallStringView returnTypeName,
330                                      Utils::SmallStringView signature,
331                                      long long id)
332         : name{name}
333         , returnTypeName{returnTypeName}
334         , signature{signature}
335         , id{id}
336     {}
337 
338 public:
339     Utils::SmallStringView name;
340     Utils::SmallStringView returnTypeName;
341     Utils::SmallStringView signature;
342     FunctionDeclarationId id;
343 };
344 
345 class PropertyDeclaration
346 {
347 public:
348     explicit PropertyDeclaration() = default;
PropertyDeclaration(Utils::SmallStringView name,TypeName typeName,PropertyDeclarationTraits traits)349     explicit PropertyDeclaration(Utils::SmallStringView name,
350                                  TypeName typeName,
351                                  PropertyDeclarationTraits traits)
352         : name{name}
353         , typeName{std::move(typeName)}
354         , traits{traits}
355     {}
356 
PropertyDeclaration(Utils::SmallStringView name,Utils::SmallStringView typeName,int traits)357     explicit PropertyDeclaration(Utils::SmallStringView name, Utils::SmallStringView typeName, int traits)
358         : name{name}
359         , typeName{NativeType{typeName}}
360         , traits{static_cast<PropertyDeclarationTraits>(traits)}
361     {}
362 
363 public:
364     Utils::SmallString name;
365     TypeName typeName;
366     PropertyDeclarationTraits traits = {};
367     TypeId typeId;
368 };
369 
370 using PropertyDeclarations = std::vector<PropertyDeclaration>;
371 
372 class PropertyDeclarationView
373 {
374 public:
PropertyDeclarationView(Utils::SmallStringView name,int traits,long long typeId,long long id)375     explicit PropertyDeclarationView(Utils::SmallStringView name,
376                                      int traits,
377                                      long long typeId,
378                                      long long id)
379         : name{name}
380         , traits{static_cast<PropertyDeclarationTraits>(traits)}
381         , typeId{typeId}
382         , id{id}
383 
384     {}
385 
386 public:
387     Utils::SmallStringView name;
388     PropertyDeclarationTraits traits = {};
389     TypeId typeId;
390     PropertyDeclarationId id;
391 };
392 
393 class AliasPropertyDeclaration
394 {
395 public:
AliasPropertyDeclaration(Utils::SmallStringView name,TypeName aliasTypeName,Utils::SmallStringView aliasPropertyName)396     explicit AliasPropertyDeclaration(Utils::SmallStringView name,
397                                       TypeName aliasTypeName,
398                                       Utils::SmallStringView aliasPropertyName)
399         : name{name}
400         , aliasTypeName{std::move(aliasTypeName)}
401         , aliasPropertyName{aliasPropertyName}
402     {}
403 
404 public:
405     Utils::SmallString name;
406     TypeName aliasTypeName;
407     Utils::SmallString aliasPropertyName;
408 };
409 
410 using AliasDeclarations = std::vector<AliasPropertyDeclaration>;
411 
412 class AliasPropertyDeclarationView
413 {
414 public:
AliasPropertyDeclarationView(Utils::SmallStringView name,long long id,long long aliasId)415     explicit AliasPropertyDeclarationView(Utils::SmallStringView name, long long id, long long aliasId)
416         : name{name}
417         , id{id}
418         , aliasId{aliasId}
419     {}
420 
421 public:
422     Utils::SmallString name;
423     PropertyDeclarationId id;
424     PropertyDeclarationId aliasId;
425 };
426 
427 class Type
428 {
429 public:
430     explicit Type() = default;
431     explicit Type(ImportId importId,
432                   Utils::SmallStringView typeName,
433                   TypeName prototype,
434                   TypeAccessSemantics accessSemantics,
435                   SourceId sourceId,
436                   ImportIds importIds = {},
437                   ExportedTypes exportedTypes = {},
438                   PropertyDeclarations propertyDeclarations = {},
439                   FunctionDeclarations functionDeclarations = {},
440                   SignalDeclarations signalDeclarations = {},
441                   EnumerationDeclarations enumerationDeclarations = {},
442                   AliasDeclarations aliasDeclarations = {},
443                   TypeId typeId = TypeId{})
444         : typeName{typeName}
445         , prototype{std::move(prototype)}
446         , importIds{std::move(importIds)}
447         , exportedTypes{std::move(exportedTypes)}
448         , propertyDeclarations{std::move(propertyDeclarations)}
449         , functionDeclarations{std::move(functionDeclarations)}
450         , signalDeclarations{std::move(signalDeclarations)}
451         , enumerationDeclarations{std::move(enumerationDeclarations)}
452         , aliasDeclarations{std::move(aliasDeclarations)}
453         , accessSemantics{accessSemantics}
454         , sourceId{sourceId}
455         , typeId{typeId}
456         , importId{importId}
457     {}
458 
Type(long long importId,Utils::SmallStringView typeName,Utils::SmallStringView prototype,int accessSemantics,int sourceId)459     explicit Type(long long importId,
460                   Utils::SmallStringView typeName,
461                   Utils::SmallStringView prototype,
462                   int accessSemantics,
463                   int sourceId)
464         : typeName{typeName}
465         , prototype{NativeType{prototype}}
466         , accessSemantics{static_cast<TypeAccessSemantics>(accessSemantics)}
467         , sourceId{sourceId}
468         , importId{importId}
469 
470     {}
471 
Type(long long importId,Utils::SmallStringView typeName,long long typeId,Utils::SmallStringView prototype,int accessSemantics,int sourceId)472     explicit Type(long long importId,
473                   Utils::SmallStringView typeName,
474                   long long typeId,
475                   Utils::SmallStringView prototype,
476                   int accessSemantics,
477                   int sourceId)
478         : typeName{typeName}
479         , prototype{NativeType{prototype}}
480         , accessSemantics{static_cast<TypeAccessSemantics>(accessSemantics)}
481         , sourceId{sourceId}
482         , typeId{typeId}
483         , importId{importId}
484     {}
485 
486 public:
487     Utils::SmallString typeName;
488     TypeName prototype;
489     Utils::SmallString attachedType;
490     ImportIds importIds;
491     ExportedTypes exportedTypes;
492     PropertyDeclarations propertyDeclarations;
493     FunctionDeclarations functionDeclarations;
494     SignalDeclarations signalDeclarations;
495     EnumerationDeclarations enumerationDeclarations;
496     AliasDeclarations aliasDeclarations;
497     TypeAccessSemantics accessSemantics = TypeAccessSemantics::Invalid;
498     SourceId sourceId;
499     TypeId typeId;
500     ImportId importId;
501     bool isCreatable = false;
502 };
503 
504 using Types = std::vector<Type>;
505 
506 class BasicImport
507 {
508 public:
509     explicit BasicImport(Utils::SmallStringView name, VersionNumber version = VersionNumber{})
510         : name{name}
511         , version{version}
512     {}
513 
BasicImport(Utils::SmallStringView name,int version)514     explicit BasicImport(Utils::SmallStringView name, int version)
515         : name{name}
516         , version{version}
517     {}
518 
519     friend bool operator==(const BasicImport &first, const BasicImport &second)
520     {
521         return first.name == second.name && first.version == second.version;
522     }
523 
524 public:
525     Utils::PathString name;
526     VersionNumber version;
527 };
528 
529 using BasicImports = std::vector<BasicImport>;
530 
531 class Import : public BasicImport
532 {
533 public:
534     explicit Import(Utils::SmallStringView name,
535                     VersionNumber version = VersionNumber{},
536                     SourceId sourceId = SourceId{},
537                     BasicImports importDependencies = {})
BasicImport(name,version)538         : BasicImport(name, version)
539         , importDependencies{std::move(importDependencies)}
540         , sourceId{sourceId}
541     {}
542 
Import(Utils::SmallStringView name,int version,int sourceId)543     explicit Import(Utils::SmallStringView name, int version, int sourceId)
544         : BasicImport(name, version)
545         , sourceId{sourceId}
546     {}
547 
548     friend bool operator==(const Import &first, const Import &second)
549     {
550         return static_cast<const BasicImport &>(first) == static_cast<const BasicImport &>(second)
551                && first.sourceId == second.sourceId
552                && first.importDependencies == second.importDependencies;
553     }
554 
555 public:
556     BasicImports importDependencies;
557     SourceId sourceId;
558     ImportId importId;
559 };
560 
561 using Imports = std::vector<Import>;
562 
563 class ImportView
564 {
565 public:
ImportView(Utils::SmallStringView name,int version,int sourceId,long long importId)566     explicit ImportView(Utils::SmallStringView name, int version, int sourceId, long long importId)
567         : name{name}
568         , version{version}
569         , sourceId{sourceId}
570         , importId{importId}
571     {}
572 
573     friend bool operator==(const ImportView &first, const ImportView &second)
574     {
575         return first.name == second.name
576                && first.version == second.version && first.sourceId == second.sourceId;
577     }
578 
579 public:
580     Utils::SmallStringView name;
581     VersionNumber version;
582     SourceId sourceId;
583     ImportId importId;
584 };
585 
586 } // namespace QmlDesigner::Storage
587