summaryrefslogtreecommitdiffstats
path: root/tools/porting/src/semantic.cpp
blob: bd9175f31eb47810b512208e7266b5b650ab0a53 (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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
/****************************************************************************
**
** 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$
**
****************************************************************************/

#include "smallobject.h"
#include "tokenengine.h"
#include "semantic.h"
#include <QtDebug>
#include <QString>
#include <QRegExp>

QT_BEGIN_NAMESPACE

using namespace TokenStreamAdapter;
using namespace TokenEngine;
using namespace CodeModel;

Semantic::Semantic(CodeModel::NamespaceScope *globalScope,
                   TokenStreamAdapter::TokenStream *tokenStream,
                   TypedPool<CodeModel::Item> *storage)
{
    m_storage = storage;
    m_tokenStream = tokenStream;

    m_currentAccess = CodeModel::Member::Public;
    m_inSlots = false;
    m_inSignals = false;
    m_inStorageSpec = false;
    m_inTypedef = false;

    globalScope->setName("::");
    currentScope.push(globalScope);

    //create global UnknownType and UnknownTypeMember
    UnknownType *type = Create<UnknownType>(m_storage);
    type->setName("__UnknownType");
    globalScope->addType(type);
    type->setParent(globalScope);

    m_sharedUnknownMember = Create<TypeMember>(m_storage);
    m_sharedUnknownMember->setNameToken(TokenRef());
    m_sharedUnknownMember->setName("Unknown");
    m_sharedUnknownMember->setType(type);
    globalScope->addMember(m_sharedUnknownMember);
    m_sharedUnknownMember->setParent(globalScope);

}

void Semantic::parseAST(TranslationUnitAST *node)
{
    TreeWalker::parseTranslationUnit(node);
}


void Semantic::parseLinkageSpecification(LinkageSpecificationAST *ast)
{
    if(!ast)
        return;
    int inStorageSpec = m_inStorageSpec;
    m_inStorageSpec = true;
    TreeWalker::parseLinkageSpecification(ast);
    m_inStorageSpec = inStorageSpec;
}

void Semantic::parseNamespace(NamespaceAST *ast)
{
    CodeModel::NamespaceScope *parent = currentScope.top()->toNamespaceScope();
    if(!parent->toNamespaceScope()) {
        emit error("Error in Semantic::parseNamespace: parent scope was not a namespace");
        return;
    }

    QByteArray nsName;
    if (!ast->namespaceName() || textOf(ast->namespaceName()).isEmpty()){
        nsName = "(__QT_ANON_NAMESPACE)";
    } else {
        nsName = textOf(ast->namespaceName());
    }

    CodeModel::NamespaceScope *namespaceScope = 0;

    // Look up namespace scope in case it is already defined.
    // (Unlike classes, C++ namespaces are "open" and can be added to.)
    CodeModel::Scope *scope = parent->scopes().value(nsName);
    if (scope)
        namespaceScope = scope->toNamespaceScope();

    // Create new namespace if not found.
    if (!namespaceScope) {
        namespaceScope = CodeModel::Create<CodeModel::NamespaceScope>(m_storage);
        namespaceScope->setName(nsName);
        parent->addScope(namespaceScope);

        NamespaceMember *namespaceMember = Create<NamespaceMember>(m_storage);
        namespaceMember->setNameToken(tokenRefFromAST(ast->namespaceName()));
        namespaceMember->setName(nsName);
        namespaceMember->setNamespaceScope(namespaceScope);
        currentScope.top()->addMember(namespaceMember);
        namespaceMember->setParent(currentScope.top());
    }

    currentScope.push(namespaceScope);
    TreeWalker::parseNamespace(ast);
    currentScope.pop();
}

void Semantic::parseClassSpecifier(ClassSpecifierAST *ast)
{
    if (!ast->name()){
        return;
    }

    QByteArray kind = textOf(ast->classKey());
    if (kind == "class")
        m_currentAccess = CodeModel::Member::Private;
    else // kind =="struct"
        m_currentAccess = CodeModel::Member::Public;

    QByteArray className = textOf(ast->name()->unqualifiedName());

    //create ClassScope
    CodeModel::ClassScope *klass = CodeModel::Create<CodeModel::ClassScope>(m_storage);
    klass->setName(className);
    currentScope.top()->addScope(klass);

    //create ClassType
    CodeModel::ClassType *type = CodeModel::Create<CodeModel::ClassType>(m_storage);
    type->setScope(klass);
    currentScope.top()->addType(type);
    type->setParent(currentScope.top());

    //create TypeMember
    CodeModel::TypeMember *typeMember = CodeModel::Create<CodeModel::TypeMember>(m_storage);
    typeMember->setNameToken(tokenRefFromAST(ast->name()->unqualifiedName()));
    typeMember->setName(className);
    typeMember->setType(type);
    currentScope.top()->addMember(typeMember);
    typeMember->setParent(currentScope.top());

    currentScope.push(klass);
    if (ast->baseClause())
        parseBaseClause(ast->baseClause(), klass);

    //TreeWalker::parseClassSpecifier(ast);
    parseNode(ast->winDeclSpec());
    parseNode(ast->classKey());
    parseNode(ast->baseClause());

    // Here's the trick for parsing c++ classes:
    // All inline function definitions must be interpreted as if they were
    // written after any other declarations in the class.
    QList<DeclarationAST *> functionDefinitions;
    if (ast->declarationList())
        foreach(DeclarationAST *decl, *ast->declarationList()) {
            if(decl->nodeType() == NodeType_FunctionDefinition)
                functionDefinitions.append(decl);
            else
            parseNode(decl);
        }
    foreach(DeclarationAST *decl, functionDefinitions)
        parseNode(decl);

    currentScope.pop();
}
/*
    Parse a class, struct or enum forward decalration.
*/
void Semantic::parseElaboratedTypeSpecifier(ElaboratedTypeSpecifierAST *node)
{
    if (!node)
        return;
    AST *kind = node->kind();
    if (!kind)
        return;

    const QByteArray kindText = textOf(kind);
    const QByteArray nameText = textOf(node->name());

    // Don't do anything if the class, struct or enum has already been declared or defined.
    if (lookupNameInScope(currentScope.top(), node->name()).count() > 0)
        return;

    if (kindText == "class" || kindText == "struct") {
        // Create ClassType.
        CodeModel::ClassType *type = CodeModel::Create<CodeModel::ClassType>(m_storage);
        type->setScope(0);
        currentScope.top()->addType(type);
        type->setParent(currentScope.top());

        // Create TypeMember.
        CodeModel::TypeMember *typeMember = CodeModel::Create<CodeModel::TypeMember>(m_storage);
        typeMember->setNameToken(tokenRefFromAST(node->name()->unqualifiedName()));
        typeMember->setName(nameText);
        typeMember->setType(type);
        currentScope.top()->addMember(typeMember);
        typeMember->setParent(currentScope.top());
    } else if (kindText == "enum") {
        //create a Type
        CodeModel::EnumType *enumType = CodeModel::Create<CodeModel::EnumType>(m_storage);
        enumType->setName(nameText);
        currentScope.top()->addType(enumType);
        enumType->setParent(currentScope.top());

        //create a TypeMember
        CodeModel::TypeMember *typeMember = CodeModel::Create<CodeModel::TypeMember>(m_storage);
        if(node->name())
            typeMember->setNameToken(tokenRefFromAST(node->name()->unqualifiedName()));
        typeMember->setName(nameText);
        typeMember->setType(enumType);
        currentScope.top()->addMember(typeMember);
        typeMember->setParent(currentScope.top());
    }
}

void Semantic::parseSimpleDeclaration(SimpleDeclarationAST *ast)
{
    TypeSpecifierAST *typeSpec = ast->typeSpec();
    InitDeclaratorListAST *declarators = ast->initDeclaratorList();

    if (typeSpec)
        parseTypeSpecifier(typeSpec);

    if (declarators){
        List<InitDeclaratorAST*> l = *declarators->initDeclaratorList();

        foreach (InitDeclaratorAST *current, l) {
            parseDeclaration(ast->functionSpecifier(), ast->storageSpecifier(), typeSpec, current);
        }
    }
}

void Semantic::parseDeclaration(AST *funSpec, AST *storageSpec, TypeSpecifierAST *typeSpec, InitDeclaratorAST *decl)
{
    if (m_inStorageSpec)
            return;

    if(!decl)
        return;

    DeclaratorAST *d = decl->declarator();
    if (!d)
        return;

    if (!d->subDeclarator() && d->parameterDeclarationClause()) {
        parseFunctionDeclaration(funSpec, storageSpec, typeSpec, decl);
		return;
	}
    if(!typeSpec || !typeSpec->name())
        return;

    DeclaratorAST *t = d;
    while (t && t->subDeclarator())
        t = t->subDeclarator();

    QByteArray id;
    if (t && t->declaratorId() && t->declaratorId()->unqualifiedName())
        id = textOf(t->declaratorId()->unqualifiedName());

    if (!t || !t->declaratorId() || !t->declaratorId()->unqualifiedName())
        return;
    AST *nameAST = t->declaratorId()->unqualifiedName();
    QByteArray name = textOf(nameAST);


    if (!scopeOfDeclarator(d, QList<QByteArray>()).isEmpty()){
        return;
    }

    //Check if this is possibly a function call by searching for '(' and ')'
    const QByteArray declText = textOf(decl);
    if (declText.contains("(") && declText.contains(")")) {
	if (decl->declarator() && decl->declarator()->subDeclarator()) {

        NameAST * name = decl->declarator()->subDeclarator()->declaratorId();
	if (name)
            parseNameUse(name);
	    return;	
        } 
    }

    //create VariableMember
    CodeModel::VariableMember *variableMember = CodeModel::Create<CodeModel::VariableMember>(m_storage);
    variableMember->setNameToken(tokenRefFromAST(nameAST));
    variableMember->setName(name);
    variableMember->setAccess(m_currentAccess);
    variableMember->setParent(currentScope.top());
    currentScope.top()->addMember(variableMember);

    //look up type of variableMember,

    TypeMember *typeMember = typeLookup(currentScope.top(), typeSpec->name());
    if(typeMember) {
        variableMember->setType(typeMember->type());
    } else {
        QByteArray text = typeOfDeclaration(typeSpec, d);
        CodeModel::UnknownType *type = CodeModel::Create<CodeModel::UnknownType>(m_storage);
        type->setName(text);
        variableMember->setType(type);
    }

    if (decl)
        parseNode(decl->initializer());

}

void Semantic::parseFunctionDeclaration(AST *funSpec, AST *storageSpec,
                                        TypeSpecifierAST * typeSpec, InitDeclaratorAST * initDeclarator)
{
    bool isFriend = false;
    bool isVirtual = false;
    bool isStatic = false;
    bool isInline = false;
    bool isPure = initDeclarator->initializer() != 0;

    if (funSpec){
        List<AST*> l = *funSpec->children();
        foreach (AST *current, l) {
            QByteArray text = textOf(current);
            if (text == "virtual") isVirtual = true;
            else if (text == "inline") isInline = true;
        }
    }

    if (storageSpec){
        List<AST*> l = *storageSpec->children();
        foreach (AST *current, l) {
            QByteArray text = textOf(current);
            if (text == "friend") isFriend = true;
            else if (text == "static") isStatic = true;
        }
    }
    DeclaratorAST *declarator = initDeclarator->declarator();
    if(!declarator || !declarator->declaratorId())
        return;
    AST *nameAST = declarator->declaratorId()->unqualifiedName();
    QByteArray name = textOf(nameAST);

    CodeModel::FunctionMember *method = CodeModel::Create<CodeModel::FunctionMember>(m_storage);
    method->setNameToken(tokenRefFromAST(nameAST));
    method->setName(name);
    method->setAccess(m_currentAccess);
    method->setStatic(isStatic);
    method->setVirtual(isVirtual);
    method->setAbstract(isPure);

    parseFunctionArguments(declarator, method);

    if (m_inSignals)
        method->setSignal(true);

    if (m_inSlots)
        method->setSlot(true);

    method->setConstant(declarator->constant() != 0);

    QByteArray text = typeOfDeclaration(typeSpec, declarator);
    if (!text.isEmpty()) {
        CodeModel::UnknownType *type = CodeModel::Create<CodeModel::UnknownType>(m_storage);
        type->setName(text);
        method->setReturnType(type);
    }

    method->setParent(currentScope.top());
    currentScope.top()->addMember(method);
}


void Semantic::parseBaseClause(BaseClauseAST * baseClause, CodeModel::ClassScope *klass)
{
    if(!baseClause)
        return;
    if(!klass)
        return;
    List<BaseSpecifierAST*> *l = baseClause->baseSpecifierList();
    if (!l)
        return;
    foreach (BaseSpecifierAST *baseSpecifier, *l) {
        QByteArray baseName;
        if (!baseSpecifier->name())
            continue;

        // Look up a class with the correct name.
        QList<Member *> candidates = nameLookup(klass, baseSpecifier->name());
        if (candidates.count() == 1 ) {
            Member *member = candidates.at(0);
            Q_ASSERT(member);
            TypeMember *typeMember = member->toTypeMember();
            if (typeMember) {
                Q_ASSERT(typeMember->type());
                ClassType *classType = typeMember->type()->toClassType();
                if (classType) {
                    klass->addBaseClass(classType);
                }
            }
        }
    }
}
void Semantic::parseFunctionArguments(const DeclaratorAST *declarator, CodeModel::FunctionMember *method)
{
    if(!declarator || !method)
        return;

    ParameterDeclarationClauseAST *clause = declarator->parameterDeclarationClause();

    if (clause && clause->parameterDeclarationList()){
        ParameterDeclarationListAST *params = clause->parameterDeclarationList();
        List<ParameterDeclarationAST*> *l = params->parameterList();
        if (!l)
            return;
        foreach (ParameterDeclarationAST *param, *l) {
            CodeModel::Argument *arg = CodeModel::Create<CodeModel::Argument>(m_storage);
            arg->setParent(method);

            if (param->declarator()){
                QByteArray text = declaratorToString(param->declarator(), QByteArray(), true);
                if(param->declarator()->declaratorId())
                    arg->setNameToken(tokenRefFromAST(param->declarator()->declaratorId()->unqualifiedName()));
                if (!text.isEmpty())
                    arg->setName(text);
            }

            QByteArray tp = typeOfDeclaration(param->typeSpec(), param->declarator());
            if (!tp.isEmpty()) {
                CodeModel::UnknownType *type = CodeModel::Create<CodeModel::UnknownType>(m_storage);
                type->setName(tp);
                arg->setType(type);
            }

            method->addArgument(arg);
        }
    }
}

// using directive (using namespace A)
void Semantic::parseUsingDirective(UsingDirectiveAST *ast)
{
    QByteArray qualifiedname = textOf(ast->name());
    QByteArray name = textOf(ast->name()->unqualifiedName());

    //look up target namespace name
    QList<Member *> memberList = nameLookup(currentScope.top(), ast->name());

    NamespaceScope *targetNamespace = 0;

    // search for namespace in member list.
    QList<Member *>::ConstIterator it = memberList.constBegin();
    while(it != memberList.constEnd()) {
        if (NamespaceMember *namespaceMember = (*it)->toNamespaceMember()) {
            targetNamespace = namespaceMember->namespaceScope();
            break;
        }
        ++it;
    }

    if (targetNamespace == 0)
        return;

    // Find the insertion namespace, which is the first common
    // ancesotor namespace for the current scope and the target namespace

    // currentScope might be a block scope, find its first namespace parent
    CodeModel::Scope *currentParent = currentScope.top();
    while (currentParent->toNamespaceScope() == 0) {
        currentParent = currentParent->parent();
    }

    CodeModel::Scope *namespaceA = currentParent;
    while (namespaceA != 0) {
        CodeModel::Scope *namespaceB = targetNamespace;
        while (namespaceB != 0) {
            if (namespaceB == namespaceA)
                break;
            namespaceB = namespaceB->parent();
        }
        if (namespaceB == namespaceA)
            break;
        namespaceA = namespaceA->parent();
    }

    if (namespaceA == 0 || namespaceA->toNamespaceScope() == 0)
        return;

    NamespaceScope *insertionNamespace = namespaceA->toNamespaceScope();

    // Create using directive link
    UsingDirectiveLink *usingDirectiveLink = Create<UsingDirectiveLink>(m_storage);
    usingDirectiveLink->setParent(currentScope.top());
    usingDirectiveLink->setTargetNamespace(targetNamespace);
    usingDirectiveLink->setInsertionNamespace(insertionNamespace);

    // add it to current namespace
    if (NamespaceScope *namespaceScope = currentScope.top()->toNamespaceScope())
        namespaceScope->addUsingDirectiveLink(usingDirectiveLink);
    else if (BlockScope *blockScope = currentScope.top()->toBlockScope())
        blockScope->addUsingDirectiveLink(usingDirectiveLink);
}

void Semantic::parseFunctionDefinition(FunctionDefinitionAST *ast)
{
    AST *funSpec = ast->functionSpecifier();
    AST *storageSpec = ast->storageSpecifier();
    TypeSpecifierAST *typeSpec = ast->typeSpec();
    InitDeclaratorAST *initDeclarator = ast->initDeclarator();
    if (!ast->initDeclarator())
        return;

    DeclaratorAST *d = initDeclarator->declarator();

    if (!d->declaratorId())
        return;

    parseFunctionDeclaration(funSpec, storageSpec, typeSpec, initDeclarator);
    CodeModel::FunctionMember *method = functionLookup(currentScope.top(), d);

    if(!method) {
        emit error("Error in Semantic::parseFunctionDefinition: Could not find declaration for function definition");
        return;
    }

    CodeModel::Scope *parent = method->parent();

    if(!ast->functionBody()) {
        emit error("Error in Semantic::parseFunctionDefinition: no function body in function definition");
        return;
    }

    //create child function scope
    QByteArray id = textOf(d->declaratorId()->unqualifiedName());
    CodeModel::BlockScope *functionScope = CodeModel::Create<CodeModel::BlockScope>(m_storage);
    functionScope->setName(QByteArray("__QT_ANON_BLOCK_SCOPE(Function: ") + id + QByteArray(")"));
    functionScope->setParent(parent);
    method->setFunctionBodyScope(functionScope);

    //add arguments to child scope
     ArgumentCollection arguments = method->arguments();
     ArgumentCollection::ConstIterator it = arguments.constBegin();
     while(it != arguments.constEnd()) {
         CodeModel::Argument *argument = *it;
         CodeModel::VariableMember *variableMember = CodeModel::Create<CodeModel::VariableMember>(m_storage);
         variableMember->setNameToken(argument->nameToken());
         variableMember->setType(argument->type());
         variableMember->setName(argument->name());
         variableMember->setParent(functionScope);
         functionScope->addMember(variableMember);
         ++it;
     }

    //push function scope and parse function body
    currentScope.push(functionScope);
    parseStatementList(ast->functionBody());
    currentScope.pop();
}

void Semantic::parseStatementList(StatementListAST *statemenList)
{
    if(!statemenList)
        return;
    CodeModel::BlockScope *blockScope = CodeModel::Create<CodeModel::BlockScope>(m_storage);
    blockScope->setName("__QT_ANON_BLOCK_SCOPE");
    blockScope->setParent(currentScope.top());
    currentScope.top()->addScope(blockScope);

    currentScope.push(blockScope);
    TreeWalker::parseStatementList(statemenList);
    currentScope.pop();
}

void Semantic::parseExpression(AbstractExpressionAST* node)
{
    if(!node)
        return;
    if(node->nodeType() == NodeType_ClassMemberAccess)
        parseClassMemberAccess(static_cast<ClassMemberAccessAST *>(node));
    else
        TreeWalker::parseExpression(node);
}

/*
    Pretty hardwired code for handling class member access of the types:
    object.member and objectPtr->member.

    This function creates a name use for object to its declaration, and a
    name use from member to its declaration in the class.
*/
void Semantic::parseClassMemberAccess(ClassMemberAccessAST *node)
{
    if(!node)
        return;
    parseExpression(node->expression());
    // Get a name use for the 'object' name.
    NameUse *nameUse = findNameUse(node->expression());
    // Since the NameUse refers to an object, its decalaration must be
    // a ClassType. Get the scope of this class type.
    if(    nameUse
        && nameUse->declaration()
        && nameUse->declaration()->toVariableMember()
        && nameUse->declaration()->toVariableMember()->type()
        && nameUse->declaration()->toVariableMember()->type()->toClassType()
        && nameUse->declaration()->toVariableMember()->type()->toClassType()->scope())   {

        CodeModel::Scope *scope = nameUse->declaration()->toVariableMember()->type()->toClassType()->scope();
        QList<CodeModel::Member *> members = lookupNameInScope(scope, node->name());
            if(members.count() != 0) {
                createNameUse(members.at(0), node->name());
                return;
            }
    }
    // Create a NameUse that refers to the global shared unknown type.
    createNameUse(m_sharedUnknownMember, node->name());
}

void Semantic::parseExpressionStatement(ExpressionStatementAST *node)
{
    TreeWalker::parseExpressionStatement(node);
}

// using declaration (using A::b)
void Semantic::parseUsing(UsingAST *ast)
{
    //CodeModel::Scope *s = lookUpScope(currentScope.top(), ast->name());
    QList<CodeModel::Member *> members = nameLookup(currentScope.top(), ast->name());
    if(members.isEmpty()) {
        emit error("Error in Semantic::parseUsing: could not look up using target");
        return;
    }
    //TODO: handle multiple members (when nameLookup returns a set of overloded functions)
    CodeModel::Member *member = members[0];
    CodeModel::Scope *targetScope = member->parent();
    if(!targetScope) {
        emit error("Error in Semantic::parseUsing: target has no parent scope");
        return;
    }

    if(!ast->name())
        return;
    AST *nameAST = ast->name()->unqualifiedName();
    if(!nameAST)
        return;
    QByteArray name = textOf(nameAST);
}

void Semantic::parseEnumSpecifier(EnumSpecifierAST *ast)
{
    if (!ast->name())
         return;

    QByteArray name = textOf(ast->name());

    //create a Type
    CodeModel::EnumType *enumType = CodeModel::Create<CodeModel::EnumType>(m_storage);
    enumType->setName(name);
    currentScope.top()->addType(enumType);
    enumType->setParent(currentScope.top());

    //create a TypeMember
    CodeModel::TypeMember *typeMember = CodeModel::Create<CodeModel::TypeMember>(m_storage);
    if(ast->name())
        typeMember->setNameToken(tokenRefFromAST(ast->name()->unqualifiedName()));
    typeMember->setName(name);
    typeMember->setType(enumType);
    currentScope.top()->addMember(typeMember);
    typeMember->setParent(currentScope.top());

    //parse the eneumerators
    List<EnumeratorAST*> *list = ast->enumeratorList();
    if (!list)
        return;
    foreach (EnumeratorAST *current, *list) {
        CodeModel::VariableMember *enumerator = CodeModel::Create<CodeModel::VariableMember>(m_storage);
        enumerator->setNameToken(tokenRefFromAST(current->id()));
        enumerator->setName(textOf(current->id()));
        enumerator->setAccess(m_currentAccess);
        enumerator->setStatic(true);
        enumerator->setType(enumType);
        currentScope.top()->addMember(enumerator);
        enumerator->setParent(currentScope.top());
    }

}

void Semantic::parseTypedef(TypedefAST *ast)
{
    TypeSpecifierAST *typeSpec = ast->typeSpec();
    InitDeclaratorListAST *declarators = ast->initDeclaratorList();

    if (typeSpec && declarators){
        QByteArray typeId;

        if (typeSpec->name())
            typeId = textOf(typeSpec->name());

        List<InitDeclaratorAST*> *l = declarators->initDeclaratorList();
        if (!l)
            return;
        foreach (InitDeclaratorAST *initDecl, *l) {
            QByteArray type, id;
            if (initDecl->declarator()){
               type = typeOfDeclaration(typeSpec, initDecl->declarator());

               DeclaratorAST *d = initDecl->declarator();
               while (d->subDeclarator()){
                   d = d->subDeclarator();
               }

               if (d->declaratorId())
                  id = textOf(d->declaratorId());
            }

            //create a type
            CodeModel::Scope *scope = currentScope.top();
            CodeModel::AliasType *typeAlias = CodeModel::Create<CodeModel::AliasType>(m_storage);
            //typeAlias->setName(id);
            //typeAlias->setParent(scope);
            scope->addType(typeAlias);

            //create a TypeMember
            CodeModel::TypeMember *typeMember = CodeModel::Create<CodeModel::TypeMember>(m_storage);
            if(typeSpec->name())
                typeMember->setNameToken(tokenRefFromAST(typeSpec->name()->unqualifiedName()));
            typeMember->setName(id);
            typeMember->setType(typeAlias);
            currentScope.top()->addMember(typeMember);
            typeMember->setParent(currentScope.top());

        }

    }
}

void Semantic::parseTypeSpecifier(TypeSpecifierAST *ast)
{
    // If this is a classSpecifier or a EnumSpecifier we skip the name lookup,
    // because looking up the name "E" in a class definition like
    // "class E { ..." makes no sense. (There might be a variable named E
    // already declared, but that variable is now shadowed by the class type.)
    if(   ast->nodeType() != NodeType_EnumSpecifier
       && ast->nodeType() != NodeType_ClassSpecifier
       && ast->nodeType() != NodeType_ElaboratedTypeSpecifier )
        parseNameUse(ast->name());
    TreeWalker::parseTypeSpecifier(ast);
}

/*
    Parses a name: looks up name, creates name use.
*/
void Semantic::parseNameUse(NameAST* name)
{
    if(!name)
        return;

    // Look up name
    QList<CodeModel::Member *> members = nameLookup(currentScope.top(), name);
    if(members.isEmpty()) {
        //cout << "no declaration found for " << textOf(name).constData() << endl;
        // Create NameUse that refer to a shared UnknownMember
        createNameUse(m_sharedUnknownMember, name);
        return;
    }

    //TODO: handle multiple members (when nameLookup returns a set of overloaded functions)
    CodeModel::Member *member = members[0];
    if(!member->parent()) {
        emit error("Error in Semantic::parseUsing: target has no parent scope");
        return;
    }

    createNameUse(member, name);
}

/*
    Looks up name used in basescope. If name->isGlobal() is true or if classOrNamespaceList()
    returns a non-empty list, the C++ qualified name lookup rules are used. Otherwise the
    unquialified name lookup rules are used.  Returns the a list of members that was found,
    In most cases this list will contain zero or one element, except in the case of overloaded functions.
    TODO: Argument-dependent name lookup
*/
QList<CodeModel::Member *> Semantic::nameLookup(CodeModel::Scope *baseScope, const NameAST* name)
{
    if (name->isGlobal() || (name->classOrNamespaceNameList()
                              && name->classOrNamespaceNameList()->size()>0 )) {
        return qualifiedNameLookup(baseScope, name);
    } else {
        return unqualifiedNameLookup(baseScope, name);
    }
}

//look up an unqualified name
QList<CodeModel::Member *> Semantic::unqualifiedNameLookup(CodeModel::Scope *baseScope, const NameAST* name)
{
    QList<UsingDirectiveLink *> usingDirectiveLinks;
    CodeModel::Scope *currentScope = baseScope;
    QList<CodeModel::Member *>  entities;

    while (currentScope != 0) {
        // Add any "using namespace" directive links for the current scope to
        // usingDirectiveLinks
        if (NamespaceScope *namespaceScope = currentScope->toNamespaceScope())
            usingDirectiveLinks += namespaceScope->usingDirectiveLinks();
        if (BlockScope *blockScope = currentScope->toBlockScope())
            usingDirectiveLinks += blockScope->usingDirectiveLinks();

        // Search usingDirectiveLinks for a link where currentScope is the
        // insertion namespace. If found look up name in the target namespace
        // for that link.
        if (NamespaceScope *namespaceScope = currentScope->toNamespaceScope()) {
            QList<UsingDirectiveLink *>::ConstIterator it = usingDirectiveLinks.constBegin();
            while (it != usingDirectiveLinks.constEnd()) {
                if ((*it)->insertionNamespace() == namespaceScope)
                    entities = lookupNameInScope((*it)->targetNamespace(), name);
                ++it;
            }
        }

        // Look up names in this scope.
        entities += lookupNameInScope(currentScope, name);
        if (!entities.isEmpty())
            break;
        currentScope = currentScope->parent();
    }
    return entities;
}

//look up a qualified name
QList<CodeModel::Member *> Semantic::qualifiedNameLookup(CodeModel::Scope *baseScope, const NameAST* name)
{
    QList<CodeModel::Member *> entities;
    CodeModel::Scope *currentScope = baseScope;

    // Check if the global ("::") scope has been specified.
    if(name->isGlobal()) {
        while (currentScope->parent())
            currentScope = currentScope->parent();
    }

    while (entities.isEmpty() && currentScope != 0) {
        CodeModel::Scope *targetScope = scopeLookup(currentScope, name);
        entities = lookupNameInScope(targetScope, name);
        currentScope = currentScope->parent();
    }

    return entities;
}

//looks up a name in a scope, includes base classes if scope is a class scope
QList<CodeModel::Member *> Semantic::lookupNameInScope(CodeModel::Scope *scope, const NameAST* name)
{
    QList<CodeModel::Member *> entities;

    if(!scope || !name)
        return entities;

    QByteArray nameText = textOf(name->unqualifiedName()->name());
    //look up name in members of current scope
    const CodeModel::MemberCollection members = scope->members();
    if (members.contains(nameText))
        entities.append(members.value(nameText));

    // if not found, look up name in  base classes (if any)
    CodeModel::ClassScope *classScope = scope->toClassScope();
    if (entities.isEmpty() && classScope) {
        const TypeCollection baseClasses = classScope->baseClasses();
        TypeCollection::ConstIterator it = baseClasses.constBegin();
        while (it != baseClasses.constEnd()) {
            CodeModel::Scope *baseClass = it.value()->toClassType()->scope();
            if (scope != baseClass)
                entities += lookupNameInScope(baseClass, name);
            ++it;
        }

        if (entities.count() > 1)
            emit error("Error in Semantic::lookupNameInScope: name "
            + nameText + " is ambigous");
    }
    return entities;
}

/*
    Resolves the classOrNamespaceNameList part of a NameAST against a base scope.
*/
CodeModel::Scope *Semantic::scopeLookup(CodeModel::Scope *baseScope, const NameAST* name)
{
    CodeModel::Scope *currentScope = baseScope;
    const List<ClassOrNamespaceNameAST *> *scopeList = name->classOrNamespaceNameList();
    // if there is no scope list, then the scope we are looking for is baseScope
    if (!scopeList)
        return baseScope;

    // Check if the global ("::") scope has been specified.
    if(name->isGlobal()) {
        while (currentScope->parent())
            currentScope = currentScope->parent();
    }

    while(currentScope != 0) {
        int nestingCounter = 0;
        CodeModel::Scope *nestedScope = currentScope;
        while (nestingCounter < scopeList->count()) {
            const QByteArray nameText = textOf((*scopeList)[nestingCounter]->name());
            nestedScope = nestedScope->scopes().value(nameText);
            if (!nestedScope)
                break;
            ++nestingCounter;
        }
        if(nestedScope) // found target scope?
            return nestedScope;

        currentScope = currentScope->parent(); //look in parent scope
    }

    return 0;
}

TypeMember *Semantic::typeLookup(CodeModel::Scope *baseScope, const NameAST* name)
{
    QList<CodeModel::Member *> memberList = nameLookup(baseScope, name);

    foreach(Member *member, memberList) {
        if(TypeMember *typeMember = member->toTypeMember())
            return typeMember;
    }
    return 0;
}

FunctionMember *Semantic::functionLookup(CodeModel::Scope *baseScope,
                                          const DeclaratorAST *functionDeclarator)
{

    QList<CodeModel::Member*> candidateList =
                nameLookup(baseScope, functionDeclarator->declaratorId());
    return selectFunction(candidateList, functionDeclarator);
}

/*
    This is a simplified function lookup routine, for matching member function
    definitions with member function declarations. It does not implement
    the general C++ function overload resolution rules.
*/
FunctionMember *Semantic::selectFunction(QList<CodeModel::Member*> candidatateList, const DeclaratorAST *functionDeclarator)
{
    // get arguments for function we are looking for
    FunctionMember testFunction;
    parseFunctionArguments(functionDeclarator, &testFunction);
    const ArgumentCollection testArgumentCollection = testFunction.arguments();

    //test againts functions in overload list.
    foreach(Member* member, candidatateList) {
        FunctionMember *function = member->toFunctionMember();
        if (!function)
            continue;
        const ArgumentCollection argumentCollection = function->arguments();

        //test argument types and number of arguments
        ArgumentCollection::ConstIterator arg1 = argumentCollection.constBegin();
        ArgumentCollection::ConstIterator arg2 = testArgumentCollection.constBegin();
        bool match = true;
        while(arg1 != argumentCollection.constEnd() && arg2 != testArgumentCollection.constEnd()) {
            if( arg1.value()->type()->name() != arg2.value()->type()->name() ) {
                match = false;
                break;
            }
            ++arg1;
            ++arg2;
        }
        if(match)
            return function;
    }
    return 0;
}

QByteArray Semantic::typeOfDeclaration(TypeSpecifierAST *typeSpec, DeclaratorAST *declarator)
{
    if (!typeSpec)
        return QByteArray();

    QByteArray text;

    if (typeSpec->cvQualify()) {
        List<AST*> cv = *typeSpec->cvQualify()->children();
        foreach (AST *current, cv) {
            text += " " + textOf(current);
        }
        text += " ";
    }


    text += textOf(typeSpec);

    if (typeSpec->cv2Qualify()) {
        List<AST*> cv = *typeSpec->cv2Qualify()->children();
        foreach (AST *current, cv) {
            text += textOf(current) + " ";
        }
    }

    if (declarator && declarator->ptrOpList()) {
        List<AST*> ptrOpList = *declarator->ptrOpList();
        foreach (AST *current, ptrOpList) {
            text += " " + textOf(current);
        }
        text += " ";
    }

    return text.trimmed().simplified();
}



QList<QByteArray> Semantic::scopeOfName(NameAST *id, const QList<QByteArray>& startScope)
{
    QList<QByteArray> scope = startScope;
    if (id && id->classOrNamespaceNameList()){
        if (id->isGlobal())
            scope.clear();

        List<ClassOrNamespaceNameAST*> l = *id->classOrNamespaceNameList();
        foreach (ClassOrNamespaceNameAST *current, l) {
            if (current->name())
               scope << textOf(current->name());
        }
    }

    return scope;
}

QList<QByteArray> Semantic::scopeOfDeclarator(DeclaratorAST *d, const QList<QByteArray>& startScope)
{
    if(!d)
        return QList<QByteArray>();
    return scopeOfName(d->declaratorId(), startScope);
}

QByteArray Semantic::typeSpecToString(TypeSpecifierAST* typeSpec)
{
    if (!typeSpec)
        return QByteArray();

    QByteArray tp;
    if (typeSpec->cvQualify()) {
        tp += "const ";
    }

    tp += (QString::fromLatin1(textOf(typeSpec)).replace(QRegExp(QLatin1String(" :: ")), QString::fromUtf8("::"))).toLatin1();
    return tp;
}

QByteArray Semantic::declaratorToString(DeclaratorAST* declarator, const QByteArray& scope, bool skipPtrOp)
{
    if (!declarator)
        return QByteArray();

    QByteArray text;

    if (!skipPtrOp && declarator->ptrOpList()){
        List<AST*> ptrOpList = *declarator->ptrOpList();
        foreach (AST *current, ptrOpList) {
            text += textOf(current);
        }
        text += QByteArray(" ");
    }

    text += scope;

    if (declarator->subDeclarator())
        text += QByteArray("(") + declaratorToString(declarator->subDeclarator()) + QByteArray(")");

    if (declarator->declaratorId())
        text += textOf(declarator->declaratorId());

    if (declarator->arrayDimensionList()) {
        List<AST*> arrays = *declarator->arrayDimensionList();
        foreach (AST *current, arrays) {
            current=current;    //silence unused symbol warning
            text += QByteArray("[]");
        }
    }

    if (declarator->parameterDeclarationClause()){
        text += QByteArray("(");

        ParameterDeclarationListAST* l = declarator->parameterDeclarationClause()->parameterDeclarationList();
        if (l != 0){
            List<ParameterDeclarationAST*> params = *l->parameterList();
            foreach (ParameterDeclarationAST *current, params) {
                QByteArray type = typeSpecToString(current->typeSpec());
                text += type;
                if (!type.isEmpty())
                    text += QByteArray(" ");
                text += declaratorToString(current->declarator());

                // ### FIXME if (it.current())
                    text += QByteArray(", ");
            }
        }

        text += QByteArray(")");

        if (declarator->constant() != 0)
            text += QByteArray(" const");
    }

    return QString::fromLatin1(text).replace(QRegExp(QLatin1String(" :: ")), QLatin1String("::")).simplified().toLatin1();
}

QByteArray Semantic::textOf(const AST *node) const
{
    if (!node)
        return QByteArray();
    QByteArray text;
    for (int i = node->startToken(); i < node->endToken(); ++i) {
        if (!m_tokenStream->isHidden(i)) {
            if (i != node->startToken())
                text += QByteArray(" ");
            text += m_tokenStream->tokenText(i);
        }
    }
    return text;
}

void Semantic::createNameUse(Member *member, NameAST *name)
{
    if (!name)
       return;

    AST *unqualifedName = name->unqualifiedName()->name();

    if(!unqualifedName || !member)
        return;

    CodeModel::NameUse *nameUse = CodeModel::Create<CodeModel::NameUse>(m_storage);
    nameUse->setParent(currentScope.top());
    nameUse->setNameToken(tokenRefFromAST(unqualifedName));
    nameUse->setName(textOf(unqualifedName));
    nameUse->setDeclaration(member);

    currentScope.top()->addNameUse(nameUse);
    addNameUse(unqualifedName, nameUse);
}

void Semantic::addNameUse(AST *node, NameUse *nameUse)
{
    const int tokenIndex = node->startToken();
    m_nameUses.insert(tokenIndex, nameUse);
}

/*
    Searches a AST node and all its children for a nameUse. The name use is
    found by looking up each node's tokens in the m_nameUses map. A depth-first
    search is used.
*/
NameUse *Semantic::findNameUse(AST *node)
{
    if(!node)
        return 0;

    List<AST*> *children = node->children();
    if(children) {
        NameUse *nameUse = 0;
        foreach(AST* child , *children) {
            nameUse = findNameUse(child);
            if(nameUse)
                break;
        }
        if (nameUse)
            return nameUse;
    }

    for (int t = node->startToken(); t < node->endToken(); ++t) {
 //       cout << t <<" |" <<m_tokenStream->tokenText(t).constData() << "|" << endl;
        if (m_nameUses.contains(t))
            return m_nameUses.value(t);
    }
    return 0;
}

/*
    Gets a TokenRef from an AST node.
    Assumes that the node only covers one token, which means that
    node->statToken() == node->endToken(). If this is not the case
    then the TokenRef will reference the token at startToken.
*/
TokenEngine::TokenRef Semantic::tokenRefFromAST(AST *node)
{
    const int startTokenIndex = node->startToken();
    const TokenEngine::TokenContainer tokenContainer = m_tokenStream->tokenContainer(startTokenIndex);
    const int containerIndex = m_tokenStream->containerIndex(startTokenIndex);
    return TokenEngine::TokenRef(tokenContainer, containerIndex);
}

QT_END_NAMESPACE