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

« back to all changes in this revision

Viewing changes to languages/cpp/cppevaluation.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
 
   begin                : Sat Jul 21 2001
3
 
   copyright            : (C) 2001 by Victor R�er
4
 
   email                : victor_roeder@gmx.de
5
 
   copyright            : (C) 2002,2003 by Roberto Raggi
6
 
   email                : roberto@kdevelop.org
7
 
   copyright            : (C) 2005 by Adam Treat
8
 
   email                : manyoso@yahoo.com
9
 
   copyright            : (C) 2006 by David Nolden
10
 
   email                : david.nolden.kdevelop@art-master.de
11
 
***************************************************************************/
12
 
 
13
 
/***************************************************************************
14
 
 *                                                                         *
15
 
 *   This program is free software; you can redistribute it and/or modify  *
16
 
 *   it under the terms of the GNU General Public License as published by  *
17
 
 *   the Free Software Foundation; either version 2 of the License, or     *
18
 
 *   (at your option) any later version.                                   *
19
 
 *                                                                         *
20
 
 ***************************************************************************/
21
 
 
22
 
#include "cppevaluation.h"
23
 
#include "simplecontext.h"
24
 
#include "safetycounter.h"
25
 
 
26
 
extern SafetyCounter safetyCounter;
27
 
 
28
 
namespace CppEvaluation {
29
 
 
30
 
void statusLog( const QString& str ) {
31
 
  ifVerboseMajor( dbgMajor() << str << endl );
32
 
  statusBarText( str, 2000 );
33
 
}
34
 
OperatorSet AllOperators;
35
 
 
36
 
///These lines register the operators to the list of all operators
37
 
RegisterOperator< DotOperator > DotReg( AllOperators );
38
 
RegisterOperator< NestedTypeOperator > NestedReg( AllOperators );
39
 
RegisterOperator< ArrowOperator > ArrowReg( AllOperators );
40
 
RegisterOperator< StarOperator > StarReg( AllOperators );
41
 
RegisterOperator< AddressOperator > AddressReg( AllOperators );
42
 
RegisterOperator< IndexOperator > IndexReg( AllOperators );
43
 
RegisterOperator< ParenOperator > ParenReg( AllOperators );
44
 
 
45
 
template<class To, class From>  
46
 
QValueList<To> convertList( const QValueList<From>& from ) {
47
 
  QValueList<To> ret;
48
 
  for( typename QValueList<From>::const_iterator it = from.begin(); it != from.end(); ++it ) {
49
 
    ret << (To)*it;
50
 
  }
51
 
  return ret;
52
 
}
53
 
 
54
 
QString nameFromType( SimpleType t ) {
55
 
  return t->fullTypeResolved();
56
 
}
57
 
 
58
 
QString Operator::printTypeList( QValueList<EvaluationResult>& lst )
59
 
{
60
 
  QString ret;
61
 
  for( QValueList<EvaluationResult>::iterator it = lst.begin(); it != lst.end(); ++it ) {
62
 
    ret += "\"" + (*it)->fullNameChain() + "\", ";
63
 
  }
64
 
  ret.truncate( ret.length() - 3 );
65
 
  return ret;
66
 
}
67
 
 
68
 
void Operator::log( const QString& msg ) {
69
 
  statusLog( "\"" + name() + "\": " + msg );
70
 
  //ifVerboseMajor( dbgMajor() << "\"" << name() << "\": " << msg << endl );
71
 
}
72
 
 
73
 
OperatorSet::~OperatorSet() {
74
 
  for( QValueList< Operator* >::iterator it = m_operators.begin(); it != m_operators.end(); ++it ) {
75
 
    delete *it;
76
 
  }
77
 
}
78
 
 
79
 
OperatorIdentification OperatorSet::identifyOperator( const QString& str_ , Operator::BindingSide allowedBindings) {
80
 
  QString str = str_.stripWhiteSpace();
81
 
  for( OperatorList::iterator it = m_operators.begin(); it != m_operators.end(); ++it ) {
82
 
    if( ((*it)->binding() & allowedBindings) == (*it)->binding() ) {
83
 
      if( OperatorIdentification ident = (*it)->identify( str ) ) {
84
 
        return ident;
85
 
      }
86
 
    }
87
 
  }
88
 
  
89
 
  return OperatorIdentification();
90
 
}
91
 
 
92
 
OperatorIdentification UnaryOperator::identify( QString& str ) {
93
 
  OperatorIdentification ret;
94
 
  if( str.startsWith( m_identString ) ) {
95
 
    ret.start = 0;
96
 
    ret.end = m_identString.length();
97
 
    ret.found = true;
98
 
    ret.op = this;
99
 
  }
100
 
  return ret;
101
 
}
102
 
 
103
 
EvaluationResult UnaryOperator::apply( QValueList<EvaluationResult> params, QValueList<EvaluationResult> innerParams ) {
104
 
  if( !checkParams( params ) ) {
105
 
  log( QString("parameter-check failed: %1 params: ").arg( params.size() ) + printTypeList( params ) );
106
 
    return EvaluationResult();
107
 
  } else {
108
 
    EvaluationResult t = unaryApply( params.front(), innerParams );
109
 
    if( !t ) {
110
 
      if( params.front() )
111
 
        log( "could not apply \"" + name() + "\" to \"" + nameFromType( params.front() ) + "\"");
112
 
      else
113
 
        log( "operator \"" + name() + "\" applied on \"" + nameFromType( params.front() )  + "\": returning unresolved type \"" + nameFromType( t ) + "\"");
114
 
    }
115
 
    return t;
116
 
  }
117
 
}
118
 
 
119
 
EvaluationResult NestedTypeOperator::unaryApply( EvaluationResult param, const QValueList<EvaluationResult>& /*innerParams*/ ) {
120
 
  return param;
121
 
}
122
 
 
123
 
EvaluationResult DotOperator::unaryApply( EvaluationResult param, const QValueList<EvaluationResult>& /*innerParams*/ ) {
124
 
  if( param->totalPointerDepth() == 0 ) {
125
 
    return param;
126
 
  } else {
127
 
    log( "failed to apply dot-operator to " + param->fullNameChain() + " because the pointer-depth is wrong" );
128
 
    return EvaluationResult();
129
 
  }
130
 
}
131
 
 
132
 
 
133
 
EvaluationResult ArrowOperator::unaryApply( EvaluationResult param, const QValueList<EvaluationResult>& innerParams ) {
134
 
  if( param->totalPointerDepth() == 1 ) {
135
 
    param->setTotalPointerDepth( param->totalPointerDepth() - 1 );
136
 
    return param;
137
 
  } else {
138
 
    if( param->resolved() ) {
139
 
      if( param->totalPointerDepth() == 0 ) {
140
 
          return param->resolved()->applyOperator( SimpleTypeImpl::ArrowOp , convertList<LocateResult, EvaluationResult>(innerParams) );
141
 
      } else {
142
 
          log("failed to apply arrow-operator to " + param->fullNameChain() + " because the pointer-depth is wrong" );
143
 
          return EvaluationResult();
144
 
      }
145
 
    } else {
146
 
      log( "failed to apply arrow-operator to unresolved type" );
147
 
      return EvaluationResult();
148
 
    }
149
 
  };
150
 
}
151
 
 
152
 
EvaluationResult StarOperator::unaryApply( EvaluationResult param, const QValueList<EvaluationResult>& /*innerParams*/ ) {
153
 
  if( param->totalPointerDepth() > 0 ) {
154
 
    param->setTotalPointerDepth( param->totalPointerDepth() - 1 );
155
 
    return param;
156
 
  } else {
157
 
    if( param->resolved() ) {
158
 
      return param->resolved()->applyOperator( SimpleTypeImpl::StarOp );
159
 
    } else {
160
 
      log( "failed to apply star-operator to unresolved type" );
161
 
      return EvaluationResult();
162
 
    }
163
 
  };
164
 
}
165
 
 
166
 
EvaluationResult AddressOperator::unaryApply( EvaluationResult param, const QValueList<EvaluationResult>& /*innerParams*/ ) {
167
 
  param->setTotalPointerDepth( param->totalPointerDepth() + 1 );
168
 
  return param;
169
 
}
170
 
 
171
 
OperatorIdentification UnaryParenOperator::identify( QString& str ) {
172
 
  OperatorIdentification ret;
173
 
  if( str.startsWith( QString( identString()[0] ) ) ) {
174
 
    ret.start = 0;
175
 
    ret.end = findClose( str, 0 );
176
 
    if( ret.end == -1 ) {
177
 
      ret.found = false;
178
 
      ret.end = 0;
179
 
    } else {
180
 
      if( str[ret.end] == identString()[1] ) {
181
 
        ret.found = true;
182
 
        ret.end += 1;
183
 
        ret.op = this;
184
 
        
185
 
                    ///Try to extract the parameter-strings.
186
 
        ParamIterator it( identString(), str.mid( ret.start, ret.end - ret.start ) );
187
 
        
188
 
        while( it ) {
189
 
          ret.innerParams << (*it).stripWhiteSpace();
190
 
          
191
 
          ++it;
192
 
        }
193
 
        
194
 
      } else {
195
 
        ret.end = 0;
196
 
      }
197
 
    }
198
 
  }
199
 
  return ret;
200
 
}
201
 
 
202
 
 
203
 
EvaluationResult IndexOperator::unaryApply( EvaluationResult param, const QValueList<EvaluationResult>& innerParams ) {
204
 
  if( param->totalPointerDepth() > 0 ) {
205
 
    param->setTotalPointerDepth( param->totalPointerDepth() - 1 );
206
 
    return param;
207
 
  } else {
208
 
    if( param->resolved() ) {
209
 
      return param->resolved()->applyOperator( SimpleTypeImpl::IndexOp, convertList<LocateResult>( innerParams ) );
210
 
    } else {
211
 
      log( "failed to apply index-operator to unresolved type" );
212
 
      return EvaluationResult();
213
 
    }
214
 
  };
215
 
}
216
 
 
217
 
EvaluationResult ParenOperator::unaryApply( EvaluationResult param, const QValueList<EvaluationResult>& innerParams ) {
218
 
  if( param ) {
219
 
    if( param->resolved() ) {
220
 
      return param->resolved()->applyOperator( SimpleTypeImpl::ParenOp, convertList<LocateResult>(innerParams) );
221
 
    } else {
222
 
      log( "failed to apply paren-operator to unresolved type" );
223
 
      return EvaluationResult();
224
 
    }
225
 
    
226
 
  } else {
227
 
    return innerParams[0];
228
 
  }
229
 
}
230
 
 
231
 
ExpressionEvaluation::ExpressionEvaluation( CppCodeCompletion* data, ExpressionInfo expr, OperatorSet& operators, const HashedStringSet& includeFiles, SimpleContext* ctx ) : m_data( data ), m_ctx( ctx ), m_expr( expr ), m_global(false), m_operators( operators ), m_includeFiles( includeFiles ) {
232
 
  safetyCounter.init();
233
 
  
234
 
  ifVerboseMajor( dbgMajor( ) << "Initializing evaluation of expression " << expr << endl );
235
 
  
236
 
  if ( expr.expr().startsWith( "::" ) )
237
 
  {
238
 
    expr.setExpr( expr.expr().mid( 2 ) );
239
 
    m_global = true;
240
 
  }
241
 
  
242
 
          //m_expr = m_data->splitExpression( expr.expr() ).join("");
243
 
}
244
 
 
245
 
EvaluationResult ExpressionEvaluation::evaluate() {
246
 
  EvaluationResult res;
247
 
  res = evaluateExpressionInternal( m_expr.expr(), m_ctx->global(), m_ctx, m_ctx, /*m_expr.canBeTypeExpression() cannot be safely determined*/true );
248
 
  
249
 
  ExpressionInfo ex = res.expr; ///backup and set the type which was chosen while the evaluation-process
250
 
  res.expr = m_expr;
251
 
  res.expr.t = ex.t;
252
 
  
253
 
  return res;
254
 
}
255
 
 
256
 
EvaluationResult ExpressionEvaluation::evaluateExpressionInternal( QString expr, EvaluationResult scope, SimpleContext * ctx, SimpleContext* innerCtx , bool canBeTypeExpression) {
257
 
  LogDebug d( "#evl#" );
258
 
  if( expr.isEmpty() || !safetyCounter ) {
259
 
    scope.expr.t = ExpressionInfo::NormalExpression;
260
 
    return scope;
261
 
  }
262
 
  
263
 
        /*if( !scope->resolved() ) {
264
 
    ifVerboseMajor( dbgMajor() << "evaluateExpressionInternal(\"" << expr << "\") scope: \"" << scope->fullTypeStructure() << "\" is unresolved " << endl );
265
 
    return EvaluationResult();
266
 
  }*/
267
 
  
268
 
  ifVerboseMajor( dbgMajor() << "evaluateExpressionInternal(\"" << expr << "\") scope: \"" << scope->fullNameChain() << "\" context: " << ctx << endl );
269
 
  
270
 
  expr = expr.stripWhiteSpace();
271
 
  
272
 
        ///Find the rightmost operator with the lowest priority, for the first split.
273
 
  QValueList<OperatorIdentification> idents;
274
 
  for( uint a = 0; a < expr.length(); ++a ) {
275
 
    QString part = expr.mid( a );
276
 
    OperatorIdentification ident = m_operators.identifyOperator( part );
277
 
    if( ident ) {
278
 
      ifVerboseMajor( dbgMajor() << "identified \"" << ident.op->name() << "\" in string " << part << endl );
279
 
      ident.start += a;
280
 
      ident.end += a;
281
 
      idents << ident;
282
 
      a += ident.end;
283
 
    } else {
284
 
      if( isLeftParen( part[0] ) ) {
285
 
        int jump = findClose( part, 0 );
286
 
        if( jump != -1 )
287
 
          a += jump;
288
 
      }
289
 
    }
290
 
  }
291
 
  
292
 
  if( !idents.isEmpty() ) {
293
 
    OperatorIdentification lowest;
294
 
    
295
 
    for( QValueList<OperatorIdentification>::iterator it = idents.begin(); it != idents.end(); ++it ) {
296
 
      if( lowest ) {
297
 
        if( lowest.op->priority() >= (*it).op->priority() )
298
 
          lowest = *it;
299
 
      } else {
300
 
        lowest = *it;
301
 
      }
302
 
    }
303
 
    
304
 
    if( lowest ) {
305
 
      QString leftSide = expr.left( lowest.start ).stripWhiteSpace();
306
 
      QString rightSide = expr.right( expr.length() - lowest.end ).stripWhiteSpace();
307
 
      
308
 
      EvaluationResult left, right;
309
 
      if( !leftSide.isEmpty() ) {
310
 
              left = evaluateExpressionInternal( leftSide, scope, ctx, innerCtx, lowest.op->canBeType( Operator::Left ) );
311
 
      } else {
312
 
        left = scope;
313
 
      }
314
 
      
315
 
      if( !left && (lowest.op->binding() & Operator::Left) ) {
316
 
        ifVerboseMajor( dbgMajor() << "problem while evaluating expression \"" << expr << "\", the operator \"" << lowest.op->name() << "\" has a binding to the left side, but no left side could be evaluated: \"" << leftSide << "\"" << endl );
317
 
      }
318
 
      
319
 
      if( !rightSide.isEmpty() && (lowest.op->binding() & Operator::Right) )
320
 
              right = evaluateExpressionInternal( rightSide, SimpleType(), ctx, innerCtx, lowest.op->canBeType( Operator::Right ) );
321
 
      
322
 
      if( !right && (lowest.op->binding() & Operator::Right) ) {
323
 
        ifVerboseMajor( dbgMajor() << "problem while evaluating expression \"" << expr << "\", the operator \"" << lowest.op->name() << "\" has a binding to the right side, but no right side could be evaluated: \"" << rightSide << "\"" << endl );
324
 
      }
325
 
      
326
 
      QValueList<EvaluationResult> innerParams;
327
 
      QValueList<EvaluationResult> params;
328
 
      if( lowest.op->binding() & Operator::Left ) params << left;
329
 
      if( lowest.op->binding() & Operator::Right ) params << right;
330
 
      
331
 
      for( QValueList<QString>::iterator it = lowest.innerParams.begin(); it != lowest.innerParams.end(); ++it ) {
332
 
        ifVerboseMajor(dbgMajor() << "evaluating inner parameter \"" + *it + "\"" );
333
 
              innerParams << evaluateExpressionInternal( (*it), SimpleType(), innerCtx, innerCtx, lowest.op->canBeType( Operator::Neutral ) );
334
 
      }
335
 
      
336
 
      EvaluationResult applied = lowest.op->apply( params, innerParams );
337
 
      if( !applied ) {
338
 
        statusLog( "\"" + expr + "\": failed to apply the operator \"" + lowest.op->name() + "\"" );
339
 
      }
340
 
      
341
 
      if( ! (lowest.op->binding() & Operator::Left) &&  !leftSide.isEmpty() ) {
342
 
                    ///When the operator has no binding to the left, the left side should be empty.
343
 
        statusLog( "\"" + expr + "\": problem with the operator \"" + lowest.op->name() + ", it has no binding to the left side, but the left side is \""+ leftSide + "\"" );
344
 
      }
345
 
      
346
 
      if( ! (lowest.op->binding() & Operator::Right) && !rightSide.isEmpty() ) {
347
 
                                ///When the operator has no binding to the right, we should continue evaluating the right side, using the left type as scope.
348
 
              ///Think about this.
349
 
              return evaluateExpressionInternal( rightSide, applied, 0, innerCtx, lowest.op->canBeType( Operator::Right ) );
350
 
      }
351
 
      
352
 
      return applied;
353
 
    } else {
354
 
      ifVerboseMajor( dbgMajor() << " could not find an operator in " << expr << endl );
355
 
      return evaluateAtomicExpression( expr, scope, ctx );
356
 
    }
357
 
  }
358
 
  
359
 
        //dbgMajor() << " could not evaluate " << expr << endl;
360
 
  ifVerboseMajor( dbgMajor() << "evaluating \"" << expr << "\" as atomic expression" << endl );
361
 
 
362
 
  TypeDesc exp = m_ctx->container()->resolveTemplateParams( TypeDesc(expr) );
363
 
 
364
 
        ifVerboseMajor( dbgMajor() << "after template-parameter resolution: \"" << exp.fullNameChain() << "\"" << endl );
365
 
        
366
 
        EvaluationResult res = evaluateAtomicExpression( exp, scope, ctx, canBeTypeExpression );
367
 
  return res;
368
 
}
369
 
 
370
 
/**This function needs a clean workover.
371
 
 * An atomic expression is one that only consists of a type-, function- or variable-name(may include '::')
372
 
 */
373
 
EvaluationResult ExpressionEvaluation::evaluateAtomicExpression( TypeDesc expr, EvaluationResult scope, SimpleContext * ctx, bool canBeTypeExpression ) {
374
 
  LogDebug d( "#evt#");
375
 
  if( !safetyCounter || !d ) return SimpleType();
376
 
        bool canBeItemExpression = true; ///To be implemented
377
 
 
378
 
    if( scope ) {
379
 
        expr.setIncludeFiles( scope.resultType->includeFiles() );
380
 
    } else {
381
 
        expr.setIncludeFiles( m_includeFiles );
382
 
    }
383
 
 
384
 
  
385
 
  ifVerboseMajor( dbgMajor() << "evaluateAtomicExpression(\"" << expr.name() << "\") scope: \"" << scope->fullNameChain() << "\" context: " << ctx << endl );
386
 
 
387
 
        EvaluationResult bestRet;
388
 
  int bestDepth = 0;
389
 
        
390
 
  if( expr.name().isEmpty() )
391
 
    return scope;
392
 
  
393
 
  TypePointer searchIn = scope->resolved();
394
 
  if( !searchIn ) {
395
 
    statusLog( "scope-type is not resolved" );
396
 
    return EvaluationResult();
397
 
  }
398
 
 
399
 
        if( ctx )
400
 
                searchIn = ctx->container().get();
401
 
        
402
 
        if( ctx && canBeItemExpression ) {
403
 
                ///Search for variables and functions, first in the current context, and then through the container-classes upwards.
404
 
                        // find the variable in the current context
405
 
                SimpleVariable var = ctx->findVariable( expr.name() );
406
 
        
407
 
    if ( var.type ) {
408
 
        TypeDesc d( var.type );
409
 
        d.setIncludeFiles( m_includeFiles );
410
 
                        EvaluationResult ret = EvaluationResult( ctx->container()->locateDecType( d ), var.toDeclarationInfo( "current_file" ));
411
 
      ret.expr.t = ExpressionInfo::NormalExpression;
412
 
      return ret;
413
 
    }
414
 
        
415
 
                SimpleType current = ctx->container();
416
 
        
417
 
                SimpleTypeImpl::TypeOfResult type;
418
 
        
419
 
                SafetyCounter s( 20 );
420
 
                bool ready = false;
421
 
    int depth = 0; 
422
 
    
423
 
                while( !ready && s )
424
 
                {
425
 
                        if( !current ) ready = true;
426
 
        
427
 
                        type = current->typeOf( expr );
428
 
      if ( type) {
429
 
                                bestRet = EvaluationResult( type.type, type.decl );
430
 
        bestDepth = depth;
431
 
        bestRet.expr = expr.fullNameChain();
432
 
        bestRet.expr.t = ExpressionInfo::NormalExpression;
433
 
      }
434
 
 
435
 
      depth++;
436
 
                        if( !ready ) current = current->parent();
437
 
                }
438
 
        }
439
 
        /*
440
 
  if( scope.expr.t & ExpressionInfo::TypeExpression )
441
 
    canBeTypeExpression = true;*/
442
 
 
443
 
        if( canBeItemExpression && (!bestRet || bestDepth > 0 ) ) {
444
 
                //SimpleTypeImpl::
445
 
                SimpleTypeImpl::TypeOfResult res = searchIn->typeOf( expr );
446
 
    
447
 
    if( res ) {
448
 
      bestRet = EvaluationResult( res.type, res.decl );
449
 
      bestDepth = 0;
450
 
    }
451
 
  }
452
 
        if( canBeTypeExpression ) {
453
 
                ///Search for Types
454
 
                LocateResult type = searchIn->locateDecType( expr );
455
 
 
456
 
                if( !bestRet ||
457
 
                /** Did we find a constructor within a class? */
458
 
                (type->resolved() && ( bestRet->resolved() && type->resolved()->desc() == bestRet->resolved()->parent()->desc() && bestRet->resolved()->asFunction() ) ) ) {
459
 
    /*if ( type && type->resolved() )
460
 
    {*/
461
 
      EvaluationResult ret = type;
462
 
      ret.expr = expr.fullNameChain();
463
 
      ret.expr.t = ExpressionInfo::TypeExpression;
464
 
      bestRet = ret;
465
 
    }
466
 
    /*} else {
467
 
                        bestRet = EvaluationResult( type );
468
 
                        QStringList s = split+exprList;
469
 
                        s.pop_front();
470
 
                        if( !s.isEmpty() )
471
 
                                bestRet->append( new TypeDescShared( s.join("::") ) );
472
 
    }*/
473
 
        }
474
 
 
475
 
  if( bestRet )
476
 
    return bestRet;
477
 
        
478
 
  ifVerboseMajor( dbgMajor() << "evaluateAtomicExpression: \"" << scope.resultType->fullNameChain() << "\"could not locate " << expr.fullNameChain() << endl );
479
 
  return  bestRet;
480
 
}
481
 
}
482
 
 
483
 
// kate: indent-mode csands; tab-width 2;