summaryrefslogtreecommitdiffstats
path: root/tools/porting/src/codemodel.h
blob: cb3088f6f06aa5f628411c585117c7f7855fd668 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/****************************************************************************
**
** Copyright (C) 2001-2004 Roberto Raggi
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the qt3to4 porting application of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef CODEMODEL_H
#define CODEMODEL_H

#include "smallobject.h"
#include "tokenengine.h"

#include <QByteArray>
#include <QList>
#include <QMap>
#include <QHash>

QT_BEGIN_NAMESPACE

namespace CodeModel
{

// types
struct Type;
struct EnumType;
struct EnumeratorType;
struct ClassType;
struct BuiltinType;
struct PointerType;
struct ReferenceType;
struct GenericType;
struct AliasType;
struct FunctionType;
struct UnknownType;

// Scopes contain child scopes, members and types.
struct Scope;
struct ClassScope;
struct NamespaceScope;
struct BlockScope;

// Members introduces names into scopes, and are also linked to a specific
// token in a source file.
struct Member;
struct FunctionMember;
struct VariableMember;
struct UsingDeclarationMember;
struct NamespaceMember;
struct TypeMember;

// Name uses links uses of a name to its declaration (a Member), and also to a
// token in a source file.
struct NameUse;

struct Argument;
struct UsingDirectiveLink;

template <typename CollectedType>
class Collection: public QMultiHash<QByteArray, CollectedType *>
{
public:
     void add(CollectedType *collectedItem)
    { insert(collectedItem->name(), collectedItem); }
};

typedef Collection<Scope> ScopeCollection;
typedef Collection<Member> MemberCollection;
typedef Collection<Type> TypeCollection;
typedef Collection<NameUse> NameUseCollection;
typedef Collection<Argument> ArgumentCollection;

struct SemanticInfo
{
    CodeModel::NamespaceScope *codeModel;

    // tokenindex -> NameUse* map. Use map here bacause we expect name uses to
    // be sparesly distributed among the tokens.
    QMap<int, NameUse*> nameUses;
};


struct Item
{
    Item()  {}
    virtual ~Item() {}
    virtual QByteArray name() const = 0;
};

struct Type: public Item
{
    virtual QByteArray name() const =0;

    virtual EnumType *toEnumType() const
    { return 0; }

    virtual ClassType *toClassType() const
    { return 0; }

    virtual UnknownType *toUnknownType() const
    { return 0; }

    virtual BuiltinType *toBuiltinType() const
    { return 0; }

    virtual PointerType *toPointerType() const
    { return 0; }

    virtual ReferenceType *toReferenceType() const
    { return 0; }

    virtual GenericType *toGenericType() const
    { return 0; }

    virtual AliasType *toAliasType() const
    { return 0; }
};

struct Scope: public Item
{
    Scope()
     : m_parent(0) {}

    void setParent(Scope *parent)
    { m_parent = parent; }

    Scope *parent() const
    { return m_parent; }

    QByteArray name() const
    { return m_name; }

    void setName(const QByteArray &name)
    { m_name=name; }

    virtual NamespaceScope *toNamespaceScope() const
    { return 0; }

    virtual ClassScope *toClassScope() const
    { return 0; }

    virtual BlockScope *toBlockScope() const
    { return 0; }

    const Collection<Scope> scopes() const
    { return m_scopes; }
    const Collection<Type> types() const
    { return m_types; }
    const Collection<Member> members() const
    { return m_members; }
    const Collection<NameUse> nameUses() const
    { return m_nameUses; }

    void addScope(Scope *scope);
    void addType(Type *type);
    void addMember(Member *member);
    void addNameUse(NameUse *nameUse);
private:
    Scope *m_parent;
    QByteArray m_name;
    Collection<Scope> m_scopes;
    Collection<Type> m_types;
    Collection<Member> m_members;
    Collection<NameUse> m_nameUses;
};

struct Member: public Item
{
    enum Binding // ### not used yet
    {
        Static,
        Instance
    };

    enum Access // ### not used yet
    {
        Public,
        Protected,
        Private
    };

    Member()
        : m_binding(Static), m_access(Public),
        m_parent(0), m_constant(0), m_static(0)   {}

    QByteArray name() const
    { return m_name; }

    void setName(const QByteArray &name)
    { m_name = name; }

    TokenEngine::TokenRef nameToken() const
    { return m_nameToken; }

    void setNameToken(TokenEngine::TokenRef nameToken)
    { m_nameToken = nameToken; }

    Binding binding() const
    { return m_binding; }

    void setBinding(Binding binding)
    { m_binding = binding; }

    Access access() const
    { return m_access; }

    void setAccess(Access access)
    { m_access = access; }

    bool isConstant() const
    { return m_constant; }

    void setConstant(bool b)
    { m_constant = b; }

    bool isStatic() const
    { return m_static; }

    void setStatic(bool b)
    { m_static = b; }

    Scope *parent() const
    { return m_parent; }

    void setParent(Scope *parent)
    { m_parent = parent; }

    virtual FunctionMember *toFunctionMember() const
    { return 0; }

    virtual VariableMember *toVariableMember() const
    { return 0; }

    virtual UsingDeclarationMember *toUsingDeclarationMember() const
    { return 0; }

    virtual NamespaceMember *toNamespaceMember() const
    { return 0; }

    virtual TypeMember *toTypeMember() const
    { return 0; }

 private:
     Binding m_binding;
     Access m_access;
     Scope *m_parent;
     QByteArray m_name;
     TokenEngine::TokenRef m_nameToken;
     uint m_constant : 1;
     uint m_static : 1;
};

struct ClassScope: public Scope
{
    const Collection<Type> baseClasses() const
    { return m_baseClasses; }

    void addBaseClass(Type *baseClass)
    {
        Q_ASSERT(baseClass->toClassType());
        m_baseClasses.add(baseClass);
    }

    virtual ClassScope *toClassScope() const
    { return const_cast<ClassScope*>(this); }

private:
    Collection<Type> m_baseClasses;
};

struct UsingDirectiveLinkable : public Scope
{
    const QList<UsingDirectiveLink *> usingDirectiveLinks() const
    { return m_usingDirectiveLinks; }

    void addUsingDirectiveLink(UsingDirectiveLink *usingDirectiveLink)
    { m_usingDirectiveLinks.append(usingDirectiveLink); }
private:
    QList<UsingDirectiveLink *> m_usingDirectiveLinks;
};

struct NamespaceScope: public UsingDirectiveLinkable
{
    NamespaceScope() {}

    virtual NamespaceScope *toNamespaceScope() const
    { return const_cast<NamespaceScope*>(this); }
};

struct BlockScope: public UsingDirectiveLinkable
{
    BlockScope() {}

    virtual BlockScope *toBlockScope() const
    { return const_cast<BlockScope*>(this); }
};

struct EnumType: public Type
{
    EnumType()
        : m_parent(0) {}

    QByteArray name() const
    { return m_name; }

    void setName(const QByteArray &name)
    { m_name = name; }

    Scope *parent() const
    { return m_parent; }

    void setParent(Scope *parent)
    { m_parent = parent; }

    virtual EnumType *toEnumType() const
    { return const_cast<EnumType*>(this); }

private:
    Scope *m_parent;
    QByteArray m_name;
};

struct UnknownType: public Type
{
    UnknownType()
        : m_parent(0) {}

    QByteArray name() const
    { return m_name; }

    void setName(const QByteArray &name)
    { m_name = name; }

    Scope *parent() const
    { return m_parent; }

    void setParent(Scope *parent)
    { m_parent = parent; }

    virtual UnknownType *toUnknownType() const
    { return const_cast<UnknownType*>(this); }

private:
    Scope *m_parent;
    QByteArray m_name;
};

struct ClassType: public Type
{
    ClassType()
        : m_parent(0), m_scope(0) {}

    ClassScope *scope() const
    { return m_scope; }

    void setScope(ClassScope *scope)
    { m_scope = scope; }

    QByteArray name() const
    { return m_scope ? m_scope->name() : /*anonymous*/ QByteArray(); }

    Scope *parent() const
    { return m_parent; }

    void setParent(Scope *parent)
    { m_parent = parent; }

    virtual ClassType *toClassType() const
    { return const_cast<ClassType*>(this); }

private:
    Scope *m_parent;
    ClassScope *m_scope;

};

struct BuiltinType: public Type
{
protected:
    BuiltinType(const QByteArray &name, Scope *parent)
        : m_name(name), m_parent(parent) {}

public:
    QByteArray name() const
    { return m_name; }

    Scope *parent() const
    { return m_parent; }

    virtual BuiltinType *toBuiltinType() const
    { return const_cast<BuiltinType*>(this); }

    static BuiltinType Bool;
    static BuiltinType Void;
    static BuiltinType Char;
    static BuiltinType Short;
    static BuiltinType Int;
    static BuiltinType Long;
    static BuiltinType Double;
    static BuiltinType Float;
    static BuiltinType Unsigned;
    static BuiltinType Signed;
    // ### more

private:
    QByteArray m_name;
    Scope *m_parent;
};

struct PointerType: public Type
{
    PointerType()
        : m_parent(0), m_baseType(0) {}

    Type *baseType() const
    { return m_baseType; }

    void setBaseType(Type *baseType)
    { m_baseType = baseType; }

    QByteArray name() const
    { return m_baseType->name(); }

    Scope *parent() const
    { return m_parent; }

    void setParent(Scope *parent)
    { m_parent = parent; }

    virtual PointerType *toPointerType() const
    { return const_cast<PointerType*>(this); }

private:
    Scope *m_parent;
    Type *m_baseType;
};

struct ReferenceType: public Type
{
    ReferenceType()
        : m_parent(0), m_baseType(0) {}

    Type *baseType() const
    { return m_baseType; }

    void setBaseType(Type *baseType)
    { m_baseType = baseType; }

    QByteArray name() const
    { return m_baseType->name(); }

    Scope *parent() const
    { return m_parent; }

    void setParent(Scope *parent)
    { m_parent = parent; }

    virtual ReferenceType *toReferenceType() const
    { return const_cast<ReferenceType*>(this); }

private:
    Scope *m_parent;
    Type *m_baseType;
};

struct GenericType: public Type // ### implement me
{
    virtual GenericType *toGenericType() const
    { return const_cast<GenericType*>(this); }
};

struct AliasType: public Type // ### implement me
{
    AliasType ()
        : m_parent(0) {}

    QByteArray name() const
    {  return m_name;  }

    Scope *parent() const
    {  return m_parent;  }

    virtual AliasType *toAliasType() const
    { return const_cast<AliasType*>(this); }
private:
    QByteArray m_name;
    Scope *m_parent;
};

struct Argument: public Item
{
    Argument()
        : m_parent(0), m_type(0) {}

    Type *type() const
    { return m_type; }

    void setType(Type *type)
    { m_type = type; }

    QByteArray name() const
    { return m_name; }

    void setName(const QByteArray &name)
    { m_name = name; }

    TokenEngine::TokenRef nameToken() const
    { return m_nameToken; }

    void setNameToken(TokenEngine::TokenRef nameToken)
    { m_nameToken = nameToken; }

    virtual FunctionMember *parent() const
    { return m_parent; }

    void setParent(FunctionMember *parent)
    { m_parent = parent; }

private:
    FunctionMember *m_parent;
    Type *m_type;
    QByteArray m_name;
    TokenEngine::TokenRef m_nameToken;
};

struct FunctionMember: public Member
{
    inline FunctionMember()
        : m_returnType(0),
          m_functionBodyScope(0),
          m_signal(0),
          m_virtual(0), m_abstract(0) { m_slot = 0; }

    virtual FunctionMember *toFunctionMember() const
    { return const_cast<FunctionMember*>(this); }

    Type *returnType() const
    { return m_returnType; }

    void setReturnType(Type *type)
    { m_returnType = type; }

    const Collection<Argument> arguments() const
    { return m_arguments; }

    void addArgument(Argument *argument)
    { m_arguments.insert(argument->name(), argument); }

    void setFunctionBodyScope(BlockScope *functionBodyScope)
    { m_functionBodyScope = functionBodyScope; }

    BlockScope *functionBodyScope() const
    {return m_functionBodyScope;}

    bool isSignal() const
    { return m_signal; }

    void setSignal(bool b)
    { m_signal = b; }

    bool isSlot() const
    { return m_slot; }

    void setSlot(bool b)
    { m_slot = b; }

    bool isVirtual() const
    { return m_virtual; }

    void setVirtual(bool b)
    { m_virtual = b; }

    bool isAbstract() const
    { return m_abstract; }

    void setAbstract(bool b)
    { m_abstract = b; }

private:
    Type *m_returnType;
    Collection<Argument> m_arguments;
    BlockScope *m_functionBodyScope;
    uint m_signal: 1;
    uint m_slot: 1;
    uint m_virtual: 1;
    uint m_abstract: 1;
};

struct VariableMember: public Member
{
    VariableMember()
        : m_type(0) {}

    Type *type() const
    { return m_type; }

    void setType(Type *type)
    { m_type = type; }

    virtual VariableMember *toVariableMember() const
    { return const_cast<VariableMember*>(this); }

private:
    Type *m_type;
};

struct UsingDeclarationMember: public Member
{
    UsingDeclarationMember()
    : m_member(0) {}

    virtual UsingDeclarationMember *toUsingDeclarationMember() const
    { return const_cast<UsingDeclarationMember*>(this); }

    Member *member() const
    { return m_member; }

    void setMember(Member *member)
    { m_member = member; }

private:
    Member *m_member;
};

struct NamespaceMember: public Member
{
    NamespaceMember()
        :m_namespaceScope(0) {}

    virtual NamespaceMember *toNamespaceMember() const
    { return const_cast<NamespaceMember*>(this); }

    NamespaceScope *namespaceScope() const
    { return m_namespaceScope; }

    void setNamespaceScope(NamespaceScope *namespaceScope)
    { m_namespaceScope = namespaceScope; }
private:
    NamespaceScope *m_namespaceScope;
};

struct TypeMember: public Member
{
    TypeMember()
        :m_type(0) {}

    virtual TypeMember *toTypeMember() const
    { return const_cast<TypeMember*>(this); }

    Type *type() const
    { return m_type; }

    void setType(Type *type)
    { m_type = type; }
private:
    Type *m_type;

};

struct NameUse: public Item
{
    NameUse()
    : m_declaration(0), m_parent(0) {}

    QByteArray name() const
    { return m_name; }

    void setName(const QByteArray &name)
    {  m_name = name; }

     TokenEngine::TokenRef nameToken() const
    { return m_nameToken; }

    void setNameToken(TokenEngine::TokenRef nameToken)
    { m_nameToken = nameToken; }

    Scope *parent() const
    { return m_parent; }

    void setParent(Scope *parent)
    { m_parent = parent; }

    Member *declaration() const
    { return m_declaration; }

    void setDeclaration(Member *parent)
    { m_declaration = parent; }

private:
    QByteArray m_name;
    TokenEngine::TokenRef m_nameToken;
    Member *m_declaration;
    Scope *m_parent;
};

struct UsingDirectiveLink: public Item
{
    UsingDirectiveLink()
        : m_parent(0), m_targetNamespace(0), m_insertionNamespace(0) {}

    QByteArray name() const
    { return QByteArray(); }

    Scope *parent() const
    { return m_parent; }

    void setParent(Scope *parent)
    { m_parent = parent; }

    NamespaceScope *targetNamespace() const
    { return m_targetNamespace; }

    void setTargetNamespace(NamespaceScope *targetNamespace)
    { m_targetNamespace = targetNamespace; }

    NamespaceScope *insertionNamespace() const
    { return m_insertionNamespace; }

    void setInsertionNamespace(NamespaceScope *insertionNamespace)
    { m_insertionNamespace = insertionNamespace; }
private:
    Scope *m_parent;
    // targetNamespace is the namespace specified by the using directive.
    NamespaceScope *m_targetNamespace;
    // m_insertionNamespace is the namespace where the names from
    // targetNamespace will be inserted. The C++ standard (7.3.4.1)
    // defines this as the nearest namespace that includes both m_parent
    // and m_targetNamespace.
    NamespaceScope *m_insertionNamespace;
};

template <class T>
T *Create(TypedPool<CodeModel::Item> *p)
{
    return new (p->allocate(sizeof(T))) T();
}

} // namespace CodeModel

QT_END_NAMESPACE

#endif // CODEMODEL_H