ResolveExpression.cpp 30 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
con's avatar
con committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42

#include "ResolveExpression.h"
#include "LookupContext.h"
#include "Overview.h"

#include <Control.h>
#include <AST.h>
#include <Scope.h>
#include <Names.h>
#include <Symbols.h>
#include <Literals.h>
#include <CoreTypes.h>
#include <TypeVisitor.h>
#include <NameVisitor.h>
hjk's avatar
hjk committed
43

hjk's avatar
hjk committed
44
#include <QtCore/QList>
Roberto Raggi's avatar
Roberto Raggi committed
45
#include <QtCore/QVarLengthArray>
hjk's avatar
hjk committed
46
#include <QtCore/QtDebug>
con's avatar
con committed
47
48
49

using namespace CPlusPlus;

hjk's avatar
hjk committed
50
namespace {
con's avatar
con committed
51
52
53

typedef QList< QPair<Name *, FullySpecifiedType> > Substitution;

Roberto Raggi's avatar
Roberto Raggi committed
54
class GenerateInstance: protected TypeVisitor, protected NameVisitor
con's avatar
con committed
55
56
57
58
59
60
{
    Control *_control;
    FullySpecifiedType _type;
    const Substitution _substitution;

public:
Roberto Raggi's avatar
Roberto Raggi committed
61
    GenerateInstance(Control *control, const Substitution &substitution)
con's avatar
con committed
62
63
64
65
66
67
68
69
70
71
        : _control(control),
          _substitution(substitution)
    { }

    FullySpecifiedType operator()(const FullySpecifiedType &ty)
    { return subst(ty); }

protected:
    FullySpecifiedType subst(Name *name)
    {
Roberto Raggi's avatar
Roberto Raggi committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
        if (TemplateNameId *t = name->asTemplateNameId()) {
            QVarLengthArray<FullySpecifiedType, 8> args(t->templateArgumentCount());

            for (unsigned i = 0; i < t->templateArgumentCount(); ++i)
                args[i] = subst(t->templateArgumentAt(i));

            TemplateNameId *n = _control->templateNameId(t->identifier(),
                                                         args.data(), args.size());

            return FullySpecifiedType(_control->namedType(n));
        } else if (name->isQualifiedNameId()) {
            // ### implement me
        }

con's avatar
con committed
86
87
88
89
90
91
        for (int i = 0; i < _substitution.size(); ++i) {
            const QPair<Name *, FullySpecifiedType> s = _substitution.at(i);
            if (name->isEqualTo(s.first))
                return s.second;
        }

Roberto Raggi's avatar
Roberto Raggi committed
92
        return FullySpecifiedType(_control->namedType(name));
con's avatar
con committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
    }

    FullySpecifiedType subst(const FullySpecifiedType &ty)
    {
        FullySpecifiedType previousType = switchType(ty);
        TypeVisitor::accept(ty.type());
        return switchType(previousType);
    }

    FullySpecifiedType switchType(const FullySpecifiedType &type)
    {
        FullySpecifiedType previousType = _type;
        _type = type;
        return previousType;
    }

    // types
    virtual void visit(PointerToMemberType * /*ty*/)
    {
112
        Q_ASSERT(false);
con's avatar
con committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
    }

    virtual void visit(PointerType *ty)
    {
        FullySpecifiedType elementType = subst(ty->elementType());
        _type.setType(_control->pointerType(elementType));
    }

    virtual void visit(ReferenceType *ty)
    {
        FullySpecifiedType elementType = subst(ty->elementType());
        _type.setType(_control->referenceType(elementType));
    }

    virtual void visit(ArrayType *ty)
    {
        FullySpecifiedType elementType = subst(ty->elementType());
        _type.setType(_control->arrayType(elementType, ty->size()));
    }

    virtual void visit(NamedType *ty)
Roberto Raggi's avatar
Roberto Raggi committed
134
135
136
137
    {
        Name *name = ty->name();
        _type.setType(subst(name).type());
    }
con's avatar
con committed
138
139
140
141
142
143
144
145

    virtual void visit(Function *ty)
    {
        Name *name = ty->name();
        FullySpecifiedType returnType = subst(ty->returnType());

        Function *fun = _control->newFunction(0, name);
        fun->setScope(ty->scope());
146
147
        fun->setConst(ty->isConst());
        fun->setVolatile(ty->isVolatile());
con's avatar
con committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
        fun->setReturnType(returnType);
        for (unsigned i = 0; i < ty->argumentCount(); ++i) {
            Symbol *arg = ty->argumentAt(i);
            FullySpecifiedType argTy = subst(arg->type());
            Argument *newArg = _control->newArgument(0, arg->name());
            newArg->setType(argTy);
            fun->arguments()->enterSymbol(newArg);
        }
        _type.setType(fun);
    }

    virtual void visit(VoidType *)
    { /* nothing to do*/ }

    virtual void visit(IntegerType *)
    { /* nothing to do*/ }

    virtual void visit(FloatType *)
    { /* nothing to do*/ }

    virtual void visit(Namespace *)
169
    { Q_ASSERT(false); }
con's avatar
con committed
170
171

    virtual void visit(Class *)
172
    { Q_ASSERT(false); }
con's avatar
con committed
173
174

    virtual void visit(Enum *)
175
    { Q_ASSERT(false); }
con's avatar
con committed
176
177
178

    // names
    virtual void visit(NameId *)
179
    { Q_ASSERT(false); }
con's avatar
con committed
180
181

    virtual void visit(TemplateNameId *)
182
    { Q_ASSERT(false); }
con's avatar
con committed
183
184

    virtual void visit(DestructorNameId *)
185
    { Q_ASSERT(false); }
con's avatar
con committed
186
187

    virtual void visit(OperatorNameId *)
188
    { Q_ASSERT(false); }
con's avatar
con committed
189
190

    virtual void visit(ConversionNameId *)
191
    { Q_ASSERT(false); }
con's avatar
con committed
192
193

    virtual void visit(QualifiedNameId *)
194
    { Q_ASSERT(false); }
con's avatar
con committed
195
196
};

197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
template <typename _Tp>
static QList<_Tp> removeDuplicates(const QList<_Tp> &results)
{
    QList<_Tp> uniqueList;
    QSet<_Tp> processed;
    foreach (const _Tp &r, results) {
        if (processed.contains(r))
            continue;

        processed.insert(r);
        uniqueList.append(r);
    }

    return uniqueList;
}

con's avatar
con committed
213
214
215
216
217
218
219
220
221
} // end of anonymous namespace

/////////////////////////////////////////////////////////////////////
// ResolveExpression
/////////////////////////////////////////////////////////////////////
ResolveExpression::ResolveExpression(const LookupContext &context)
    : ASTVisitor(context.expressionDocument()->control()),
      _context(context),
      sem(_context.control())
222
{ }
con's avatar
con committed
223
224
225
226
227
228
229
230

ResolveExpression::~ResolveExpression()
{ }

QList<ResolveExpression::Result> ResolveExpression::operator()(ExpressionAST *ast)
{
    const QList<Result> previousResults = switchResults(QList<Result>());
    accept(ast);
231
    return removeDuplicates(switchResults(previousResults));
con's avatar
con committed
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
}

QList<ResolveExpression::Result>
ResolveExpression::switchResults(const QList<ResolveExpression::Result> &results)
{
    const QList<Result> previousResults = _results;
    _results = results;
    return previousResults;
}

void ResolveExpression::addResults(const QList<Result> &results)
{
    foreach (const Result r, results)
        addResult(r);
}

void ResolveExpression::addResult(const FullySpecifiedType &ty, Symbol *symbol)
{ return addResult(Result(ty, symbol)); }

void ResolveExpression::addResult(const Result &r)
{
    Result p = r;
Roberto Raggi's avatar
Roberto Raggi committed
254

con's avatar
con committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    if (! p.second)
        p.second = _context.symbol();

    if (! _results.contains(p))
        _results.append(p);
}

QList<Scope *> ResolveExpression::visibleScopes(const Result &result) const
{ return _context.visibleScopes(result); }

bool ResolveExpression::visit(ExpressionListAST *)
{
    // nothing to do.
    return false;
}

271
bool ResolveExpression::visit(BinaryExpressionAST *ast)
con's avatar
con committed
272
{
273
    accept(ast->left_expression);
con's avatar
con committed
274
275
276
277
278
    return false;
}

bool ResolveExpression::visit(CastExpressionAST *ast)
{
279
    addResult(sem.check(ast->type_id, _context.expressionDocument()->globalSymbols()));
con's avatar
con committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
    return false;
}

bool ResolveExpression::visit(ConditionAST *)
{
    // nothing to do.
    return false;
}

bool ResolveExpression::visit(ConditionalExpressionAST *)
{
    // nothing to do.
    return false;
}

bool ResolveExpression::visit(CppCastExpressionAST *ast)
{
297
    addResult(sem.check(ast->type_id, _context.expressionDocument()->globalSymbols()));
con's avatar
con committed
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
    return false;
}

bool ResolveExpression::visit(DeleteExpressionAST *)
{
    // nothing to do.
    return false;
}

bool ResolveExpression::visit(ArrayInitializerAST *)
{
    // nothing to do.
    return false;
}

bool ResolveExpression::visit(NewExpressionAST *)
{
    // nothing to do.
    return false;
}

bool ResolveExpression::visit(TypeidExpressionAST *)
{
    Name *std_type_info[2];
    std_type_info[0] = control()->nameId(control()->findOrInsertIdentifier("std"));
    std_type_info[1] = control()->nameId(control()->findOrInsertIdentifier("type_info"));

    Name *q = control()->qualifiedNameId(std_type_info, 2, /*global=*/ true);
    FullySpecifiedType ty(control()->namedType(q));
    addResult(ty);

    return false;
}

bool ResolveExpression::visit(TypenameCallExpressionAST *)
{
    // nothing to do
    return false;
}

bool ResolveExpression::visit(TypeConstructorCallAST *)
{
    // nothing to do.
    return false;
}

bool ResolveExpression::visit(PostfixExpressionAST *ast)
{
    accept(ast->base_expression);

    for (PostfixAST *fx = ast->postfix_expressions; fx; fx = fx->next) {
        accept(fx);
    }

    return false;
}

bool ResolveExpression::visit(SizeofExpressionAST *)
{
    FullySpecifiedType ty(control()->integerType(IntegerType::Int));
    ty.setUnsigned(true);
    addResult(ty);
    return false;
}

363
bool ResolveExpression::visit(NumericLiteralAST *ast)
con's avatar
con committed
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
    Type *type = 0;
    NumericLiteral *literal = numericLiteral(ast->literal_token);

    if (literal->isChar())
        type = control()->integerType(IntegerType::Char);
    else if (literal->isWideChar())
        type = control()->integerType(IntegerType::WideChar);
    else if (literal->isInt())
        type = control()->integerType(IntegerType::Int);
    else if (literal->isLong())
        type = control()->integerType(IntegerType::Long);
    else if (literal->isLongLong())
        type = control()->integerType(IntegerType::LongLong);
    else if (literal->isFloat())
        type = control()->floatType(FloatType::Float);
    else if (literal->isDouble())
        type = control()->floatType(FloatType::Double);
    else if (literal->isLongDouble())
        type = control()->floatType(FloatType::LongDouble);
    else
        type = control()->integerType(IntegerType::Int);

    FullySpecifiedType ty(type);
    if (literal->isUnsigned())
        ty.setUnsigned(true);

con's avatar
con committed
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
    addResult(ty);
    return false;
}

bool ResolveExpression::visit(BoolLiteralAST *)
{
    FullySpecifiedType ty(control()->integerType(IntegerType::Bool));
    addResult(ty);
    return false;
}

bool ResolveExpression::visit(ThisExpressionAST *)
{
    if (! _context.symbol())
        return false;

    Scope *scope = _context.symbol()->scope();
    for (; scope; scope = scope->enclosingScope()) {
        if (scope->isFunctionScope()) {
            Function *fun = scope->owner()->asFunction();
            if (Scope *cscope = scope->enclosingClassScope()) {
                Class *klass = cscope->owner()->asClass();
                FullySpecifiedType classTy(control()->namedType(klass->name()));
                FullySpecifiedType ptrTy(control()->pointerType(classTy));
                addResult(ptrTy, fun);
                break;
            } else if (QualifiedNameId *q = fun->name()->asQualifiedNameId()) {
                Name *nestedNameSpecifier = 0;
                if (q->nameCount() == 1 && q->isGlobal())
                    nestedNameSpecifier = q->nameAt(0);
                else
                    nestedNameSpecifier = control()->qualifiedNameId(q->names(), q->nameCount() - 1);
                FullySpecifiedType classTy(control()->namedType(nestedNameSpecifier));
                FullySpecifiedType ptrTy(control()->pointerType(classTy));
                addResult(ptrTy, fun);
                break;
            }
        }
    }
    return false;
}

bool ResolveExpression::visit(NestedExpressionAST *ast)
{
    accept(ast->expression);
    return false;
}

bool ResolveExpression::visit(StringLiteralAST *)
{
    FullySpecifiedType charTy = control()->integerType(IntegerType::Char);
    charTy.setConst(true);
    FullySpecifiedType ty(control()->pointerType(charTy));
    addResult(ty);
    return false;
}

bool ResolveExpression::visit(ThrowExpressionAST *)
{
    return false;
}

bool ResolveExpression::visit(TypeIdAST *)
{
    return false;
}

bool ResolveExpression::visit(UnaryExpressionAST *ast)
{
    accept(ast->expression);
    unsigned unaryOp = tokenKind(ast->unary_op_token);
    if (unaryOp == T_AMPER) {
        QMutableListIterator<Result > it(_results);
        while (it.hasNext()) {
            Result p = it.next();
            p.first.setType(control()->pointerType(p.first));
            it.setValue(p);
        }
    } else if (unaryOp == T_STAR) {
        QMutableListIterator<Result > it(_results);
        while (it.hasNext()) {
            Result p = it.next();
            if (PointerType *ptrTy = p.first->asPointerType()) {
                p.first = ptrTy->elementType();
                it.setValue(p);
            } else {
                it.remove();
            }
        }
    }
    return false;
}

484
485
486
487
488
489
bool ResolveExpression::visit(CompoundLiteralAST *ast)
{
    accept(ast->type_id);
    return false;
}

con's avatar
con committed
490
491
bool ResolveExpression::visit(QualifiedNameAST *ast)
{
492
    ResolveClass resolveClass;
493
    Name *name = ast->name;
con's avatar
con committed
494
495
496
497
498

    QList<Symbol *> symbols = _context.resolve(name);
    foreach (Symbol *symbol, symbols) {
        if (symbol->isTypedef()) {
            if (NamedType *namedTy = symbol->type()->asNamedType()) {
Roberto Raggi's avatar
Roberto Raggi committed
499
                const Result r(namedTy, symbol);
500
                const QList<Symbol *> resolvedClasses =
501
                        resolveClass(namedTy->name(), r, _context);
con's avatar
con committed
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
                if (resolvedClasses.count()) {
                    foreach (Symbol *s, resolvedClasses) {
                        addResult(s->type(), s);
                    }
                    continue;
                }
            }
        }
        addResult(symbol->type(), symbol);
    }
    return false;
}

bool ResolveExpression::visit(OperatorFunctionIdAST *)
{
    return false;
}

bool ResolveExpression::visit(ConversionFunctionIdAST *)
{
    return false;
}

bool ResolveExpression::visit(SimpleNameAST *ast)
{
527
    QList<Symbol *> symbols = _context.resolve(ast->name);
con's avatar
con committed
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
    foreach (Symbol *symbol, symbols)
        addResult(symbol->type(), symbol);

    return false;
}

bool ResolveExpression::visit(DestructorNameAST *)
{
    FullySpecifiedType ty(control()->voidType());
    addResult(ty);
    return false;
}

bool ResolveExpression::visit(TemplateIdAST *ast)
{
543
    QList<Symbol *> symbols = _context.resolve(ast->name);
con's avatar
con committed
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
    foreach (Symbol *symbol, symbols)
        addResult(symbol->type(), symbol);

    return false;
}

bool ResolveExpression::visit(CallAST *ast)
{
    // Compute the types of the actual arguments.
    QList< QList<Result> > arguments;
    for (ExpressionListAST *exprIt = ast->expression_list; exprIt;
            exprIt = exprIt->next) {
        arguments.append(operator()(exprIt->expression));
    }

    QList<Result> baseResults = _results;
    _results.clear();

    foreach (Result p, baseResults) {
563
        if (Function *funTy = p.first->asFunctionType()) {
con's avatar
con committed
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
            unsigned minNumberArguments = 0;
            for (; minNumberArguments < funTy->argumentCount(); ++minNumberArguments) {
                Argument *arg = funTy->argumentAt(minNumberArguments)->asArgument();
                if (arg->hasInitializer())
                    break;
            }
            const unsigned actualArgumentCount = arguments.count();
            if (actualArgumentCount < minNumberArguments) {
                // not enough arguments.
            } else if (! funTy->isVariadic() && actualArgumentCount > funTy->argumentCount()) {
                // too many arguments.
            } else {
                p.first = funTy->returnType();
                addResult(p);
            }
579
        } else if (Class *classTy = p.first->asClassType()) {
con's avatar
con committed
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
            // Constructor call
            p.first = control()->namedType(classTy->name());
            addResult(p);
        }
    }

    return false;
}

bool ResolveExpression::visit(ArrayAccessAST *ast)
{
    const QList<Result> baseResults = _results;
    _results.clear();

    const QList<Result> indexResults = operator()(ast->expression);
595
    ResolveClass symbolsForDotAcccess;
con's avatar
con committed
596
597
598
599
600
601
602
603
604
605
606
607
608

    foreach (Result p, baseResults) {
        FullySpecifiedType ty = p.first;
        Symbol *contextSymbol = p.second;

        if (ReferenceType *refTy = ty->asReferenceType())
            ty = refTy->elementType();

        if (PointerType *ptrTy = ty->asPointerType()) {
            addResult(ptrTy->elementType(), contextSymbol);
        } else if (ArrayType *arrTy = ty->asArrayType()) {
            addResult(arrTy->elementType(), contextSymbol);
        } else if (NamedType *namedTy = ty->asNamedType()) {
Roberto Raggi's avatar
Roberto Raggi committed
609
            const QList<Symbol *> classObjectCandidates =
610
                    symbolsForDotAcccess(namedTy->name(), p, _context);
con's avatar
con committed
611
612

            foreach (Symbol *classObject, classObjectCandidates) {
613
614
                const QList<Result> overloads =
                        resolveArrayOperator(p, namedTy, classObject->asClass());
con's avatar
con committed
615
616
                foreach (Result r, overloads) {
                    FullySpecifiedType ty = r.first;
617
                    Function *funTy = ty->asFunctionType();
con's avatar
con committed
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
                    if (! funTy)
                        continue;

                    ty = funTy->returnType();
                    addResult(ty, funTy);
                }
            }
        }
    }
    return false;
}

bool ResolveExpression::visit(MemberAccessAST *ast)
{
    // The candidate types for the base expression are stored in
    // _results.
    QList<Result> baseResults = _results;

    // Evaluate the expression-id that follows the access operator.
637
638
639
    Name *memberName = 0;
    if (ast->member_name)
        memberName = ast->member_name->name;
con's avatar
con committed
640
641

    // Remember the access operator.
642
    const int accessOp = tokenKind(ast->access_token);
con's avatar
con committed
643
644
645
646
647
648

    _results = resolveMemberExpression(baseResults, accessOp, memberName);

    return false;
}

649
QList<ResolveExpression::Result>
Roberto Raggi's avatar
Roberto Raggi committed
650
651
ResolveExpression::resolveBaseExpression(const QList<Result> &baseResults, int accessOp,
                                         bool *replacedDotOperator) const
con's avatar
con committed
652
{
653
    QList<Result> results;
con's avatar
con committed
654

655
    if (baseResults.isEmpty())
656
        return results;
con's avatar
con committed
657

658
    Result result = baseResults.first();
659
660
    FullySpecifiedType ty = result.first.simplified();
    Symbol *lastVisibleSymbol = result.second;
con's avatar
con committed
661

662
    if (accessOp == T_ARROW)  {
663
664
665
        if (lastVisibleSymbol && ty->isClassType() && ! lastVisibleSymbol->isClass()) {
            // ### remove ! lastVisibleSymbol->isClass() from the condition.
            results.append(Result(ty, lastVisibleSymbol));
con's avatar
con committed
666

667
668
669
        } else if (NamedType *namedTy = ty->asNamedType()) {
            // ### This code is pretty slow.
            const QList<Symbol *> candidates = _context.resolve(namedTy->name());
670

671
672
            foreach (Symbol *candidate, candidates) {
                if (candidate->isTypedef()) {
673
674
675
676
                    FullySpecifiedType declTy = candidate->type().simplified();
                    const ResolveExpression::Result r(declTy, candidate);

                    // update the result
677
                    result = r;
678
679
680
681

                    // refresh the cached ty and lastVisibileSymbol.
                    ty = result.first.simplified();
                    lastVisibleSymbol = result.second;
682
                    break;
683
                }
684
685
686
687
688
            }
        }

        if (NamedType *namedTy = ty->asNamedType()) {
            ResolveClass resolveClass;
689

690
            const QList<Symbol *> candidates = resolveClass(namedTy->name(), result, _context);
691
692
693
            foreach (Symbol *classObject, candidates) {
                const QList<Result> overloads = resolveArrowOperator(result, namedTy,
                                                                     classObject->asClass());
694

695
696
697
698
                foreach (const Result &r, overloads) {
                    FullySpecifiedType typeOfOverloadFunction = r.first.simplified();
                    Symbol *lastVisibleSymbol = r.second;
                    Function *funTy = typeOfOverloadFunction->asFunctionType();
699
700
                    if (! funTy)
                        continue;
701

702
                    typeOfOverloadFunction = funTy->returnType().simplified();
703

704
705
                    if (PointerType *ptrTy = typeOfOverloadFunction->asPointerType()) {
                        FullySpecifiedType elementTy = ptrTy->elementType().simplified();
706

707
708
                        if (elementTy->isNamedType())
                            results.append(Result(elementTy, lastVisibleSymbol));
con's avatar
con committed
709
710
711
                    }
                }
            }
712
        } else if (PointerType *ptrTy = ty->asPointerType()) {
713
            FullySpecifiedType elementTy = ptrTy->elementType().simplified();
con's avatar
con committed
714

715
716
            if (elementTy->isNamedType() || elementTy->isClassType())
                results.append(Result(elementTy, lastVisibleSymbol));
717
718
        }
    } else if (accessOp == T_DOT) {
Roberto Raggi's avatar
Roberto Raggi committed
719
720
721
722
723
724
725
726
727
728
        if (replacedDotOperator) {
            if (PointerType *ptrTy = ty->asPointerType()) {
                *replacedDotOperator = true;
                ty = ptrTy->elementType().simplified();
            } else if (ArrayType *arrTy = ty->asArrayType()) {
                *replacedDotOperator = true;
                ty = arrTy->elementType().simplified();
            }
        }

729
730
        if (ty->isClassType() || ty->isNamedType())
            results.append(Result(ty, lastVisibleSymbol));
con's avatar
con committed
731

732
733
734
735
736
737
        if (Function *fun = ty->asFunctionType()) {
            Scope *funScope = fun->scope();

            if (funScope && (funScope->isBlockScope() || funScope->isNamespaceScope())) {
                FullySpecifiedType retTy = fun->returnType().simplified();
                results.append(Result(retTy, lastVisibleSymbol));
738
            }
con's avatar
con committed
739
740
741
        }
    }

742
    return removeDuplicates(results);
con's avatar
con committed
743
744
745
}

QList<ResolveExpression::Result>
746
747
ResolveExpression::resolveMemberExpression(const QList<Result> &baseResults,
                                           unsigned accessOp,
Roberto Raggi's avatar
Roberto Raggi committed
748
749
                                           Name *memberName,
                                           bool *replacedDotOperator) const
con's avatar
con committed
750
{
751
    ResolveClass resolveClass;
752
    QList<Result> results;
Roberto Raggi's avatar
Roberto Raggi committed
753

Roberto Raggi's avatar
Roberto Raggi committed
754
    const QList<Result> classObjectResults = resolveBaseExpression(baseResults, accessOp, replacedDotOperator);
755
756
757
758
759
    foreach (const Result &r, classObjectResults) {
        FullySpecifiedType ty = r.first;

        if (Class *klass = ty->asClassType())
            results += resolveMember(memberName, klass);
Roberto Raggi's avatar
Roberto Raggi committed
760

761
762
763
764
765
766
767
768
769
        else if (NamedType *namedTy = ty->asNamedType()) {
            Name *className = namedTy->name();
            const QList<Symbol *> classes = resolveClass(className, r, _context);

            foreach (Symbol *c, classes) {
                if (Class *klass = c->asClass())
                    results += resolveMember(memberName, klass, className);
            }
        }
con's avatar
con committed
770
    }
771

772
    return removeDuplicates(results);
con's avatar
con committed
773
774
775
}

QList<ResolveExpression::Result>
776
777
ResolveExpression::resolveMember(Name *memberName, Class *klass,
                                 Name *className) const
con's avatar
con committed
778
{
779
780
    QList<Result> results;

781
782
783
784
785
786
    if (! className)
        className = klass->name();

    if (! className)
        return results;

con's avatar
con committed
787
788
789
790
    QList<Scope *> scopes;
    _context.expand(klass->members(), _context.visibleScopes(), &scopes);

    QList<Symbol *> candidates = _context.resolve(memberName, scopes);
791

con's avatar
con committed
792
793
    foreach (Symbol *candidate, candidates) {
        FullySpecifiedType ty = candidate->type();
794
795
796
797
798
799
800
801
802
803
804
805
806
807
        Name *unqualifiedNameId = className;
        
        if (QualifiedNameId *q = className->asQualifiedNameId())
            unqualifiedNameId = q->unqualifiedNameId();
        
        if (TemplateNameId *templId = unqualifiedNameId->asTemplateNameId()) {
            Substitution subst;
            
            for (unsigned i = 0; i < templId->templateArgumentCount(); ++i) {
                FullySpecifiedType templArgTy = templId->templateArgumentAt(i);
                
                if (i < klass->templateParameterCount())
                    subst.append(qMakePair(klass->templateParameterAt(i)->name(),
                                           templArgTy));
con's avatar
con committed
808
            }
809
            
Roberto Raggi's avatar
Roberto Raggi committed
810
            GenerateInstance inst(control(), subst);
811
            ty = inst(ty);
con's avatar
con committed
812
        }
813
814
        
        results.append(Result(ty, candidate));
con's avatar
con committed
815
816
    }

817
    return removeDuplicates(results);
con's avatar
con committed
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
}

QList<ResolveExpression::Result>
ResolveExpression::resolveArrowOperator(const Result &,
                                        NamedType *namedTy,
                                        Class *klass) const
{
    QList<Scope *> scopes;
    _context.expand(klass->members(), _context.visibleScopes(), &scopes);
    QList<Result> results;

    Name *memberName = control()->operatorNameId(OperatorNameId::ArrowOp);
    QList<Symbol *> candidates = _context.resolve(memberName, scopes);
    foreach (Symbol *candidate, candidates) {
        FullySpecifiedType ty = candidate->type();
        Name *unqualifiedNameId = namedTy->name();
        if (QualifiedNameId *q = namedTy->name()->asQualifiedNameId())
            unqualifiedNameId = q->unqualifiedNameId();
        if (TemplateNameId *templId = unqualifiedNameId->asTemplateNameId()) {
            Substitution subst;
            for (unsigned i = 0; i < templId->templateArgumentCount(); ++i) {
                FullySpecifiedType templArgTy = templId->templateArgumentAt(i);
                if (i < klass->templateParameterCount()) {
                    subst.append(qMakePair(klass->templateParameterAt(i)->name(),
                                           templArgTy));
                }
            }
Roberto Raggi's avatar
Roberto Raggi committed
845
            GenerateInstance inst(control(), subst);
con's avatar
con committed
846
847
848
849
            ty = inst(ty);
        }

        const Result result(ty, candidate);
850
        results.append(result);
con's avatar
con committed
851
852
    }

853
    return removeDuplicates(results);
con's avatar
con committed
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
}

QList<ResolveExpression::Result>
ResolveExpression::resolveArrayOperator(const Result &,
                                        NamedType *namedTy,
                                        Class *klass) const
{
    // ### todo handle index expressions.

    QList<Scope *> scopes;
    _context.expand(klass->members(), _context.visibleScopes(), &scopes);
    QList<Result> results;

    Name *memberName = control()->operatorNameId(OperatorNameId::ArrayAccessOp);
    QList<Symbol *> candidates = _context.resolve(memberName, scopes);
    foreach (Symbol *candidate, candidates) {
        FullySpecifiedType ty = candidate->type();
        Name *unqualifiedNameId = namedTy->name();
        if (QualifiedNameId *q = namedTy->name()->asQualifiedNameId())
            unqualifiedNameId = q->unqualifiedNameId();
        if (TemplateNameId *templId = unqualifiedNameId->asTemplateNameId()) {
            Substitution subst;
            for (unsigned i = 0; i < templId->templateArgumentCount(); ++i) {
                FullySpecifiedType templArgTy = templId->templateArgumentAt(i);
                if (i < klass->templateParameterCount()) {
                    subst.append(qMakePair(klass->templateParameterAt(i)->name(),
                                           templArgTy));
                }
            }
Roberto Raggi's avatar
Roberto Raggi committed
883
            GenerateInstance inst(control(), subst);
con's avatar
con committed
884
885
886
            ty = inst(ty);
        }

887
        results.append(Result(ty, candidate));
con's avatar
con committed
888
889
    }

890
    return removeDuplicates(results);
con's avatar
con committed
891
892
893
894
895
896
}

bool ResolveExpression::visit(PostIncrDecrAST *)
{
    return false;
}
Roberto Raggi's avatar
Roberto Raggi committed
897
898

////////////////////////////////////////////////////////////////////////////////
899
ResolveClass::ResolveClass()
900
901
{ }

902
903
QList<Symbol *> ResolveClass::operator()(Name *name,
                                         const ResolveExpression::Result &p,
904
                                         const LookupContext &context)
905
906
{
    const QList<ResolveExpression::Result> previousBlackList = _blackList;
907
    const QList<Symbol *> symbols = resolveClass(name, p, context);
908
909
910
911
    _blackList = previousBlackList;
    return symbols;
}

912
913
QList<Symbol *> ResolveClass::resolveClass(Name *name,
                                           const ResolveExpression::Result &p,
914
                                           const LookupContext &context)
Roberto Raggi's avatar
Roberto Raggi committed
915
916
917
918
919
920
921
922
923
{
    QList<Symbol *> resolvedSymbols;

    if (_blackList.contains(p))
        return resolvedSymbols;

    _blackList.append(p);

    const QList<Symbol *> candidates =
924
            context.resolve(name, context.visibleScopes(p));
Roberto Raggi's avatar
Roberto Raggi committed
925
926
927
928
929
930
931
932

    foreach (Symbol *candidate, candidates) {
        if (Class *klass = candidate->asClass()) {
            if (resolvedSymbols.contains(klass))
                continue; // we already know about `klass'
            resolvedSymbols.append(klass);
        } else if (candidate->isTypedef()) {
            if (Declaration *decl = candidate->asDeclaration()) {
Roberto Raggi's avatar
Roberto Raggi committed
933
                if (Class *asClass = decl->type()->asClassType()) {
Roberto Raggi's avatar
Roberto Raggi committed
934
935
936
937
938
939
940
941
                    // typedef struct { } Point;
                    // Point pt;
                    // pt.
                    resolvedSymbols.append(asClass);
                } else {
                    // typedef Point Boh;
                    // Boh b;
                    // b.
942
943
944
945
946
                    FullySpecifiedType declType = decl->type().simplified();
                    if (NamedType *namedTy = declType->asNamedType()) {
                        const ResolveExpression::Result r(declType, decl);
                        resolvedSymbols += resolveClass(namedTy->name(), r, context);
                    }
Roberto Raggi's avatar
Roberto Raggi committed
947
948
949
                }
            }
        } else if (Declaration *decl = candidate->asDeclaration()) {
950
            if (Function *funTy = decl->type()->asFunctionType()) {
Roberto Raggi's avatar
Roberto Raggi committed
951
952
                // QString foo("ciao");
                // foo.
953
                if (funTy->scope() && (funTy->scope()->isBlockScope() || funTy->scope()->isNamespaceScope())) {
954
955
956
957
958
                    FullySpecifiedType retTy = funTy->returnType().simplified();
                    if (NamedType *namedTy = retTy->asNamedType()) {
                        const ResolveExpression::Result r(retTy, decl);
                        resolvedSymbols += resolveClass(namedTy->name(), r, context);
                    }
Roberto Raggi's avatar
Roberto Raggi committed
959
960
961
962
963
964
965
                }
            }
        }
    }

    return resolvedSymbols;
}