~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to languages/cpp/cppduchain/expressionvisitor.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2010-05-05 07:21:55 UTC
  • mfrom: (1.2.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505072155-h78lx19pu04sbhtn
Tags: 4:4.0.0-2
* Upload to unstable (Closes: #579947, #481832).
* Acknowledge obsolete NMU fixes (Closes: #562410, #546961).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright 2007 David Nolden <david.nolden.kdevelop@art-master.de>
 
3
 
 
4
   This library is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU Library General Public
 
6
   License version 2 as published by the Free Software Foundation.
 
7
 
 
8
   This library is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
   Library General Public License for more details.
 
12
 
 
13
   You should have received a copy of the GNU Library General Public License
 
14
   along with this library; see the file COPYING.LIB.  If not, write to
 
15
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
16
   Boston, MA 02110-1301, USA.
 
17
*/
 
18
 
 
19
#include "expressionvisitor.h"
 
20
 
 
21
#include <language/duchain/duchainlock.h>
 
22
#include <language/duchain/duchain.h>
 
23
#include <parsesession.h>
 
24
#include <language/duchain/declaration.h>
 
25
#include <language/duchain/functiondefinition.h>
 
26
#include <language/duchain/types/identifiedtype.h>
 
27
#include <typeinfo>
 
28
#include <util/pushvalue.h>
 
29
#include "tokens.h"
 
30
#include "typebuilder.h"
 
31
#include "cpptypes.h"
 
32
#include <language/duchain/dumpchain.h>
 
33
#include "typeutils.h"
 
34
#include "name_visitor.h"
 
35
#include "type_visitor.h"
 
36
#include "lexer.h"
 
37
#include "overloadresolution.h"
 
38
#include "cppduchain.h"
 
39
#include "overloadresolutionhelper.h"
 
40
#include "builtinoperators.h"
 
41
#include "qtfunctiondeclaration.h"
 
42
#include "missingdeclarationtype.h"
 
43
#include "missingdeclarationproblem.h"
 
44
#include "dumpchain.h"
 
45
 
 
46
//If this is enabled and a type is not found, it is searched again with verbose debug output.
 
47
//#define DEBUG_RESOLUTION_PROBLEMS
 
48
 
 
49
//If this is enabled, all encounterd problems will be dumped to kDebug
 
50
// #define DUMP_PROBLEMS
 
51
 
 
52
//If this is enabled, problems will be created when no overloaded function was found for a function-call. This is expensive,
 
53
//because the problem report contains a lot of information, and the problem currently appears very often.
 
54
//#define DEBUG_FUNCTION_CALLS
 
55
 
 
56
const uint maxExpressionVisitorProblems = 400;
 
57
 
 
58
///Remember to always when visiting a node create a PushPositiveValue object for the context
 
59
 
 
60
/** A typical expression:
 
61
 | | \ExpressionStatement[(39) (0, 92)] "d -> a = 5 ;"
 
62
| | | | \BinaryExpression[(39) (0, 92)] "d -> a = 5"
 
63
| | | | | \PostfixExpression[(39) (0, 92)] "d -> a"
 
64
| | | | | | \PrimaryExpression[(39) (0, 92)] "d"
 
65
| | | | | | | \Name[(39) (0, 92)] "d"
 
66
| | | | | | | | \UnqualifiedName[(39) (0, 92)] "d"
 
67
| | | | | | | | /UnqualifiedName[(40) (0, 93)]
 
68
| | | | | | | /Name[(40) (0, 93)]
 
69
| | | | | | /PrimaryExpression[(40) (0, 93)]
 
70
| | | | | | \ClassMemberAccess[(40) (0, 93)] "-> a"
 
71
| | | | | | | \Name[(41) (0, 95)] "a"
 
72
| | | | | | | | \UnqualifiedName[(41) (0, 95)] "a"
 
73
| | | | | | | | /UnqualifiedName[(42) (0, 97)]
 
74
| | | | | | | /Name[(42) (0, 97)]
 
75
| | | | | | /ClassMemberAccess[(42) (0, 97)]
 
76
| | | | | /PostfixExpression[(42) (0, 97)]
 
77
| | | | | \PrimaryExpression[(43) (0, 99)] "5"
 
78
| | | | | /PrimaryExpression[(44) (0, 100)]
 
79
| | | | /BinaryExpression[(44) (0, 100)]
 
80
| | | /ExpressionStatement[(45) (0, 102)
 
81
*/
 
82
 
 
83
/**
 
84
 * @todo Deal DelayedType correctly everywhere.
 
85
 * When a DelayedType is encountered, it should be filled with the
 
86
 * appropriate expression to compute the type/value later on.
 
87
 * */
 
88
 
 
89
#define LOCKDUCHAIN     DUChainReadLocker lock(DUChain::lock())
 
90
#define MUST_HAVE(X) if(!X) { problem( node, "no " # X ); return; }
 
91
 
 
92
namespace Cpp {
 
93
using namespace KDevelop;
 
94
using namespace TypeUtils;
 
95
 
 
96
bool isNumber( const IndexedString& str ) {
 
97
  static IndexedString _0("0");
 
98
  static IndexedString _1("1");
 
99
  static IndexedString _2("2");
 
100
  static IndexedString _3("3");
 
101
  static IndexedString _4("4");
 
102
  static IndexedString _5("5");
 
103
  static IndexedString _6("6");
 
104
  static IndexedString _7("7");
 
105
  static IndexedString _8("8");
 
106
  static IndexedString _9("9");
 
107
  if( str.isEmpty() )
 
108
    return false;
 
109
  return str == _0 || str == _1 || str == _2 || str == _3 || str == _4 || str == _5 || str == _6 || str == _7 || str == _8 || str == _9;
 
110
}
 
111
 
 
112
QHash<int, QString> initOperatorNames() {
 
113
  QHash<int, QString> ret;
 
114
  ret['+'] = "+";
 
115
  ret['-'] = "-";
 
116
  ret['*'] = "*";
 
117
  ret['/'] = "/";
 
118
  ret['%'] = "%";
 
119
  ret['^'] = "^";
 
120
  ret['&'] = "&";
 
121
  ret['|'] = "|";
 
122
  ret['~'] = "~";
 
123
  ret['!'] = "!";
 
124
  ret['='] = "=";
 
125
  ret['<'] = "<";
 
126
  ret['>'] = ">";
 
127
  ret[','] = ",";
 
128
  ret[Token_assign] = "=";
 
129
  ret[Token_shift] = "<<"; ///@todo Parser does not differentiate between << and >>
 
130
  ret[Token_eq] = "==";
 
131
  ret[Token_not_eq] = "!=";
 
132
  ret[Token_leq] = "<=";
 
133
  ret[Token_geq] = ">=";
 
134
  ret[Token_not_eq] = "!=";
 
135
  ret[Token_and] = "&&";
 
136
  ret[Token_or] = "||";
 
137
 
 
138
  return ret;
 
139
}
 
140
 
 
141
QHash<int, QString> operatorNames = initOperatorNames();
 
142
 
 
143
QString operatorNameFromTokenKind( int tokenKind )
 
144
{
 
145
  QHash<int, QString>::const_iterator it = operatorNames.constFind(tokenKind);
 
146
  if( it == operatorNames.constEnd() )
 
147
    return QString();
 
148
  else
 
149
    return *it;
 
150
}
 
151
 
 
152
QList<DeclarationPointer> convert( const QList<Declaration*>& list ) {
 
153
  QList<DeclarationPointer> ret;
 
154
  foreach( Declaration* decl, list )
 
155
    ret << DeclarationPointer(decl);
 
156
  return ret;
 
157
}
 
158
 
 
159
QList<Declaration*> convert( const QList<DeclarationPointer>& list ) {
 
160
  QList<Declaration*> ret;
 
161
  foreach( const DeclarationPointer &decl, list )
 
162
    if( decl )
 
163
      ret << decl.data();
 
164
  return ret;
 
165
}
 
166
 
 
167
template <class _Tp>
 
168
void ExpressionVisitor::visitIndependentNodes(const ListNode<_Tp> *nodes)
 
169
{
 
170
  if (!nodes)
 
171
    return;
 
172
 
 
173
  AbstractType::Ptr oldLastType = m_lastType;
 
174
  Instance oldLastInstance = m_lastInstance;
 
175
 
 
176
  const ListNode<_Tp>
 
177
    *it = nodes->toFront(),
 
178
    *end = it;
 
179
 
 
180
  do
 
181
    {
 
182
      m_lastType =  oldLastType;
 
183
      m_lastInstance = oldLastInstance;
 
184
 
 
185
      visit(it->element);
 
186
      it = it->next;
 
187
    }
 
188
  while (it != end);
 
189
}
 
190
 
 
191
typedef PushPositiveValue<DUContext*> PushPositiveContext;
 
192
 
 
193
const Token& ExpressionVisitor::tokenFromIndex( int index ) {
 
194
  return m_session->token_stream->token(index);
 
195
}
 
196
 
 
197
 
 
198
typedef PushValue<AbstractType::Ptr> PushAbstractType;
 
199
 
 
200
TopDUContext* ExpressionVisitor::topContext() const {
 
201
  if( m_source ) {
 
202
    return const_cast<TopDUContext*>(m_source); ///@todo remove const_cast
 
203
  }else{
 
204
    return m_topContext;
 
205
  }
 
206
}
 
207
 
 
208
bool ExpressionVisitor::isLValue( const AbstractType::Ptr& type, const Instance& instance ) {
 
209
  return instance && (instance.declaration || isReferenceType(type));
 
210
}
 
211
 
 
212
ExpressionVisitor::ExpressionVisitor(ParseSession* session, const KDevelop::TopDUContext* source, bool strict) : m_strict(strict), m_memberAccess(false), m_skipLastNamePart(false), m_source(source), m_ignore_uses(0), m_session(session), m_currentContext(0), m_topContext(0), m_reportRealProblems(false) {
 
213
}
 
214
 
 
215
ExpressionVisitor::~ExpressionVisitor() {
 
216
}
 
217
 
 
218
QList<DeclarationPointer> ExpressionVisitor::lastDeclarations() const {
 
219
  return m_lastDeclarations;
 
220
}
 
221
 
 
222
 
 
223
ParseSession* ExpressionVisitor::session() {
 
224
  return m_session;
 
225
}
 
226
 
 
227
void ExpressionVisitor::parse( AST* ast ) {
 
228
  m_lastType = 0;
 
229
  m_lastInstance = Instance();
 
230
  Q_ASSERT(ast->ducontext);
 
231
  m_topContext = ast->ducontext->topContext();
 
232
  visit(ast);
 
233
  m_topContext = 0;
 
234
  flushUse();
 
235
}
 
236
 
 
237
void ExpressionVisitor::parseNamePrefix( NameAST* ast ) {
 
238
  m_skipLastNamePart = true;
 
239
  parse(ast);
 
240
  m_skipLastNamePart = false;
 
241
}
 
242
 
 
243
void ExpressionVisitor::reportRealProblems(bool report) {
 
244
  m_reportRealProblems = report;
 
245
}
 
246
 
 
247
QList< KSharedPtr< KDevelop::Problem > > ExpressionVisitor::realProblems() const {
 
248
  return m_problems;
 
249
}
 
250
 
 
251
void ExpressionVisitor::problem( AST* node, const QString& str ) {
 
252
#ifdef DUMP_PROBLEMS
 
253
  kDebug(9007) << "Cpp::ExpressionVisitor problem:" << str;
 
254
 
 
255
  kDebug(9007) << "Cpp::ExpressionVisitor dumping the node that created the problem";
 
256
  Cpp::DumpChain d;
 
257
  
 
258
  d.dump(node, m_session);
 
259
#endif
 
260
}
 
261
 
 
262
AbstractType::Ptr ExpressionVisitor::lastType() {
 
263
  return m_lastType;
 
264
}
 
265
 
 
266
ExpressionVisitor::Instance ExpressionVisitor::lastInstance() {
 
267
  return m_lastInstance;
 
268
}
 
269
 
 
270
/** Find the member in the declaration's du-chain. **/
 
271
void ExpressionVisitor::findMember( AST* node, AbstractType::Ptr base, const Identifier& member, bool isConst, bool postProblem ) {
 
272
 
 
273
    ///have test
 
274
 
 
275
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
276
 
 
277
    LOCKDUCHAIN;
 
278
 
 
279
    base = realType(base, topContext(), &isConst);
 
280
 
 
281
    clearLast();
 
282
 
 
283
    isConst |= isConstant(base);
 
284
 
 
285
    IdentifiedType* idType = dynamic_cast<IdentifiedType*>( base.unsafeData() );
 
286
    //Make sure that it is a structure-type, because other types do not have members
 
287
    StructureType* structureType = dynamic_cast<StructureType*>( base.unsafeData() );
 
288
 
 
289
    if( !structureType || !idType ) {
 
290
      problem( node, QString("findMember called on non-identified or non-structure type \"%1\"").arg(base ? base->toString() : "<type disappeared>") );
 
291
      return;
 
292
    }
 
293
 
 
294
    Declaration* declaration = idType->declaration(topContext());
 
295
    MUST_HAVE(declaration);
 
296
    MUST_HAVE(declaration->context());
 
297
 
 
298
    DUContext* internalContext = declaration->logicalInternalContext(topContext());
 
299
 
 
300
    MUST_HAVE( internalContext );
 
301
 
 
302
  m_lastDeclarations = convert(findLocalDeclarations( internalContext, member, topContext() ));
 
303
 
 
304
 
 
305
    if( m_lastDeclarations.isEmpty() ) {
 
306
      if( postProblem ) {
 
307
        problem( node, QString("could not find member \"%1\" in \"%2\", scope of context: %3").arg(member.toString()).arg(declaration->toString()).arg(declaration->context()->scopeIdentifier().toString()) );
 
308
      }
 
309
      return;
 
310
    }
 
311
 
 
312
    //Give a default return without const-checking.
 
313
    m_lastType = m_lastDeclarations.front()->abstractType();
 
314
    m_lastInstance = Instance( m_lastDeclarations.front() );
 
315
 
 
316
    //If it is a function, match the const qualifier
 
317
    for( QList<DeclarationPointer>::const_iterator it = m_lastDeclarations.constBegin(); it != m_lastDeclarations.constEnd(); ++it ) {
 
318
      AbstractType::Ptr t = (*it)->abstractType();
 
319
      if( t ) {
 
320
        if( (t->modifiers() & AbstractType::ConstModifier) == isConst ) {
 
321
          m_lastType = t;
 
322
          m_lastInstance.declaration = *it;
 
323
          break;
 
324
        }
 
325
      }
 
326
    }
 
327
}
 
328
 
 
329
/**
 
330
 *  Here the . and -> operators are implemented.
 
331
 *  Before visitClassMemberAccess is called, m_lastType and m_lastInstance must be set
 
332
 *  to the base-types
 
333
 *
 
334
 * have test
 
335
 *
 
336
 **/
 
337
  void ExpressionVisitor::visitClassMemberAccess(ClassMemberAccessAST* node)
 
338
{
 
339
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
340
 
 
341
    if( !m_lastInstance || !m_lastType ) {
 
342
      problem(node, "VisitClassMemberAccess called without a base-declaration. '.' and '->' operators are only allowed on type-instances.");
 
343
      return;
 
344
    }
 
345
 
 
346
    bool isConst = false;
 
347
 
 
348
    switch( tokenFromIndex(node->op).kind ) {
 
349
      case Token_arrow:
 
350
      {
 
351
        ///have test
 
352
        LOCKDUCHAIN;
 
353
        //When the type is a reference, dereference it so we get to the pointer-type
 
354
 
 
355
        PointerType::Ptr pnt = realType(m_lastType, topContext()).cast<PointerType>();
 
356
        if( pnt ) {
 
357
/*          kDebug(9007) << "got type:" << pnt->toString();
 
358
          kDebug(9007) << "base-type:" << pnt->baseType()->toString();*/
 
359
 
 
360
          isConst = isConstant(pnt.cast<AbstractType>());
 
361
          //It is a pointer, reduce the pointer-depth by one
 
362
          m_lastType = pnt->baseType();
 
363
          m_lastInstance = Instance( getDeclaration(m_lastType) );
 
364
        } else {
 
365
          findMember( node, m_lastType, Identifier("operator->") );
 
366
          if( !m_lastType ) {
 
367
            problem( node, "no overloaded operator-> found" );
 
368
            return;
 
369
          }
 
370
 
 
371
          getReturnValue(node);
 
372
          if( !m_lastType ) {
 
373
            problem( node, "could not get return-type of operator->" );
 
374
            return;
 
375
          }
 
376
 
 
377
          if( !getPointerTarget(node, &isConst) ) {
 
378
            clearLast();
 
379
            return;
 
380
          }
 
381
 
 
382
          if( !m_lastDeclarations.isEmpty() ) {
 
383
            DeclarationPointer decl(m_lastDeclarations.first());
 
384
            lock.unlock();
 
385
            newUse( node, node->op, node->op+1, decl );
 
386
          }
 
387
        }
 
388
      }
 
389
      case '.':
 
390
        ///have test
 
391
      break;
 
392
      default:
 
393
        problem( node, QString("unknown class-member access operation: %1").arg( tokenFromIndex(node->op).kind ) );
 
394
        return;
 
395
      break;
 
396
    }
 
397
 
 
398
  m_memberAccess = true;
 
399
  visitName(node->name);
 
400
  m_memberAccess = false;
 
401
  }
 
402
 
 
403
 
 
404
  AbstractType::Ptr ExpressionVisitor::realLastType(bool* constant) const {
 
405
    LOCKDUCHAIN;
 
406
    return AbstractType::Ptr(realType( m_lastType, topContext(), constant ));
 
407
  }
 
408
 
 
409
  bool ExpressionVisitor::getPointerTarget( AST* node, bool* constant )  {
 
410
    if( !m_lastType ) return false;
 
411
 
 
412
    AbstractType::Ptr base = realLastType();
 
413
 
 
414
    clearLast();
 
415
 
 
416
    PointerType* pnt = dynamic_cast<PointerType*>( base.unsafeData() );
 
417
    if( pnt ) {
 
418
      if( constant )
 
419
        (*constant) |= (pnt->modifiers() & AbstractType::ConstModifier);
 
420
      m_lastType = pnt->baseType();
 
421
      m_lastInstance = Instance(getDeclaration(m_lastType));
 
422
      return true;
 
423
    } else {
 
424
      LOCKDUCHAIN;
 
425
      problem(node, QString("Cannot dereference base-type \"%1\"").arg(base->toString()) );
 
426
      return false;
 
427
    }
 
428
  }
 
429
 
 
430
  Declaration* ExpressionVisitor::getDeclaration( const AbstractType::Ptr& base ) {
 
431
    if( !base ) return 0;
 
432
 
 
433
    const IdentifiedType* idType = dynamic_cast<const IdentifiedType*>(base.unsafeData());
 
434
    if( idType ) {
 
435
      LOCKDUCHAIN;
 
436
      return idType->declaration(topContext());
 
437
    } else {
 
438
      return 0;
 
439
    }
 
440
  }
 
441
 
 
442
  /**
 
443
   * Here declarations are located
 
444
   *
 
445
   * have test
 
446
   **/
 
447
 
 
448
  void ExpressionVisitor::visitName(NameAST* node)
 
449
  {
 
450
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
451
    
 
452
    DUContext* searchInContext = m_currentContext;
 
453
    
 
454
    SimpleCursor position = m_session->positionAt( m_session->token_stream->position(node->start_token) );
 
455
    if( m_currentContext->url() != m_session->m_url ) //.equals( m_session->m_url, KUrl::CompareWithoutTrailingSlash ) )
 
456
      position = position.invalid();
 
457
 
 
458
    if( m_memberAccess ) {
 
459
      LOCKDUCHAIN;
 
460
      bool isConst = false; //@todo get this from upside
 
461
 
 
462
      m_lastType = realType(m_lastType, topContext(), &isConst);
 
463
 
 
464
      isConst |= isConstant(m_lastType);
 
465
 
 
466
      IdentifiedType* idType = dynamic_cast<IdentifiedType*>( m_lastType.unsafeData() );
 
467
      //Make sure that it is a structure-type, because other types do not have members
 
468
      StructureType* structureType = dynamic_cast<StructureType*>( m_lastType.unsafeData() );
 
469
 
 
470
      if( !structureType || !idType ) {
 
471
        problem( node, QString("member searched in non-identified or non-structure type \"%1\"").arg(m_lastType ? m_lastType->toString() : "<type disappeared>") );
 
472
        clearLast();
 
473
        return;
 
474
      }
 
475
 
 
476
      Declaration* declaration = idType->declaration(topContext());
 
477
      MUST_HAVE(declaration);
 
478
      MUST_HAVE(declaration->context());
 
479
 
 
480
      searchInContext = declaration->logicalInternalContext(topContext());
 
481
 
 
482
      MUST_HAVE( searchInContext );
 
483
    }
 
484
 
 
485
    clearLast();
 
486
 
 
487
    NameASTVisitor nameV( m_session, this, searchInContext, topContext(), m_currentContext, position.isValid() ? position : searchInContext->range().end, m_memberAccess ? DUContext::DontSearchInParent : DUContext::NoSearchFlags );
 
488
    nameV.run(node, m_skipLastNamePart);
 
489
 
 
490
    if( nameV.identifier().isEmpty() ) {
 
491
      problem( node, "name is empty" );
 
492
      return;
 
493
    }
 
494
 
 
495
    QualifiedIdentifier identifier = nameV.identifier();
 
496
 
 
497
    ///@todo It would be better if the parser would treat true and false exactly
 
498
    ///like constant-integer expressions, storing them in a primary expression.
 
499
    static QualifiedIdentifier trueIdentifier("true");
 
500
    static QualifiedIdentifier falseIdentifier("false");
 
501
 
 
502
    if( identifier == trueIdentifier || identifier == falseIdentifier ) {
 
503
      ///We have a boolean constant, we need to catch that here
 
504
      LOCKDUCHAIN;
 
505
      m_lastType = AbstractType::Ptr(new ConstantIntegralType(IntegralType::TypeBoolean));
 
506
      m_lastInstance = Instance( true );
 
507
      static_cast<ConstantIntegralType*>(m_lastType.unsafeData())->setValue<qint64>( identifier == trueIdentifier );
 
508
    } else {
 
509
      LOCKDUCHAIN;
 
510
 
 
511
      m_lastDeclarations = nameV.declarations();
 
512
 
 
513
      if( m_lastDeclarations.isEmpty() || !m_lastDeclarations.first().data() ) {
 
514
        
 
515
        if(Cpp::isTemplateDependent(m_currentContext) ) {
 
516
          if(m_memberAccess || (node->qualified_names && nameV.foundSomething() && Cpp::isTemplateDependent(nameV.foundSomething().data()))) {
 
517
          //Do nothing. Within a not instantiated template, we cannot be that sure
 
518
          m_lastType.clear();
 
519
          return;
 
520
          }
 
521
        }
 
522
        
 
523
        MissingDeclarationType::Ptr missing(new MissingDeclarationType);
 
524
        
 
525
        missing->setIdentifier(IndexedTypeIdentifier(nameV.identifier()));
 
526
        if(m_memberAccess)
 
527
          missing->containerContext = searchInContext;
 
528
        
 
529
        missing->searchStartContext = m_currentContext;
 
530
        
 
531
        if(m_reportRealProblems && m_problems.size() < maxExpressionVisitorProblems) {
 
532
          KSharedPtr<KDevelop::Problem> problem(new Cpp::MissingDeclarationProblem(missing));
 
533
          problem->setSource(KDevelop::ProblemData::SemanticAnalysis);
 
534
          CppEditorIntegrator editor(session());
 
535
          
 
536
          problem->setFinalLocation(DocumentRange(m_currentContext->url().str(), editor.findRange(node).textRange()));
 
537
          if(!problem->range().isEmpty() && !editor.findRangeForContext(node->start_token, node->end_token).isEmpty())
 
538
            m_problems << problem;
 
539
        }
 
540
        m_lastType = missing.cast<KDevelop::AbstractType>();
 
541
 
 
542
        problem( node, QString("could not find declaration of %1").arg( nameV.identifier().toString() ) );
 
543
      } else {
 
544
        m_lastType = m_lastDeclarations.first()->abstractType();
 
545
        //kDebug(9007) << "found declaration: " << m_lastDeclarations.first()->toString();
 
546
 
 
547
        ///If the found declaration declares a type, this is a type-expression and m_lastInstance should be zero.
 
548
        ///The declaration declares a type if its abstractType's declaration is that declaration. Else it is an insantiation, and m_lastType should be filled.
 
549
 
 
550
        if( m_lastDeclarations.first()->kind() == Declaration::Instance )
 
551
          m_lastInstance = Instance( m_lastDeclarations.first() );
 
552
        else
 
553
          m_lastInstance = Instance(false);
 
554
 
 
555
        //A CppTemplateParameterType represents an unresolved template-parameter, so create a DelayedType instead.
 
556
        if( dynamic_cast<CppTemplateParameterType*>(m_lastType.unsafeData()) )
 
557
          createDelayedType(node, false);
 
558
      }
 
559
    }
 
560
    if( m_lastType )
 
561
      expressionType( node, m_lastType, m_lastInstance );
 
562
  }
 
563
 
 
564
 
 
565
  /** Primary expressions just forward to their encapsulated expression
 
566
   *
 
567
   * have test
 
568
   *
 
569
  */
 
570
  void ExpressionVisitor::visitPrimaryExpression(PrimaryExpressionAST* node)
 
571
  {
 
572
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
573
 
 
574
    clearLast();
 
575
    
 
576
    if( node->literal ) {
 
577
      visit( node->literal );
 
578
      return; //We had a string-literal
 
579
    }
 
580
 
 
581
    if( !node->sub_expression && !node->expression_statement && !node->name )
 
582
    {
 
583
      IndexedString startNumber = IndexedString::fromIndex(m_session->contents()[tokenFromIndex(node->start_token).position]); //Extracts the first digit
 
584
 
 
585
      if( isNumber(startNumber) )
 
586
      {
 
587
        QString num;
 
588
        for( size_t a = node->start_token; a < node->end_token; a++ )
 
589
          num += tokenFromIndex(a).symbolString();
 
590
 
 
591
        LOCKDUCHAIN;
 
592
        if( num.indexOf('.') != -1 || num.endsWith('f') || num.endsWith('d') ) {
 
593
          double val = 0;
 
594
          bool ok = false;
 
595
          while( !num.isEmpty() && !ok ) {
 
596
            val = num.toDouble(&ok);
 
597
            num.truncate(num.length()-1);
 
598
          }
 
599
 
 
600
 
 
601
          if( num.endsWith('f') ) {
 
602
            m_lastType = AbstractType::Ptr(new ConstantIntegralType(IntegralType::TypeFloat));
 
603
            static_cast<ConstantIntegralType*>(m_lastType.unsafeData())->setValue<float>((float)val);
 
604
          } else {
 
605
            m_lastType = AbstractType::Ptr(new ConstantIntegralType(IntegralType::TypeDouble));
 
606
            static_cast<ConstantIntegralType*>(m_lastType.unsafeData())->setValue<double>(val);
 
607
          }
 
608
        } else {
 
609
          qint64 val = 0;
 
610
          uint mod = AbstractType::NoModifiers;
 
611
 
 
612
          if( num.endsWith("u") || ( num.length() > 1 && num[1] == 'x' ) )
 
613
            mod = AbstractType::UnsignedModifier;
 
614
 
 
615
          bool ok = false;
 
616
          while( !num.isEmpty() && !ok ) {
 
617
            val = num.toLongLong(&ok, 0);
 
618
            num.truncate(num.length()-1);
 
619
          }
 
620
 
 
621
          m_lastType = AbstractType::Ptr(new ConstantIntegralType(IntegralType::TypeInt));
 
622
          m_lastType->setModifiers(mod);
 
623
 
 
624
          if( mod & AbstractType::UnsignedModifier )
 
625
            ConstantIntegralType::Ptr::staticCast(m_lastType)->setValue<quint64>(val);
 
626
          else
 
627
            ConstantIntegralType::Ptr::staticCast(m_lastType)->setValue<qint64>(val);
 
628
        }
 
629
        m_lastInstance = Instance(true);
 
630
 
 
631
        return;
 
632
      }
 
633
    }
 
634
 
 
635
    visit( node->sub_expression );
 
636
    visit( node->expression_statement );
 
637
    visit( node->name );
 
638
 
 
639
    const Token& token(tokenFromIndex(node->token));
 
640
    
 
641
    static const IndexedString True("true");
 
642
    static const IndexedString False("false");
 
643
 
 
644
    if(token.kind == Token_char_literal) {
 
645
      // char literal e.g. 'x'
 
646
      LOCKDUCHAIN;
 
647
      m_lastType = AbstractType::Ptr(new ConstantIntegralType(IntegralType::TypeChar));
 
648
      m_lastInstance = Instance( true );
 
649
      if ( token.size == 3 ) {
 
650
        static_cast<ConstantIntegralType*>(m_lastType.unsafeData())->setValue<char>( token.symbolByteArray().at(1) );
 
651
      } else if (token.size == 4) {
 
652
        if (token.symbolByteArray() == "'\\t'") {
 
653
          static_cast<ConstantIntegralType*>(m_lastType.unsafeData())->setValue<char>('\t');
 
654
        } else if (token.symbolByteArray() == "'\\n'") {
 
655
          static_cast<ConstantIntegralType*>(m_lastType.unsafeData())->setValue<char>('\n');
 
656
        } else if (token.symbolByteArray() == "'\\r'") {
 
657
          static_cast<ConstantIntegralType*>(m_lastType.unsafeData())->setValue<char>('\r');
 
658
        }
 
659
      }
 
660
 
 
661
    } else if(token.symbol() == True || token.symbol() == False) {
 
662
      ///We have a boolean constant, we need to catch that here
 
663
      LOCKDUCHAIN;
 
664
      m_lastType = AbstractType::Ptr(new ConstantIntegralType(IntegralType::TypeBoolean));
 
665
      m_lastInstance = Instance( true );
 
666
      static_cast<ConstantIntegralType*>(m_lastType.unsafeData())->setValue<qint64>( token.symbol() == True );
 
667
    }
 
668
    
 
669
    //Respect "this" token
 
670
    if( token.kind == Token_this ) {
 
671
      LOCKDUCHAIN;
 
672
 
 
673
      AbstractType::Ptr thisType;
 
674
 
 
675
      DUContext* context = m_currentContext; //Here we find the context of the function-declaration/definition we're currently in
 
676
      while( context->parentContext() && context->type() == DUContext::Other && context->parentContext()->type() == DUContext::Other )
 
677
      { //Move context to the top context of type "Other". This is needed because every compound-statement creates a new sub-context.
 
678
        context = context->parentContext();
 
679
      }
 
680
 
 
681
      ///Step 1: Find the function-declaration for the function we are in
 
682
      Declaration* functionDeclaration = 0;
 
683
 
 
684
      if( context->owner() && dynamic_cast<FunctionDefinition*>(context->owner()) )
 
685
        functionDeclaration = static_cast<FunctionDefinition*>(context->owner())->declaration(topContext());
 
686
 
 
687
      if( !functionDeclaration && context->owner() )
 
688
        functionDeclaration = context->owner();
 
689
 
 
690
      if( !functionDeclaration )
 
691
      {
 
692
        problem(node, "\"this\" used, but no function-declaration could be found");
 
693
        return;
 
694
      }
 
695
 
 
696
      ///Step 2: Find the type of "this" from the function-declaration
 
697
      DUContext* classContext = functionDeclaration->context();
 
698
 
 
699
      //Take the type from the classContext
 
700
      if( classContext && classContext->type() == DUContext::Class && classContext->owner() && classContext->owner() )
 
701
        thisType = classContext->owner()->abstractType();
 
702
 
 
703
      if( !thisType ) {
 
704
        problem(node, "\"this\" used in invalid classContext");
 
705
        return;
 
706
      }
 
707
 
 
708
      ///Step 3: Create a pointer-type for the "this" type and return it
 
709
      KDevelop::FunctionType::Ptr cppFunction = functionDeclaration->abstractType().cast<KDevelop::FunctionType>();
 
710
 
 
711
      if( cppFunction ) {
 
712
        PointerType::Ptr thisPointer( new PointerType() );
 
713
        thisPointer->setModifiers(cppFunction->modifiers() & (AbstractType::ConstModifier | AbstractType::VolatileModifier));
 
714
        thisPointer->setBaseType( thisType );
 
715
 
 
716
        m_lastType = thisPointer.cast<AbstractType>();
 
717
        m_lastInstance = Instance(true);
 
718
      }else{
 
719
        if( context->owner() && context->owner() && context->owner()->abstractType() )
 
720
          problem(node, QString("\"this\" used in non-function context of type %1(%2)").arg( "unknown" ) .arg(m_currentContext->owner()->abstractType()->toString()));
 
721
        else
 
722
          problem(node, "\"this\" used in non-function context with invalid type");
 
723
      }
 
724
    }
 
725
 
 
726
    if( m_lastType )
 
727
      expressionType( node, m_lastType, m_lastInstance );
 
728
  }
 
729
 
 
730
  /** Translation-units just forward to their encapsulated expression */
 
731
  void ExpressionVisitor::visitTranslationUnit(TranslationUnitAST* node)
 
732
  {
 
733
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
734
 
 
735
    visitNodes(this, node->declarations);
 
736
 
 
737
    if( m_lastType )
 
738
      expressionType( node, m_lastType, m_lastInstance );
 
739
  }
 
740
 
 
741
  /** Sub-expressions of a post-fix expression, will be applied in order to m_lastType
 
742
   *
 
743
   * have test  */
 
744
 
 
745
  void  ExpressionVisitor::visitSubExpressions( AST* node, const ListNode<ExpressionAST*>* nodes ) {
 
746
    if( !nodes )
 
747
      return;
 
748
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
749
 
 
750
    bool onlyFunctionCalls = false;
 
751
 
 
752
    if( !m_lastType ) {
 
753
       problem( node, "primary expression returned no type" );
 
754
       onlyFunctionCalls = true; //We want to visit function-calls even when the function was not resolved, so we get uses for the arguments
 
755
    }
 
756
    const ListNode<ExpressionAST*> *it = nodes->toFront(), *end = it;
 
757
 
 
758
    int num = 0;
 
759
    do
 
760
      {
 
761
        if( !onlyFunctionCalls || (it->element && it->element->kind == AST::Kind_FunctionCall) )
 
762
          visit(it->element);
 
763
 
 
764
        if( !m_lastType ) {
 
765
          problem( node, QString("while parsing post-fix-expression: sub-expression %1 returned no type").arg(num) );
 
766
          return;
 
767
        }
 
768
        it = it->next;
 
769
        num++;
 
770
      }
 
771
    while (it != end);
 
772
 
 
773
    if( m_lastType )
 
774
      expressionType( node, m_lastType, m_lastInstance );
 
775
  }
 
776
 
 
777
  /** A postfix-expression is a primary expression together with a chain of sub-expressions that are applied from left to right
 
778
   *
 
779
   * have test */
 
780
 
 
781
  void ExpressionVisitor::visitPostfixExpression(PostfixExpressionAST* node)
 
782
  {
 
783
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
784
 
 
785
    clearLast();
 
786
    if( node->type_specifier ) {
 
787
      problem( node, "unexpected type-specifier" );
 
788
      return;
 
789
    }
 
790
    if( !node->expression ) {
 
791
      problem( node, "primary expression missing" );
 
792
      return;
 
793
    }
 
794
    //First evaluate the primary expression, and then pass the result from sub-expression to sub-expression through m_lastType
 
795
    visit( node->expression );
 
796
 
 
797
    if( !node->sub_expressions )
 
798
      return;
 
799
 
 
800
    visitSubExpressions( node, node->sub_expressions );
 
801
  }
 
802
 
 
803
/** A helper-class for evaluating constant unary expressions under different types(int, float, etc.) */
 
804
template<class Type>
 
805
struct ConstantUnaryExpressionEvaluator {
 
806
 
 
807
  Type endValue;
 
808
 
 
809
  uint type;
 
810
  uint modifier;
 
811
 
 
812
  /**
 
813
   * Writes the results into endValue, type, and modifier.
 
814
   * */
 
815
  ConstantUnaryExpressionEvaluator( int tokenKind, ConstantIntegralType* left ) {
 
816
    endValue = 0;
 
817
    type = left->dataType();
 
818
    modifier = left->modifiers();
 
819
    evaluateSpecialTokens( tokenKind, left );
 
820
    switch( tokenKind ) {
 
821
      case '+':
 
822
        endValue = +left->value<Type>();
 
823
      break;
 
824
      case '-':
 
825
        endValue = -left->value<Type>();
 
826
      break;
 
827
      case Token_incr:
 
828
        endValue = left->value<Type>()+1;
 
829
      case Token_decr:
 
830
        endValue = left->value<Type>()-1;
 
831
    }
 
832
  }
 
833
 
 
834
  //This function is used to disable some operators on bool and double values
 
835
  void evaluateSpecialTokens( int tokenKind, ConstantIntegralType* left ) {
 
836
    switch( tokenKind ) {
 
837
      case '~':
 
838
        endValue = ~left->value<Type>();
 
839
      break;
 
840
      case '!':
 
841
        endValue = !left->value<Type>();
 
842
      break;
 
843
    }
 
844
  }
 
845
 
 
846
  AbstractType::Ptr createType() {
 
847
    AbstractType::Ptr ret = AbstractType::Ptr(new ConstantIntegralType(type));
 
848
    ret->setModifiers(modifier);
 
849
    static_cast<ConstantIntegralType*>(ret.unsafeData())->setValue<Type>( endValue );
 
850
    return ret;
 
851
  }
 
852
};
 
853
 
 
854
template<>
 
855
void ConstantUnaryExpressionEvaluator<double>::evaluateSpecialTokens( int tokenKind, ConstantIntegralType* left ) {
 
856
  Q_UNUSED(tokenKind);
 
857
  Q_UNUSED(left);
 
858
}
 
859
 
 
860
template<>
 
861
void ConstantUnaryExpressionEvaluator<float>::evaluateSpecialTokens( int tokenKind, ConstantIntegralType* left ) {
 
862
  Q_UNUSED(tokenKind);
 
863
  Q_UNUSED(left);
 
864
}
 
865
 
 
866
QString toString(AbstractType::Ptr t) {
 
867
  if(!t)
 
868
    return "<no type>";
 
869
  return t->toString();
 
870
}
 
871
 
 
872
void ExpressionVisitor::createDelayedType( AST* node , bool expression ) {
 
873
  DelayedType::Ptr type(new DelayedType());
 
874
  QString id;
 
875
  for( size_t s = node->start_token; s < node->end_token; ++s )
 
876
    id += m_session->token_stream->token(s).symbolString();
 
877
 
 
878
  //We have  to prevent automatic parsing and splitting by QualifiedIdentifier and Identifier
 
879
  Identifier idd;
 
880
  idd.setIdentifier(id);
 
881
  
 
882
  QualifiedIdentifier ident;
 
883
  ident.push(idd);
 
884
  
 
885
  ident.setIsExpression( expression );
 
886
  type->setIdentifier( IndexedTypeIdentifier(ident) );
 
887
  m_lastType = type.cast<AbstractType>();
 
888
}
 
889
 
 
890
  /**
 
891
   *
 
892
   * partially have test **/
 
893
  void ExpressionVisitor::visitBinaryExpression(BinaryExpressionAST* node)  {
 
894
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
895
 
 
896
    clearLast();
 
897
 
 
898
    ///First resolve left part, then right, then combine
 
899
    visit(node->left_expression);
 
900
 
 
901
    Instance leftInstance = m_lastInstance;
 
902
    AbstractType::Ptr leftType = m_lastType;
 
903
    clearLast();
 
904
 
 
905
    if( tokenFromIndex(node->op).kind == ',' ) {
 
906
      /**A ',' binary expression is used for separating the argument-expressions in a function-call.
 
907
       * Those should be collected into m_parameters
 
908
       *
 
909
       * How this should work: Every binary ',' expression yields a m_lastType of null.
 
910
       *
 
911
       * So whenever an operand(left or right side) yields a type, we can be sure it is not a binary-expression
 
912
       * so we can add the type to the parameter-list.
 
913
       * */
 
914
      if( leftType && leftInstance) {
 
915
        m_parameters << OverloadResolver::Parameter(leftType, isLValue( leftType, leftInstance ) );
 
916
        m_parameterNodes.append(node->left_expression);
 
917
 
 
918
        //LOCKDUCHAIN;
 
919
        //kDebug(9007) << "Adding parameter from left: " << (leftType.data() ? leftType->toString() : QString("<notype>"));
 
920
      } else {
 
921
        //If neither leftType nor leftInstance are true, the expression was probably another binary
 
922
        //expression that has put the types/instances into m_parameters and returns nothing.
 
923
        if( leftType || leftInstance ) {
 
924
          if( leftType )
 
925
            problem( node->left_expression, "left operand of binary ','-expression is no type-instance" );
 
926
          else
 
927
            problem( node->left_expression, "left operand of binary ','-expression could not be evaluated" );
 
928
 
 
929
          m_parameters << OverloadResolver::Parameter(AbstractType::Ptr(), false);
 
930
          m_parameterNodes.append(node->left_expression);
 
931
          //LOCKDUCHAIN;
 
932
          //kDebug(9007) << "Adding empty from left";
 
933
        }
 
934
      }
 
935
    }
 
936
    
 
937
    visit(node->right_expression);
 
938
 
 
939
    Instance rightInstance = m_lastInstance;
 
940
    AbstractType::Ptr rightType = m_lastType;
 
941
    clearLast();
 
942
 
 
943
    if( tokenFromIndex(node->op).kind == ',' ) {
 
944
 
 
945
      if( rightType && rightInstance) {
 
946
        m_parameters << OverloadResolver::Parameter(rightType, isLValue( rightType, rightInstance ) );
 
947
        m_parameterNodes.append(node->right_expression);
 
948
        //LOCKDUCHAIN;
 
949
        //kDebug(9007) << "Adding parameter from right: " << (rightType.data() ? rightType->toString() : QString("<notype>"));
 
950
      } else {
 
951
        //If neither leftType nor leftInstance are true, the expression was probably another binary
 
952
        //expression that has put the types/instances into m_parameters and returns nothing.
 
953
        if( rightType || rightInstance ) {
 
954
          if( rightType )
 
955
            problem( node->right_expression, "right operand of binary ','-expression is no type-instance" );
 
956
          else
 
957
            problem( node->right_expression, "right operand of binary ','-expression could not be evaluated" );
 
958
 
 
959
          m_parameters << OverloadResolver::Parameter(AbstractType::Ptr(), false);
 
960
          m_parameterNodes.append(node->right_expression);
 
961
          //kDebug(9007) << "Adding empty from right";
 
962
        }
 
963
      }
 
964
 
 
965
      clearLast();
 
966
      return;
 
967
    }
 
968
    
 
969
    if(MissingDeclarationType::Ptr missing = leftType.cast<Cpp::MissingDeclarationType>()) {
 
970
      if(rightType) {
 
971
        Cpp::ExpressionEvaluationResult res;
 
972
        res.type = rightType->indexed();
 
973
        res.isInstance = rightInstance;
 
974
        missing->assigned = res;
 
975
      }
 
976
      clearLast();
 
977
      return;
 
978
    }
 
979
 
 
980
    if(MissingDeclarationType::Ptr missing = rightType.cast<Cpp::MissingDeclarationType>()) {
 
981
      if(leftType) {
 
982
        Cpp::ExpressionEvaluationResult res;
 
983
        res.type = leftType->indexed();
 
984
        res.isInstance = leftInstance;
 
985
        missing->convertedTo = res;
 
986
      }
 
987
      clearLast();
 
988
      return;
 
989
    }
 
990
 
 
991
    if( !leftInstance && !leftType ) {
 
992
      problem( node, "left operand of binary expression could not be evaluated" );
 
993
      return;
 
994
    }
 
995
 
 
996
    if( !rightInstance && !rightType ) {
 
997
      problem( node, "right operand of binary expression could not be evaluated" );
 
998
      m_lastInstance = leftInstance;
 
999
      m_lastType = leftType;
 
1000
      return;
 
1001
    }
 
1002
    
 
1003
    if( dynamic_cast<DelayedType*>(rightType.unsafeData()) || dynamic_cast<DelayedType*>(leftType.unsafeData()) ) {
 
1004
      m_lastInstance = Instance(true);
 
1005
      createDelayedType(node);
 
1006
      return;
 
1007
    }
 
1008
 
 
1009
    int tokenKind = tokenFromIndex(node->op).kind;
 
1010
    
 
1011
    if(rightType && leftType && rightInstance && leftInstance) {
 
1012
      LOCKDUCHAIN;
 
1013
      //Test if there is a builtin operator that can be used. If it is, this will also evaluate the values of constant expressions.
 
1014
      m_lastType = binaryOperatorReturnType(leftType, rightType, tokenKind);
 
1015
      m_lastInstance = Instance(true);
 
1016
    }
 
1017
    if(!m_lastType) {
 
1018
      QString op = operatorNameFromTokenKind(tokenFromIndex(node->op).kind);
 
1019
 
 
1020
      bool success = false;
 
1021
      if( !op.isEmpty() )
 
1022
      {
 
1023
        LOCKDUCHAIN;
 
1024
        KDevelop::DUContextPointer ptr(m_currentContext);
 
1025
        OverloadResolutionHelper helper(ptr, TopDUContextPointer(topContext()) );
 
1026
        helper.setOperator( OverloadResolver::Parameter(leftType, isLValue( leftType, leftInstance ) ), op );
 
1027
        helper.setKnownParameters( OverloadResolver::ParameterList( OverloadResolver::Parameter(rightType, isLValue( rightType, rightInstance ) ) ) );
 
1028
        QList<OverloadResolutionFunction> functions = helper.resolve(false);
 
1029
 
 
1030
        if( !functions.isEmpty() )
 
1031
        {
 
1032
          KDevelop::FunctionType::Ptr function = functions.first().function.declaration()->type<KDevelop::FunctionType>();
 
1033
          if( functions.first().function.isViable() && function ) {
 
1034
            success = true;
 
1035
            m_lastType = function->returnType();
 
1036
            m_lastInstance = Instance(functions.first().function.declaration());
 
1037
 
 
1038
            lock.unlock();
 
1039
            newUse( node, node->op, node->op+1, functions.first().function.declaration() );
 
1040
          }else{
 
1041
            //Do not complain here, because we do not check for builtin operators
 
1042
            //problem(node, "No fitting operator. found" );
 
1043
            //problem(node, QString("Found no viable operator-function"));
 
1044
          }
 
1045
        }else{
 
1046
          //Do not complain here, because we do not check for builtin operators
 
1047
          //problem(node, "No fitting operator. found" );
 
1048
        }
 
1049
        //Find an overloaded binary operator
 
1050
      } else {
 
1051
        problem(node, "not implemented binary expression" );
 
1052
      }
 
1053
 
 
1054
      if( !success ) {
 
1055
        m_lastType = leftType;
 
1056
        m_lastInstance = leftInstance;
 
1057
      }
 
1058
    }
 
1059
 
 
1060
 
 
1061
    if( m_lastType )
 
1062
      expressionType( node, m_lastType, m_lastInstance );
 
1063
  }
 
1064
 
 
1065
  /**
 
1066
   *
 
1067
   * Not ready yet */
 
1068
 
 
1069
  void ExpressionVisitor::visitTypeSpecifier(TypeSpecifierAST* ast)
 
1070
  {
 
1071
    PushPositiveContext pushContext( m_currentContext, ast->ducontext );
 
1072
 
 
1073
    clearLast();
 
1074
 
 
1075
    TypeASTVisitor comp(m_session, this, m_currentContext, topContext(), m_currentContext);
 
1076
    comp.run(ast);
 
1077
 
 
1078
    LOCKDUCHAIN;
 
1079
 
 
1080
    QList<DeclarationPointer> decls = comp.declarations();
 
1081
 
 
1082
    m_lastType = comp.type();
 
1083
    
 
1084
    if( !decls.isEmpty() )
 
1085
    {
 
1086
      m_lastDeclarations = decls;
 
1087
//       m_lastType = decls.first()->abstractType(); If we do this, we may lose modifiers and such
 
1088
 
 
1089
      if( decls.first()->kind() == Declaration::Type )
 
1090
        m_lastInstance = Instance(false);
 
1091
      else
 
1092
        ///Allow non-types, because we sometimes don't know whether something is a type or not, and it may get parsed as a type.
 
1093
        m_lastInstance = Instance(decls.first());
 
1094
 
 
1095
      if( dynamic_cast<CppTemplateParameterType*>(m_lastType.unsafeData()) )
 
1096
        createDelayedType(ast, false);
 
1097
    } else {
 
1098
      problem(ast, "Could not resolve type");
 
1099
#ifdef DEBUG_RESOLUTION_PROBLEMS
 
1100
      //Run the ast-visitor in debug mode
 
1101
 
 
1102
      ++m_ignore_uses;
 
1103
      TypeASTVisitor comp2(m_session, this, m_currentContext, topContext(), true);
 
1104
      comp2.run(ast);
 
1105
      --m_ignore_uses;
 
1106
#endif
 
1107
    }
 
1108
  }
 
1109
 
 
1110
  void ExpressionVisitor::visitSimpleTypeSpecifier(SimpleTypeSpecifierAST* node)
 
1111
  {
 
1112
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
1113
    clearLast();
 
1114
 
 
1115
    TypeASTVisitor tvisitor(m_session, this, m_currentContext, topContext(), m_currentContext);
 
1116
    tvisitor.run(node);
 
1117
    m_lastType = tvisitor.type();
 
1118
    m_lastDeclarations = tvisitor.declarations();
 
1119
    m_lastInstance = Instance(false);
 
1120
  }
 
1121
 
 
1122
  void ExpressionVisitor::visitInitDeclarator(InitDeclaratorAST* node)
 
1123
  {
 
1124
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
1125
    
 
1126
    if(node->declarator)
 
1127
    {
 
1128
      CppClassType::Ptr constructedType = computeConstructedType();
 
1129
      
 
1130
      //Build constructor uses (similar to visitFunctionCall)
 
1131
      
 
1132
      AbstractType::Ptr oldLastType = m_lastType;
 
1133
      Instance oldInstance = m_lastInstance;
 
1134
      QList< DeclarationPointer > declarations = m_lastDeclarations;
 
1135
      
 
1136
      clearLast();
 
1137
 
 
1138
      bool fail = true;
 
1139
      
 
1140
      size_t token = node->start_token;
 
1141
      
 
1142
      if(node->initializer)
 
1143
      {
 
1144
        if(node->initializer->expression && !node->initializer->initializer_clause)
 
1145
        {
 
1146
          token = node->initializer->start_token;
 
1147
          fail = !buildParametersFromExpression(node->initializer->expression);
 
1148
        } else if(!node->initializer->expression && node->initializer->initializer_clause && constructedType)
 
1149
        { // report operator= use in i.e.: foo = bar;
 
1150
          token = node->initializer->start_token;
 
1151
          fail = !buildParametersFromExpression(node->initializer->initializer_clause->expression);
 
1152
          LOCKDUCHAIN;
 
1153
          declarations.clear();
 
1154
          if ( ClassDeclaration* cdec = dynamic_cast<ClassDeclaration*>(constructedType->declaration(m_source)) ) {
 
1155
            ///TODO: global operator= functions, for now only class members are handled
 
1156
            foreach(Declaration* dec, cdec->internalContext()->findDeclarations(Identifier("operator="))) {
 
1157
              declarations << DeclarationPointer(dec);
 
1158
            }
 
1159
          }
 
1160
        }
 
1161
      }
 
1162
      else if(node->declarator->parameter_is_initializer && node->declarator->parameter_declaration_clause)
 
1163
      {
 
1164
        token = node->declarator->parameter_declaration_clause->start_token-1;
 
1165
        fail = !buildParametersFromDeclaration(node->declarator->parameter_declaration_clause);
 
1166
      }
 
1167
      
 
1168
      if(fail || !constructedType) {
 
1169
        DefaultVisitor::visitInitDeclarator(node);
 
1170
        return;
 
1171
      }
 
1172
      
 
1173
      DeclarationPointer chosenFunction;
 
1174
      {
 
1175
        LOCKDUCHAIN;
 
1176
        
 
1177
        KDevelop::DUContextPointer ptr(m_currentContext);
 
1178
        OverloadResolver resolver( ptr, KDevelop::TopDUContextPointer(topContext()), oldInstance );
 
1179
 
 
1180
        if( !fail )
 
1181
          chosenFunction = resolver.resolveList(m_parameters, convert(declarations));
 
1182
        else if(!declarations.isEmpty() && !m_strict)
 
1183
          chosenFunction = declarations.first();
 
1184
      }
 
1185
      
 
1186
      if(chosenFunction)
 
1187
        newUse( node , token, token+1, chosenFunction );
 
1188
    }else{
 
1189
      DefaultVisitor::visitInitDeclarator(node);
 
1190
    }
 
1191
  }
 
1192
 
 
1193
  //Used to parse pointer-depth and cv-qualifies of types in new-expessions and casts
 
1194
  void ExpressionVisitor::visitDeclarator(DeclaratorAST* node)  {
 
1195
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
1196
#if 0
 
1197
    if( !m_lastType ) {
 
1198
      problem(node, "Declarator used without type");
 
1199
      return;
 
1200
    }
 
1201
 
 
1202
    if( m_lastInstance ) {
 
1203
      problem(node, "Declarator used on an instance instead of a type");
 
1204
      return;
 
1205
    }
 
1206
    #endif
 
1207
 
 
1208
    AbstractType::Ptr oldLastType = m_lastType;
 
1209
    Instance oldLastInstance = m_lastInstance;
 
1210
 
 
1211
    visit(node->sub_declarator);
 
1212
//     visit(node->id);
 
1213
    visit(node->bit_expression);
 
1214
    visitNodes(this, node->array_dimensions);
 
1215
 
 
1216
    visit(node->parameter_declaration_clause);
 
1217
    visit(node->exception_spec);
 
1218
    
 
1219
    LOCKDUCHAIN;
 
1220
    if( node->array_dimensions && oldLastType ) {
 
1221
      ArrayType::Ptr p( new ArrayType() );
 
1222
      p->setElementType( oldLastType );
 
1223
 
 
1224
      m_lastType = p.cast<AbstractType>();
 
1225
      m_lastInstance = Instance(false);
 
1226
    }else{
 
1227
      m_lastType = oldLastType;
 
1228
      m_lastInstance = oldLastInstance;
 
1229
    }
 
1230
 
 
1231
    visitNodes(this, node->ptr_ops);
 
1232
  }
 
1233
 
 
1234
  void ExpressionVisitor::visitNewDeclarator(NewDeclaratorAST* node)  {
 
1235
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
1236
 
 
1237
    if( !m_lastType ) {
 
1238
      problem(node, "Declarator used without type");
 
1239
      return;
 
1240
    }
 
1241
 
 
1242
    if( m_lastInstance ) {
 
1243
      problem(node, "Declarator used on an instance instead of a type");
 
1244
      return;
 
1245
    }
 
1246
 
 
1247
    AbstractType::Ptr lastType = m_lastType;
 
1248
    Instance instance = m_lastInstance;
 
1249
 
 
1250
    DefaultVisitor::visitNewDeclarator(node);
 
1251
 
 
1252
    m_lastType = lastType;
 
1253
    m_lastInstance = instance;
 
1254
 
 
1255
    LOCKDUCHAIN;
 
1256
    visit(node->ptr_op);
 
1257
  }
 
1258
 
 
1259
  void ExpressionVisitor::visitCppCastExpression(CppCastExpressionAST* node)  {
 
1260
 
 
1261
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
1262
 
 
1263
    //Visit the expression just so it is evaluated and expressionType(..) eventually called, the result will not be used here
 
1264
    clearLast();
 
1265
    visit( node->expression );
 
1266
    clearLast();
 
1267
 
 
1268
    if( node->type_id )
 
1269
      visit(node->type_id);
 
1270
 
 
1271
    if( !m_lastType ) {
 
1272
      problem(node, "Could not resolve type");
 
1273
      return;
 
1274
    }
 
1275
 
 
1276
    m_lastInstance = Instance(true);
 
1277
 
 
1278
    if( m_lastType )
 
1279
      expressionType( node, m_lastType, m_lastInstance );
 
1280
 
 
1281
    visitSubExpressions( node, node->sub_expressions );
 
1282
  }
 
1283
  //Used to parse pointer-depth and cv-qualifies of types in new-expessions and casts
 
1284
  void ExpressionVisitor::visitPtrOperator(PtrOperatorAST* node) {
 
1285
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
1286
 
 
1287
    if( !m_lastType ) {
 
1288
      problem(node, "Pointer-operator used without type");
 
1289
      return;
 
1290
    }
 
1291
 
 
1292
    if( m_lastInstance ) {
 
1293
      problem(node, "Pointer-operator used on an instance instead of a type");
 
1294
      return;
 
1295
    }
 
1296
 
 
1297
    LOCKDUCHAIN;
 
1298
    
 
1299
    static IndexedString ref("&");
 
1300
    static IndexedString ptr("*");
 
1301
    
 
1302
    IndexedString op = m_session->token_stream->token(node->op).symbol();
 
1303
    
 
1304
    
 
1305
    if(op == ptr) {
 
1306
    
 
1307
      PointerType::Ptr p( new PointerType() );
 
1308
      p->setBaseType( m_lastType );
 
1309
      p->setModifiers(TypeBuilder::parseConstVolatile(m_session, node->cv));
 
1310
  
 
1311
      m_lastType = p.cast<AbstractType>();
 
1312
    }else{
 
1313
      ReferenceType::Ptr p( new ReferenceType() );
 
1314
      p->setBaseType( m_lastType );
 
1315
      p->setModifiers(TypeBuilder::parseConstVolatile(m_session, node->cv));
 
1316
  
 
1317
      m_lastType = p.cast<AbstractType>();
 
1318
    }
 
1319
    m_lastInstance = Instance(false);
 
1320
  }
 
1321
 
 
1322
  /**
 
1323
   *
 
1324
   * Has test */
 
1325
  void ExpressionVisitor::visitCastExpression(CastExpressionAST* node)  {
 
1326
 
 
1327
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
1328
 
 
1329
    //Visit the expression just so it is evaluated and expressionType(..) eventually called, the result will not be used here
 
1330
    clearLast();
 
1331
 
 
1332
    visit( node->expression );
 
1333
 
 
1334
    clearLast();
 
1335
 
 
1336
    //Visit declarator and type-specifier, which should build the type
 
1337
    if( node->type_id ) {
 
1338
      visit(node->type_id->type_specifier);
 
1339
      visit(node->type_id->declarator);
 
1340
    }
 
1341
    if( !m_lastType ) {
 
1342
      problem(node, "Could not resolve type");
 
1343
      return;
 
1344
    }
 
1345
 
 
1346
    m_lastInstance = Instance(true);
 
1347
 
 
1348
    if( m_lastType )
 
1349
      expressionType( node, m_lastType, m_lastInstance );
 
1350
  }
 
1351
 
 
1352
  void ExpressionVisitor::visitNewExpression(NewExpressionAST* node)  {
 
1353
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
1354
    clearLast();
 
1355
    visit( node->expression );
 
1356
    clearLast();
 
1357
    
 
1358
    CppClassType::Ptr constructedType;
 
1359
    
 
1360
 
 
1361
    //Visit declarator and type-specifier, which should build the type
 
1362
    if( node->type_id ) {
 
1363
      visit(node->type_id->type_specifier);
 
1364
      constructedType = computeConstructedType();
 
1365
      visit(node->type_id->declarator);
 
1366
    } else if( node->new_type_id ) {
 
1367
      visit(node->new_type_id->type_specifier);
 
1368
      constructedType = computeConstructedType();
 
1369
      visit(node->new_type_id->new_declarator);
 
1370
    }
 
1371
    
 
1372
    if( m_lastType )
 
1373
    {
 
1374
      LOCKDUCHAIN;
 
1375
      ///@todo cv-qualifiers
 
1376
      PointerType::Ptr p( new PointerType() );
 
1377
      p->setBaseType( m_lastType );
 
1378
 
 
1379
      m_lastType = p.cast<AbstractType>();
 
1380
 
 
1381
      m_lastInstance = Instance(true);
 
1382
 
 
1383
      if( m_lastType )
 
1384
        expressionType( node, m_lastType, m_lastInstance );
 
1385
    }else{
 
1386
      problem(node, "Could not resolve type");
 
1387
    }
 
1388
 
 
1389
    AbstractType::Ptr lastType = m_lastType;
 
1390
    Instance instance = m_lastInstance;
 
1391
 
 
1392
    if(node->new_initializer) {
 
1393
 
 
1394
      //Build constructor uses (similar to visitFunctionCall)
 
1395
      //Largely a copy of visitInitDeclarator()
 
1396
      
 
1397
      AbstractType::Ptr oldLastType = m_lastType;
 
1398
      Instance oldInstance = m_lastInstance;
 
1399
      QList< DeclarationPointer > declarations = m_lastDeclarations;
 
1400
      
 
1401
      clearLast();
 
1402
 
 
1403
      bool fail = !buildParametersFromExpression(node->new_initializer->expression);
 
1404
      
 
1405
      size_t token = node->new_initializer->start_token;
 
1406
 
 
1407
      DeclarationPointer chosenFunction;
 
1408
      {
 
1409
        LOCKDUCHAIN;
 
1410
        
 
1411
        KDevelop::DUContextPointer ptr(m_currentContext);
 
1412
        OverloadResolver resolver( ptr, KDevelop::TopDUContextPointer(topContext()), oldInstance );
 
1413
 
 
1414
        if( !fail )
 
1415
          chosenFunction = resolver.resolveList(m_parameters, convert(declarations));
 
1416
        else if(!declarations.isEmpty() && !m_strict)
 
1417
          chosenFunction = declarations.first();
 
1418
      }
 
1419
      
 
1420
      if(chosenFunction)
 
1421
        newUse( node , token, token+1, chosenFunction );    
 
1422
    }
 
1423
 
 
1424
    m_lastType = lastType;
 
1425
    m_lastInstance = instance;
 
1426
  }
 
1427
 
 
1428
  /**
 
1429
   *
 
1430
   * have test */
 
1431
  void ExpressionVisitor::visitConditionalExpression(ConditionalExpressionAST* node)
 
1432
  {
 
1433
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
1434
 
 
1435
    //Also visit the not interesting parts, so they are evaluated
 
1436
    clearLast();
 
1437
    
 
1438
    visit(node->condition);
 
1439
 
 
1440
    
 
1441
    if( dynamic_cast<DelayedType*>(m_lastType.unsafeData()) ) {
 
1442
      //Store the expression so it's evaluated later
 
1443
      m_lastInstance = Instance(true);
 
1444
      createDelayedType(node);
 
1445
      return;
 
1446
    }
 
1447
 
 
1448
    AbstractType::Ptr conditionType = m_lastType;
 
1449
 
 
1450
    clearLast();
 
1451
    visit(node->left_expression);
 
1452
    AbstractType::Ptr leftType = m_lastType;
 
1453
    clearLast();
 
1454
 
 
1455
 
 
1456
    ///@todo test if result of right expression can be converted to the result of the right expression. If not, post a problem(because c++ wants it that way)
 
1457
 
 
1458
    //Since both possible results of a conditional expression must have the same type, we only consider the right one here
 
1459
    visit(node->right_expression);
 
1460
 
 
1461
    {
 
1462
      LOCKDUCHAIN;
 
1463
      if( ConstantIntegralType* condition = dynamic_cast<ConstantIntegralType*>( conditionType.unsafeData() ) ) {
 
1464
        ///For constant integral types, the condition could be evaluated, so we choose the correct result.
 
1465
        if( condition->value<quint64>() == 0 ) {
 
1466
          ///The right expression is the correct one, so do nothing
 
1467
        } else {
 
1468
          ///Condition is true, so we choose the left expression value/type
 
1469
          m_lastType = leftType;
 
1470
        }
 
1471
      }
 
1472
    }
 
1473
 
 
1474
 
 
1475
    if( m_lastType )
 
1476
      expressionType( node, m_lastType, m_lastInstance );
 
1477
  }
 
1478
 
 
1479
  /**
 
1480
   * have test */
 
1481
  void ExpressionVisitor::visitExpressionStatement(ExpressionStatementAST* node)
 
1482
  {
 
1483
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
1484
    clearLast();
 
1485
    visit(node->expression);
 
1486
    if( m_lastType )
 
1487
      expressionType( node, m_lastType, m_lastInstance );
 
1488
  }
 
1489
 
 
1490
  /** For a compound statement, process all statements and return the type of the last one
 
1491
   *
 
1492
   * have test */
 
1493
  void ExpressionVisitor::visitCompoundStatement(CompoundStatementAST* node)
 
1494
  {
 
1495
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
1496
    visitIndependentNodes(node->statements);
 
1497
  }
 
1498
 
 
1499
  /**
 
1500
   * have test */
 
1501
 
 
1502
  void ExpressionVisitor::visitExpressionOrDeclarationStatement(ExpressionOrDeclarationStatementAST* node)  {
 
1503
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
1504
    //visit(node->declaration);
 
1505
    visit(node->expression);
 
1506
 
 
1507
    if( m_lastType )
 
1508
      expressionType( node, m_lastType, m_lastInstance );
 
1509
  }
 
1510
 
 
1511
  bool ExpressionVisitor::dereferenceLastPointer(AST* node) {
 
1512
    if( PointerType::Ptr pt = realLastType().cast<PointerType>() )
 
1513
    {
 
1514
      //Dereference
 
1515
      m_lastType = pt->baseType();
 
1516
      m_lastInstance = Instance( getDeclaration(m_lastType) );
 
1517
      return true;
 
1518
    }else if( ArrayType::Ptr pt = realLastType().cast<ArrayType>() ) {
 
1519
      m_lastType = pt->elementType();
 
1520
      m_lastInstance = Instance( getDeclaration(m_lastType) );
 
1521
      return true;
 
1522
    }else{
 
1523
      return false;
 
1524
    }
 
1525
  }
 
1526
  
 
1527
  /**
 
1528
   * partially have test */
 
1529
  void ExpressionVisitor::visitUnaryExpression(UnaryExpressionAST* node)
 
1530
  {
 
1531
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
1532
 
 
1533
    clearLast();
 
1534
 
 
1535
    visit(node->expression);
 
1536
 
 
1537
    if( !m_lastInstance || !m_lastType ) {
 
1538
      clearLast();
 
1539
      problem(node, "Tried to evaluate unary expression on a non-instance item" );
 
1540
      return;
 
1541
    }
 
1542
 
 
1543
    if( m_lastType.cast<DelayedType>() ) {
 
1544
      //Store the expression so it's evaluated later
 
1545
      m_lastInstance = Instance(true);
 
1546
      createDelayedType(node);
 
1547
      return;
 
1548
    }
 
1549
 
 
1550
    switch( tokenFromIndex(node->op).kind ) {
 
1551
    case '*':
 
1552
    {
 
1553
      LOCKDUCHAIN;
 
1554
      if( dereferenceLastPointer(node) ) {
 
1555
      } else {
 
1556
        //Get return-value of operator*
 
1557
        findMember(node, m_lastType, Identifier("operator*") );
 
1558
        if( !m_lastType ) {
 
1559
          problem( node, "no overloaded operator* found" );
 
1560
          return;
 
1561
        }
 
1562
 
 
1563
        getReturnValue(node);
 
1564
 
 
1565
        if( !m_lastDeclarations.isEmpty() ) {
 
1566
          DeclarationPointer decl( m_lastDeclarations.first() );
 
1567
          lock.unlock();
 
1568
          newUse( node, node->op, node->op+1, decl );
 
1569
        }
 
1570
      }
 
1571
    }
 
1572
    break;
 
1573
    case '&':
 
1574
    {
 
1575
      m_lastType = increasePointerDepth(m_lastType);
 
1576
      //m_lastInstance will be left alone as it was before. A pointer is not identified, and has no declaration.
 
1577
    }
 
1578
    break;
 
1579
    default:
 
1580
    {
 
1581
      KDevelop::IntegralType* integral = dynamic_cast<KDevelop::IntegralType*>(m_lastType.unsafeData());
 
1582
      if( integral ) {
 
1583
        //The type of integral types does not change on unary operators
 
1584
        //Eventually evaluate the value of constant integral types
 
1585
        ConstantIntegralType* constantIntegral = dynamic_cast<ConstantIntegralType*>(integral);
 
1586
 
 
1587
        if( constantIntegral ) {
 
1588
 
 
1589
          switch( constantIntegral->dataType() ) {
 
1590
            case IntegralType::TypeFloat:
 
1591
            {
 
1592
              ConstantUnaryExpressionEvaluator<float> evaluator( tokenFromIndex(node->op).kind, constantIntegral );
 
1593
              m_lastType = evaluator.createType();
 
1594
              break;
 
1595
            }
 
1596
            case IntegralType::TypeDouble:
 
1597
            {
 
1598
              ConstantUnaryExpressionEvaluator<double> evaluator( tokenFromIndex(node->op).kind, constantIntegral );
 
1599
              m_lastType = evaluator.createType();
 
1600
              break;
 
1601
            }
 
1602
            default:
 
1603
              if( constantIntegral->modifiers() & AbstractType::UnsignedModifier ) {
 
1604
                ConstantUnaryExpressionEvaluator<quint64> evaluator( tokenFromIndex(node->op).kind, constantIntegral );
 
1605
                m_lastType = evaluator.createType();
 
1606
              } else {
 
1607
                ConstantUnaryExpressionEvaluator<qint64> evaluator( tokenFromIndex(node->op).kind, constantIntegral );
 
1608
                m_lastType = evaluator.createType();
 
1609
              }
 
1610
              break;
 
1611
          }
 
1612
 
 
1613
          m_lastInstance = Instance(true);
 
1614
        }
 
1615
      } else {
 
1616
        QString op = operatorNameFromTokenKind(tokenFromIndex(node->op).kind);
 
1617
        if( !op.isEmpty() )
 
1618
        {
 
1619
          LOCKDUCHAIN;
 
1620
          KDevelop::DUContextPointer ptr(m_currentContext);
 
1621
          OverloadResolutionHelper helper( ptr, TopDUContextPointer(topContext()) );
 
1622
          helper.setOperator( OverloadResolver::Parameter(m_lastType, isLValue( m_lastType, m_lastInstance ) ), op );
 
1623
 
 
1624
          //helper.setKnownParameters( OverloadResolver::Parameter(rightType, isLValue( rightType, rightInstance ) ) );
 
1625
          QList<OverloadResolutionFunction> functions = helper.resolve(false);
 
1626
 
 
1627
          if( !functions.isEmpty() )
 
1628
          {
 
1629
            KDevelop::FunctionType::Ptr function = functions.first().function.declaration()->type<KDevelop::FunctionType>();
 
1630
            if( functions.first().function.isViable() && function ) {
 
1631
              m_lastType = function->returnType();
 
1632
              m_lastInstance = Instance(true);
 
1633
 
 
1634
              lock.unlock();
 
1635
              newUse( node, node->op, node->op+1, functions.first().function.declaration() );
 
1636
            }else{
 
1637
              problem(node, QString("Found no viable function"));
 
1638
            }
 
1639
          }else{
 
1640
            //Do not complain here, because we do not check for builtin operators
 
1641
            //problem(node, "No fitting operator. found" );
 
1642
          }
 
1643
 
 
1644
        }else{
 
1645
          problem(node, "Invalid unary expression");
 
1646
        }
 
1647
      }
 
1648
    }
 
1649
    break;
 
1650
    }
 
1651
 
 
1652
    if( m_lastType )
 
1653
      expressionType( node, m_lastType, m_lastInstance );
 
1654
  }
 
1655
 
 
1656
  void ExpressionVisitor::getReturnValue( AST* node ) {
 
1657
    if( !m_lastType )
 
1658
      return;
 
1659
 
 
1660
    KDevelop::FunctionType* f = dynamic_cast<KDevelop::FunctionType*>( m_lastType.unsafeData() );
 
1661
    if( !f ) {
 
1662
      LOCKDUCHAIN;
 
1663
      problem(node, QString("cannot get return-type of type %1, it is not a function-type").arg(m_lastType->toString()));
 
1664
      m_lastType = 0;
 
1665
      m_lastInstance = Instance();
 
1666
      return;
 
1667
    }
 
1668
 
 
1669
    m_lastType = f->returnType();
 
1670
    //Just keep the function instance, set in findMember(..)
 
1671
  }
 
1672
  
 
1673
 
 
1674
  CppClassType::Ptr ExpressionVisitor::computeConstructedType()
 
1675
  {
 
1676
    CppClassType::Ptr constructedType;
 
1677
 
 
1678
    AbstractType::Ptr oldLastType = m_lastType;
 
1679
    
 
1680
    if(!m_lastInstance) {
 
1681
      LOCKDUCHAIN;
 
1682
      if(m_lastDeclarations.isEmpty() && m_lastType && !m_lastInstance) {
 
1683
        IdentifiedType* idType = dynamic_cast<IdentifiedType*>(m_lastType.unsafeData());
 
1684
        if(idType) {
 
1685
          Declaration* decl = idType->declaration(m_source);
 
1686
          if(decl)
 
1687
            m_lastDeclarations << DeclarationPointer(decl);
 
1688
        }
 
1689
      }
 
1690
      
 
1691
      if( !m_lastDeclarations.isEmpty() && m_lastDeclarations.first().data() && m_lastDeclarations.first()->kind() == Declaration::Type && (constructedType = unAliasedType(m_lastDeclarations.first()->logicalDeclaration(topContext())->abstractType()).cast<CppClassType>()) ) {
 
1692
 
 
1693
        if( constructedType && constructedType->declaration(topContext()) && constructedType->declaration(topContext())->internalContext() )
 
1694
        {
 
1695
          Declaration* constructedDecl = constructedType->declaration(topContext());
 
1696
          
 
1697
          //Replace a type with its constructros if there is constructors available, so overload-resolution can happen
 
1698
          m_lastDeclarations = convert(constructedDecl->internalContext()->findLocalDeclarations( constructedDecl->identifier(), constructedDecl->internalContext()->range().end, topContext(), AbstractType::Ptr(), DUContext::OnlyFunctions ));
 
1699
        }
 
1700
      }
 
1701
    }
 
1702
 
 
1703
    return constructedType;
 
1704
  }
 
1705
 
 
1706
  bool ExpressionVisitor::buildParametersFromDeclaration(ParameterDeclarationClauseAST* node, bool store)
 
1707
  {
 
1708
    if(store) {
 
1709
      m_parameters.clear();
 
1710
      m_parameterNodes.clear();
 
1711
    }
 
1712
 
 
1713
    if(node->parameter_declarations)
 
1714
    {
 
1715
      const ListNode<ParameterDeclarationAST*>
 
1716
        *it = node->parameter_declarations->toFront(),
 
1717
        *end = it;
 
1718
 
 
1719
      do
 
1720
        {
 
1721
          //Just to make sure we build the uses. This problem only appears if we mis-parse a declarator as a parameter declaration
 
1722
          if(it->element->declarator && it->element->declarator->array_dimensions)
 
1723
          {
 
1724
            const ListNode< ExpressionAST* >* itt = it->element->declarator->array_dimensions->toFront(), *end2 = itt;
 
1725
            do{
 
1726
              visit(it->element->declarator->array_dimensions->element);
 
1727
            }while(itt != end2);
 
1728
          }
 
1729
          
 
1730
          visit(it->element->type_specifier);
 
1731
          
 
1732
          if(it->element->declarator) {
 
1733
            ///@todo Eventually build constructor uses for mis-parsed sub-declarators or parameter-declaration-clauses
 
1734
            if(it->element->declarator->sub_declarator && it->element->declarator->sub_declarator->id)
 
1735
            {
 
1736
              //Special handling is required: Things that really are initializers are treated as declarators in a mis-parsed ParameterDeclarationClause
 
1737
              visitName(it->element->declarator->sub_declarator->id);
 
1738
            }else if(it->element->declarator->parameter_declaration_clause)
 
1739
            {
 
1740
              buildParametersFromDeclaration(it->element->declarator->parameter_declaration_clause, false);
 
1741
            }
 
1742
          }
 
1743
          visit(it->element->expression);
 
1744
          if(store) {
 
1745
            m_parameters.append( OverloadResolver::Parameter( m_lastType, isLValue( m_lastType, m_lastInstance ) ) );
 
1746
            m_parameterNodes.append(it->element);
 
1747
          }
 
1748
          it = it->next;
 
1749
        }
 
1750
      while (it != end);
 
1751
    }
 
1752
    
 
1753
    bool fail = false;
 
1754
    
 
1755
    if(store) {
 
1756
      //Check if all parameters could be evaluated
 
1757
      int paramNum = 1;
 
1758
      for( QList<OverloadResolver::Parameter>::const_iterator it = m_parameters.constBegin(); it != m_parameters.constEnd(); ++it ) {
 
1759
        if( !(*it).type ) {
 
1760
          problem( node, QString("parameter %1 could not be evaluated").arg(paramNum) );
 
1761
          fail = true;
 
1762
          paramNum++;
 
1763
        }
 
1764
      }
 
1765
    }
 
1766
    
 
1767
    return !fail;
 
1768
  }
 
1769
 
 
1770
  bool ExpressionVisitor::buildParametersFromExpression(AST* expression)
 
1771
  {
 
1772
    /**
 
1773
     * Evaluate the function-argument types. Those are represented a little strangely:
 
1774
     * expression contains them, using recursive binary expressions
 
1775
     * */
 
1776
    
 
1777
    m_parameters.clear();
 
1778
    m_parameterNodes.clear();
 
1779
    
 
1780
    if(!expression)
 
1781
      return true;
 
1782
    
 
1783
    visit(expression);
 
1784
 
 
1785
    //binary expressions don't yield m_lastType, so when m_lastType is set wo probably only have one single parameter
 
1786
    if( m_lastType ) {
 
1787
      m_parameters << OverloadResolver::Parameter( m_lastType, isLValue( m_lastType, m_lastInstance ) );
 
1788
      m_parameterNodes.append(expression);
 
1789
    }
 
1790
    
 
1791
    //Check if all parameters could be evaluated
 
1792
    int paramNum = 1;
 
1793
    bool fail = false;
 
1794
    for( QList<OverloadResolver::Parameter>::const_iterator it = m_parameters.constBegin(); it != m_parameters.constEnd(); ++it ) {
 
1795
      if( !(*it).type ) {
 
1796
        problem( expression, QString("parameter %1 could not be evaluated").arg(paramNum) );
 
1797
        fail = true;
 
1798
        paramNum++;
 
1799
      }
 
1800
    }
 
1801
    
 
1802
    return !fail;
 
1803
  }
 
1804
 
 
1805
  void ExpressionVisitor::visitFunctionCall(FunctionCallAST* node) {
 
1806
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
1807
 
 
1808
    /**
 
1809
     * If a class name was found, get its constructors.
 
1810
     * */
 
1811
    AbstractType::Ptr oldLastType = m_lastType;
 
1812
    
 
1813
    CppClassType::Ptr constructedType;
 
1814
 
 
1815
    if(!m_lastInstance) {
 
1816
      constructedType = computeConstructedType();
 
1817
    }else{
 
1818
      if(m_lastDeclarations.isEmpty() && m_lastType) {
 
1819
        LOCKDUCHAIN;
 
1820
        //The function-call operator may also be applied on instances that don't have an explicit declaration, like return-values
 
1821
        AbstractType::Ptr type = realType(m_lastType);
 
1822
        IdentifiedType* identified = dynamic_cast<IdentifiedType*>(type.unsafeData());
 
1823
        if(identified) {
 
1824
          DeclarationPointer decl(identified->declaration(m_source));
 
1825
          if(decl) {
 
1826
            m_lastDeclarations << decl;
 
1827
          }
 
1828
        }
 
1829
      }
 
1830
    }
 
1831
 
 
1832
    QList<DeclarationPointer> declarations = m_lastDeclarations;
 
1833
    Instance oldInstance = m_lastInstance;
 
1834
 
 
1835
    QList<OverloadResolver::Parameter> oldParams = m_parameters;
 
1836
    KDevVarLengthArray<AST*> oldParameterNodes = m_parameterNodes;
 
1837
 
 
1838
    //Backup the current use and invalidate it, we will update and create it after overload-resolution
 
1839
    CurrentUse oldCurrentUse = m_currentUse;
 
1840
    m_currentUse.isValid = false;
 
1841
 
 
1842
    clearLast();
 
1843
    
 
1844
    bool fail = !buildParametersFromExpression(node->arguments);
 
1845
    
 
1846
    if( declarations.isEmpty() && !constructedType ) {
 
1847
 
 
1848
      if(MissingDeclarationType::Ptr missing = oldLastType.cast<Cpp::MissingDeclarationType>()) {
 
1849
        missing->arguments = m_parameters;
 
1850
        missing->isFunction = true;
 
1851
      }
 
1852
      m_lastType = oldLastType;
 
1853
      problem( node, "function-call: no matching declarations found" );
 
1854
      return;
 
1855
    }
 
1856
 
 
1857
    LOCKDUCHAIN;
 
1858
 
 
1859
    if(declarations.isEmpty() && constructedType) {
 
1860
      //Default-constructor is used
 
1861
      m_lastType = AbstractType::Ptr(constructedType.unsafeData());
 
1862
      DeclarationPointer decl(constructedType->declaration(topContext()));
 
1863
      m_lastInstance = Instance(decl.data());
 
1864
      m_lastDeclarations.clear();
 
1865
//       m_lastDeclarations << decl;
 
1866
      lock.unlock();
 
1867
      if(oldCurrentUse.isValid) {
 
1868
        newUse( oldCurrentUse.node, oldCurrentUse.start_token, oldCurrentUse.end_token, decl );
 
1869
      }
 
1870
      flushUse();
 
1871
      return;
 
1872
    }
 
1873
 
 
1874
    //Resolve functions
 
1875
    DeclarationPointer chosenFunction;
 
1876
    KDevelop::DUContextPointer ptr(m_currentContext);
 
1877
    OverloadResolver resolver( ptr, KDevelop::TopDUContextPointer(topContext()), oldInstance );
 
1878
 
 
1879
    if( !fail )
 
1880
      chosenFunction = resolver.resolveList(m_parameters, convert(declarations));
 
1881
 
 
1882
    if( !chosenFunction && !m_strict ) {
 
1883
      //Because we do not want to rely too much on our understanding of the code, we take the first function instead of totally failing.
 
1884
#ifdef DEBUG_FUNCTION_CALLS
 
1885
      QString params;
 
1886
      foreach(const OverloadResolver::Parameter& param, m_parameters)
 
1887
        params += param.toString() + ", ";
 
1888
 
 
1889
      QString candidates;
 
1890
      foreach(const DeclarationPointer &decl, declarations) {
 
1891
        if( !decl.data() )
 
1892
          continue;
 
1893
        int defaultParamCount = 0;
 
1894
        if( AbstractFunctionDeclaration* aDec = dynamic_cast<AbstractFunctionDeclaration*>(decl.data()) )
 
1895
          defaultParamCount = aDec->defaultParameters().count();
 
1896
 
 
1897
        candidates += decl->toString() + QString(" default-params: %1").arg(defaultParamCount) + '\n';
 
1898
      }
 
1899
 
 
1900
      problem(node, QString("Could not find a function that matches the parameters. Using first candidate function. Parameters: %1 Candidates: %2").arg(params).arg(candidates));
 
1901
#endif
 
1902
      fail = true;
 
1903
    }
 
1904
 
 
1905
    if( fail ) {
 
1906
      //Since not all parameters could be evaluated, Choose the first function
 
1907
      chosenFunction = declarations.front();
 
1908
    }
 
1909
 
 
1910
    clearLast();
 
1911
 
 
1912
    if( constructedType ) {
 
1913
      //Constructor was called
 
1914
      m_lastType = AbstractType::Ptr(constructedType.unsafeData());
 
1915
      m_lastInstance = Instance(constructedType->declaration(topContext()));
 
1916
    } else {
 
1917
      KDevelop::FunctionType::Ptr functionType = chosenFunction->abstractType().cast<KDevelop::FunctionType>();
 
1918
      if( !chosenFunction || !functionType ) {
 
1919
        problem( node, QString( "could not find a matching function for function-call" ) );
 
1920
      } else {
 
1921
        m_lastType = functionType->returnType();
 
1922
        m_lastInstance = Instance(chosenFunction);
 
1923
      }
 
1924
    }
 
1925
 
 
1926
    static IndexedString functionCallOperatorIdentifier("operator()");
 
1927
    
 
1928
    bool createUseOnParen = (bool)chosenFunction && (constructedType || chosenFunction->identifier().identifier() == functionCallOperatorIdentifier);
 
1929
    //Re-create the use we have discarded earlier, this time with the correct overloaded function chosen.
 
1930
    lock.unlock();
 
1931
    
 
1932
    if(createUseOnParen) {
 
1933
      if(oldCurrentUse.isValid)
 
1934
        newUse( oldCurrentUse.node, oldCurrentUse.start_token, oldCurrentUse.end_token, oldCurrentUse.declaration );
 
1935
      
 
1936
      newUse( node , node->start_token, node->start_token+1, chosenFunction );
 
1937
    }else if(oldCurrentUse.isValid) {
 
1938
      newUse( oldCurrentUse.node, oldCurrentUse.start_token, oldCurrentUse.end_token, chosenFunction );
 
1939
    }
 
1940
 
 
1941
    m_parameterNodes = oldParameterNodes;
 
1942
    m_parameters = oldParams;
 
1943
    
 
1944
    flushUse();
 
1945
    
 
1946
    if( m_lastType )
 
1947
      expressionType( node, m_lastType, m_lastInstance );
 
1948
  }
 
1949
  
 
1950
  void ExpressionVisitor::visitSignalSlotExpression(SignalSlotExpressionAST* node) {
 
1951
    
 
1952
    //So uses for the argument-types are built
 
1953
    LOCKDUCHAIN;
 
1954
    
 
1955
    putStringType();
 
1956
    
 
1957
    if(m_parameters.isEmpty())
 
1958
      return;
 
1959
    
 
1960
    DUContext* container = 0;///@todo check whether signal/slot match, warn if not.
 
1961
    
 
1962
    StructureType::Ptr slotStructure = TypeUtils::targetType(TypeUtils::matchingClassPointer(qObjectPtrType(), TypeUtils::realType(m_parameters.back().type, m_topContext), m_topContext), m_topContext).cast<StructureType>();
 
1963
    if(slotStructure)
 
1964
      container = slotStructure->internalContext(m_topContext);    
 
1965
    
 
1966
    if(!container) {
 
1967
      Declaration* klass = Cpp::localClassFromCodeContext(m_currentContext);
 
1968
      if(klass)
 
1969
        container = klass->internalContext();
 
1970
    }
 
1971
    
 
1972
    if(!container) {
 
1973
      lock.unlock();
 
1974
      problem(node, QString("No signal/slot container"));
 
1975
      return;
 
1976
    }
 
1977
    
 
1978
    if(!node->name) {
 
1979
      problem(node, QString("Bad signal/slot"));
 
1980
      return;
 
1981
    }
 
1982
    
 
1983
    {
 
1984
      SimpleCursor position = container->range().end;
 
1985
      lock.unlock();
 
1986
      //This builds the uses
 
1987
      NameASTVisitor nameV( m_session, this, container, topContext(), m_currentContext, position );
 
1988
      nameV.run(node->name, true);
 
1989
      lock.lock();
 
1990
    }
 
1991
    
 
1992
 
 
1993
    CppEditorIntegrator editor(session());
 
1994
    QByteArray tokenByteArray = editor.tokensToByteArray(node->name->id, node->name->end_token);
 
1995
    
 
1996
    QByteArray sig;
 
1997
    if(node->name->end_token-1 >= node->name->id+2) {
 
1998
      sig = QMetaObject::normalizedSignature( editor.tokensToByteArray(node->name->id+1, node->name->end_token) );
 
1999
      sig = sig.mid(1, sig.length()-2);
 
2000
    }
 
2001
 
 
2002
    Identifier id(tokenFromIndex(node->name->id).symbol());
 
2003
    
 
2004
    if(!id.isEmpty()) {
 
2005
      foreach(Declaration* decl, container->findDeclarations(id, SimpleCursor::invalid(), m_topContext, (DUContext::SearchFlags)(DUContext::DontSearchInParent | DUContext::NoFiltering))) {
 
2006
        QtFunctionDeclaration* qtFunction = dynamic_cast<QtFunctionDeclaration*>(decl);
 
2007
        if(qtFunction) {
 
2008
            ///Allow incomplete matching between the specified signature and the real signature, as Qt allows it.
 
2009
            ///@todo: For signals, we should only allow it when at least as many arguments are specified as in the slot declaration.
 
2010
            ///@todo: For slots, we should only allow it if the parameter has a default argument.
 
2011
            uint functionSigLength = qtFunction->normalizedSignature().length();
 
2012
            const char* functionSig = qtFunction->normalizedSignature().c_str();
 
2013
            
 
2014
            if(functionSigLength >= sig.length() &&
 
2015
               strncmp(functionSig, sig.data(), sig.length()) == 0 &&
 
2016
               (sig.isEmpty() || functionSigLength == sig.length() || functionSig[sig.length()] == ' ' || functionSig[sig.length()] == ','))
 
2017
            {
 
2018
              //Match
 
2019
              lock.unlock();
 
2020
              newUse( node, node->name->id, node->name->id+1, DeclarationPointer(qtFunction) );
 
2021
              return;
 
2022
            }
 
2023
        }
 
2024
      }
 
2025
    }
 
2026
  }
 
2027
 
 
2028
  void ExpressionVisitor::visitSubscriptExpression(SubscriptExpressionAST* node)
 
2029
  {
 
2030
    ///@todo create use
 
2031
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
2032
 
 
2033
    Instance masterInstance = m_lastInstance;
 
2034
    AbstractType::Ptr masterType = m_lastType;
 
2035
 
 
2036
    if( !masterType || !masterInstance ) {
 
2037
      problem(node, "Tried subscript-expression on invalid object");
 
2038
      return;
 
2039
    }
 
2040
 
 
2041
    {
 
2042
      LOCKDUCHAIN;
 
2043
 
 
2044
      //If the type the subscript-operator is applied on is a pointer, dereference it
 
2045
      if( dereferenceLastPointer(node) ) {
 
2046
        //Make visit the sub-expression, so uses are built
 
2047
        lock.unlock();
 
2048
 
 
2049
        masterInstance = m_lastInstance;
 
2050
        masterType = m_lastType;
 
2051
 
 
2052
        visit(node->subscript); //Visit so uses are built
 
2053
        clearLast();
 
2054
 
 
2055
        m_lastType = masterType;
 
2056
        m_lastInstance = masterInstance;
 
2057
        return;
 
2058
      }
 
2059
    }
 
2060
 
 
2061
    clearLast();
 
2062
 
 
2063
    visit(node->subscript);
 
2064
 
 
2065
    LOCKDUCHAIN;
 
2066
 
 
2067
    KDevelop::DUContextPointer ptr(m_currentContext);
 
2068
    OverloadResolutionHelper helper( ptr, TopDUContextPointer(topContext()) );
 
2069
    helper.setOperator( OverloadResolver::Parameter(masterType, isLValue( masterType, masterInstance ) ), "[]" );
 
2070
 
 
2071
    helper.setKnownParameters( OverloadResolver::Parameter( m_lastType, isLValue( m_lastType, m_lastInstance ) ) );
 
2072
    QList<OverloadResolutionFunction> functions = helper.resolve(false);
 
2073
 
 
2074
    if( !functions.isEmpty() )
 
2075
    {
 
2076
      KDevelop::FunctionType::Ptr function = functions.first().function.declaration()->type<KDevelop::FunctionType>();
 
2077
 
 
2078
      if( function ) {
 
2079
        m_lastType = function->returnType();
 
2080
        m_lastInstance = Instance(true);
 
2081
      }else{
 
2082
        clearLast();
 
2083
        problem(node, QString("Found no subscript-function"));
 
2084
      }
 
2085
 
 
2086
      if( !functions.first().function.isViable() ) {
 
2087
        problem(node, QString("Found no viable subscript-function, chosen function: %1").arg(functions.first().function.declaration() ? functions.first().function.declaration()->toString() : QString()));
 
2088
      }
 
2089
 
 
2090
    }else{
 
2091
      clearLast();
 
2092
      //Do not complain here, because we do not check for builtin operators
 
2093
      //problem(node, "No fitting operator. found" );
 
2094
    }
 
2095
  }
 
2096
  
 
2097
  void ExpressionVisitor::visitSizeofExpression(SizeofExpressionAST* node)  {
 
2098
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
2099
    visit(node->type_id);
 
2100
    visit(node->expression);
 
2101
    LOCKDUCHAIN;
 
2102
    m_lastType = AbstractType::Ptr( new KDevelop::IntegralType(IntegralType::TypeInt) );
 
2103
    m_lastInstance = Instance(true);
 
2104
  }
 
2105
 
 
2106
  void ExpressionVisitor::visitCondition(ConditionAST* node)  {
 
2107
    LOCKDUCHAIN;
 
2108
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
2109
    m_lastType = AbstractType::Ptr( new KDevelop::IntegralType(IntegralType::TypeBoolean) );
 
2110
    m_lastInstance = Instance(true);
 
2111
  }
 
2112
 
 
2113
  void ExpressionVisitor::visitTypeId(TypeIdAST* type_id)  {
 
2114
    PushPositiveContext pushContext( m_currentContext, type_id->ducontext );
 
2115
    visit(type_id->type_specifier);
 
2116
    visit(type_id->declarator);
 
2117
  }
 
2118
 
 
2119
  AbstractType::Ptr ExpressionVisitor::qObjectPtrType() const {
 
2120
    CppClassType::Ptr p(new CppClassType());
 
2121
    p->setDeclarationId( DeclarationId(QualifiedIdentifier("QObject")) );
 
2122
    PointerType::Ptr pointer(new PointerType);
 
2123
    pointer->setBaseType(p.cast<AbstractType>());
 
2124
    return pointer.cast<AbstractType>();
 
2125
  }
 
2126
 
 
2127
  void ExpressionVisitor::putStringType() {
 
2128
    IntegralType::Ptr i(new KDevelop::IntegralType(IntegralType::TypeChar));
 
2129
    i->setModifiers(AbstractType::ConstModifier);
 
2130
    
 
2131
    PointerType::Ptr p( new PointerType() );
 
2132
    
 
2133
    p->setBaseType( AbstractType::Ptr(i.unsafeData()) );
 
2134
 
 
2135
    m_lastType = p.cast<AbstractType>();
 
2136
    m_lastInstance = Instance(true);
 
2137
  }
 
2138
 
 
2139
 
 
2140
  void ExpressionVisitor::visitStringLiteral(StringLiteralAST* node)  {
 
2141
    LOCKDUCHAIN;
 
2142
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
2143
 
 
2144
    putStringType();
 
2145
  }
 
2146
 
 
2147
  void ExpressionVisitor::visitIncrDecrExpression(IncrDecrExpressionAST* node)  {
 
2148
 
 
2149
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
2150
 
 
2151
    ///post-fix increment/decrement like "i++" or "i--"
 
2152
    ///This does neither change the evaluated value, nor the type(except for overloaded operators)
 
2153
 
 
2154
    if( dynamic_cast<KDevelop::IntegralType*>(m_lastType.unsafeData()) ) {
 
2155
      ///Leave the type and its value alone
 
2156
    } else {
 
2157
      ///It is not an integral type, try finding an overloaded operator and use the return-value
 
2158
      QString op = operatorNameFromTokenKind(tokenFromIndex(node->op).kind);
 
2159
      if( !op.isEmpty() )
 
2160
      {
 
2161
        LOCKDUCHAIN;
 
2162
        KDevelop::DUContextPointer ptr(m_currentContext);
 
2163
        OverloadResolutionHelper helper( ptr, TopDUContextPointer(topContext()) );
 
2164
        helper.setOperator( OverloadResolver::Parameter(m_lastType, isLValue( m_lastType, m_lastInstance ) ), op );
 
2165
 
 
2166
        //Overloaded postfix operators have one additional int parameter
 
2167
        static AbstractType::Ptr integer = AbstractType::Ptr(new ConstantIntegralType(IntegralType::TypeInt));
 
2168
        helper.setKnownParameters( OverloadResolver::Parameter( integer, false ) );
 
2169
 
 
2170
        QList<OverloadResolutionFunction> functions = helper.resolve(false);
 
2171
 
 
2172
        if( !functions.isEmpty() )
 
2173
        {
 
2174
          KDevelop::FunctionType::Ptr function = functions.first().function.declaration()->type<KDevelop::FunctionType>();
 
2175
          if( functions.first().function.isViable() && function ) {
 
2176
            m_lastType = function->returnType();
 
2177
            m_lastInstance = Instance(true);
 
2178
          }else{
 
2179
            problem(node, QString("Found no viable function"));
 
2180
          }
 
2181
 
 
2182
          lock.unlock();
 
2183
          newUse( node, node->op, node->op+1, functions.first().function.declaration() );
 
2184
        }else{
 
2185
          //Do not complain here, because we do not check for builtin operators
 
2186
          //problem(node, "No fitting operator. found" );
 
2187
        }
 
2188
      }
 
2189
    }
 
2190
 
 
2191
    if( m_lastType )
 
2192
      expressionType( node, m_lastType, m_lastInstance );
 
2193
  }
 
2194
 
 
2195
#if 0
 
2196
  void ExpressionVisitor::visitNewTypeId(NewTypeIdAST* node)  {
 
2197
    //Return a pointer to the type
 
2198
    problem(node);
 
2199
  }
 
2200
  #endif
 
2201
 
 
2202
  void ExpressionVisitor::visitSimpleDeclaration(SimpleDeclarationAST* node)  {
 
2203
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
2204
    ///Simple type-specifiers like "int" are parsed as SimpleDeclarationAST, so treat them here.
 
2205
    ///Also we use this to parse constructor uses
 
2206
    visit(node->type_specifier);
 
2207
    QList< DeclarationPointer > oldLastDecls = m_lastDeclarations;
 
2208
    AbstractType::Ptr oldLastType = m_lastType;
 
2209
    Instance oldLastInstance = m_lastInstance;
 
2210
 
 
2211
    if(node->init_declarators)
 
2212
    {
 
2213
      const ListNode<InitDeclaratorAST*>
 
2214
        *it = node->init_declarators->toFront(),
 
2215
        *end = it;
 
2216
 
 
2217
      do
 
2218
        {
 
2219
          //Make sure each init-declarator gets the same type assigned
 
2220
          m_lastDeclarations = oldLastDecls;
 
2221
          m_lastType = oldLastType;
 
2222
          m_lastInstance = oldLastInstance;
 
2223
          
 
2224
          visit(it->element);
 
2225
          it = it->next;
 
2226
        }
 
2227
      while (it != end);
 
2228
    }
 
2229
    
 
2230
    visit(node->win_decl_specifiers);
 
2231
  }
 
2232
 
 
2233
  void ExpressionVisitor::visitDeclarationStatement(DeclarationStatementAST* node)  {
 
2234
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
2235
    ///Simple type-specifiers like "int" are parsed as SimpleDeclarationAST, so treat them here.
 
2236
    visit( node->declaration );
 
2237
  }
 
2238
  void ExpressionVisitor::visitThrowExpression(ThrowExpressionAST* node)  {
 
2239
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
2240
    visit( node->expression );
 
2241
  }
 
2242
  void ExpressionVisitor::visitDeleteExpression(DeleteExpressionAST* node)  {
 
2243
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
2244
    visit( node->expression );
 
2245
  }
 
2246
 
 
2247
  void ExpressionVisitor::visitNewInitializer(NewInitializerAST* node)  {
 
2248
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
2249
    visit(node->expression);
 
2250
  }
 
2251
 
 
2252
  void ExpressionVisitor::visitElaboratedTypeSpecifier(ElaboratedTypeSpecifierAST* node)  {
 
2253
    //Happens as template-parameter
 
2254
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
2255
    visit(node->name);
 
2256
    ///@todo respect const etc.
 
2257
  }
 
2258
 
 
2259
  void ExpressionVisitor::visitMemInitializer(MemInitializerAST* node)  {
 
2260
 
 
2261
    PushPositiveContext pushContext( m_currentContext, node->ducontext );
 
2262
    {
 
2263
      LOCKDUCHAIN;
 
2264
      Declaration* klass = localClassFromCodeContext(m_currentContext);
 
2265
      if(klass)
 
2266
        m_lastType = klass->abstractType();
 
2267
    }
 
2268
 
 
2269
    m_memberAccess = true;
 
2270
    visit(node->initializer_id);
 
2271
    m_memberAccess = false;
 
2272
 
 
2273
    AbstractType::Ptr itemType = m_lastType;
 
2274
    Instance oldLastInstance = m_lastInstance;
 
2275
    QList< DeclarationPointer > declarations = m_lastDeclarations;
 
2276
    if (buildParametersFromExpression(node->expression)) {
 
2277
      // build use for the ctor of the base class, see also visitInitDeclarator
 
2278
      DeclarationPointer chosenFunction;
 
2279
      {
 
2280
        LOCKDUCHAIN;
 
2281
 
 
2282
        KDevelop::DUContextPointer ptr(m_currentContext);
 
2283
        OverloadResolver resolver( ptr, KDevelop::TopDUContextPointer(topContext()), oldLastInstance );
 
2284
 
 
2285
        chosenFunction = resolver.resolveList(m_parameters, convert(declarations));
 
2286
      }
 
2287
 
 
2288
      if (chosenFunction) {
 
2289
        uint token = node->initializer_id->end_token;
 
2290
        newUse( node , token, token+1, chosenFunction );
 
2291
      }
 
2292
    }
 
2293
 
 
2294
    visit(node->expression);
 
2295
    TypePtr< MissingDeclarationType > missingDeclType = itemType.cast<MissingDeclarationType>();
 
2296
 
 
2297
    if(m_lastType && missingDeclType) {
 
2298
        Cpp::ExpressionEvaluationResult res;
 
2299
        res.type = m_lastType->indexed();
 
2300
        res.isInstance = m_lastInstance;
 
2301
        missingDeclType->assigned = res;
 
2302
    }
 
2303
  }
 
2304
 
 
2305
  ///Nodes that are invalid inside an expression:
 
2306
  void ExpressionVisitor::visitPtrToMember(PtrToMemberAST* node)  { DefaultVisitor::visitPtrToMember(node); }
 
2307
  void ExpressionVisitor::visitOperatorFunctionId(OperatorFunctionIdAST* node)  { DefaultVisitor::visitOperatorFunctionId(node); }
 
2308
  void ExpressionVisitor::visitTypeIdentification(TypeIdentificationAST* node)  { DefaultVisitor::visitTypeIdentification(node); }
 
2309
  void ExpressionVisitor::visitUnqualifiedName(UnqualifiedNameAST* node)  { DefaultVisitor::visitUnqualifiedName(node); }
 
2310
  void ExpressionVisitor::visitOperator(OperatorAST* node)  { DefaultVisitor::visitOperator(node); }
 
2311
  void ExpressionVisitor::visitAccessSpecifier(AccessSpecifierAST* node)  { DefaultVisitor::visitAccessSpecifier(node); }
 
2312
  void ExpressionVisitor::visitAsmDefinition(AsmDefinitionAST* node)  { DefaultVisitor::visitAsmDefinition(node); }
 
2313
  void ExpressionVisitor::visitBaseClause(BaseClauseAST* node)  { DefaultVisitor::visitBaseClause(node); }
 
2314
  void ExpressionVisitor::visitBaseSpecifier(BaseSpecifierAST* node)  { DefaultVisitor::visitBaseSpecifier(node); }
 
2315
  void ExpressionVisitor::visitClassSpecifier(ClassSpecifierAST* node)  { DefaultVisitor::visitClassSpecifier(node); }
 
2316
 
 
2317
  void ExpressionVisitor::visitCtorInitializer(CtorInitializerAST* node)  { DefaultVisitor::visitCtorInitializer(node); }
 
2318
  void ExpressionVisitor::visitDoStatement(DoStatementAST* node)  { DefaultVisitor::visitDoStatement(node); }
 
2319
  void ExpressionVisitor::visitEnumSpecifier(EnumSpecifierAST* node)  { DefaultVisitor::visitEnumSpecifier(node); }
 
2320
  void ExpressionVisitor::visitEnumerator(EnumeratorAST* node)  { DefaultVisitor::visitEnumerator(node); }
 
2321
  void ExpressionVisitor::visitExceptionSpecification(ExceptionSpecificationAST* node)  { DefaultVisitor::visitExceptionSpecification(node); }
 
2322
  void ExpressionVisitor::visitForStatement(ForStatementAST* node)  { DefaultVisitor::visitForStatement(node); }
 
2323
  void ExpressionVisitor::visitFunctionDefinition(FunctionDefinitionAST* node)  { DefaultVisitor::visitFunctionDefinition(node); }
 
2324
  void ExpressionVisitor::visitIfStatement(IfStatementAST* node)  { DefaultVisitor::visitIfStatement(node); }
 
2325
  void ExpressionVisitor::visitLabeledStatement(LabeledStatementAST* node)  { DefaultVisitor::visitLabeledStatement(node); }
 
2326
  void ExpressionVisitor::visitLinkageBody(LinkageBodyAST* node)  { DefaultVisitor::visitLinkageBody(node); }
 
2327
  void ExpressionVisitor::visitLinkageSpecification(LinkageSpecificationAST* node)  { DefaultVisitor::visitLinkageSpecification(node); }
 
2328
  void ExpressionVisitor::visitNamespace(NamespaceAST* node)  { DefaultVisitor::visitNamespace(node); }
 
2329
  void ExpressionVisitor::visitNamespaceAliasDefinition(NamespaceAliasDefinitionAST* node)  { DefaultVisitor::visitNamespaceAliasDefinition(node); }
 
2330
  void ExpressionVisitor::visitParameterDeclaration(ParameterDeclarationAST* node)  { DefaultVisitor::visitParameterDeclaration(node); }
 
2331
  void ExpressionVisitor::visitParameterDeclarationClause(ParameterDeclarationClauseAST* node)  { DefaultVisitor::visitParameterDeclarationClause(node); }
 
2332
  void ExpressionVisitor::visitReturnStatement(ReturnStatementAST* node)  { DefaultVisitor::visitReturnStatement(node); }
 
2333
  void ExpressionVisitor::visitSwitchStatement(SwitchStatementAST* node)  { DefaultVisitor::visitSwitchStatement(node); }
 
2334
  void ExpressionVisitor::visitTemplateArgument(TemplateArgumentAST* node)  { DefaultVisitor::visitTemplateArgument(node); }
 
2335
  void ExpressionVisitor::visitTemplateDeclaration(TemplateDeclarationAST* node)  { DefaultVisitor::visitTemplateDeclaration(node); }
 
2336
  void ExpressionVisitor::visitTemplateParameter(TemplateParameterAST* node)  { DefaultVisitor::visitTemplateParameter(node); }
 
2337
  void ExpressionVisitor::visitTryBlockStatement(TryBlockStatementAST* node)  { DefaultVisitor::visitTryBlockStatement(node); }
 
2338
  void ExpressionVisitor::visitTypeParameter(TypeParameterAST* node)  { DefaultVisitor::visitTypeParameter(node); }
 
2339
  void ExpressionVisitor::visitTypedef(TypedefAST* node)  { DefaultVisitor::visitTypedef(node); }
 
2340
  void ExpressionVisitor::visitUsing(UsingAST* node)  { DefaultVisitor::visitUsing(node); }
 
2341
  void ExpressionVisitor::visitUsingDirective(UsingDirectiveAST* node)  { DefaultVisitor::visitUsingDirective(node); }
 
2342
  void ExpressionVisitor::visitWhileStatement(WhileStatementAST* node)  { DefaultVisitor::visitWhileStatement(node); }
 
2343
  void ExpressionVisitor::visitWinDeclSpec(WinDeclSpecAST* node)  { DefaultVisitor::visitWinDeclSpec(node); }
 
2344
}
 
2345