summaryrefslogtreecommitdiffstats
path: root/languages/cpp/cppevaluation.cpp
blob: 3338b97a6001a4c5840c3f1159a2a5bf8ffd17e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/***************************************************************************
   begin                : Sat Jul 21 2001
   copyright            : (C) 2001 by Victor R�er
   email                : victor_roeder@gmx.de
   copyright            : (C) 2002,2003 by Roberto Raggi
   email                : roberto@tdevelop.org
   copyright            : (C) 2005 by Adam Treat
   email                : manyoso@yahoo.com
   copyright            : (C) 2006 by David Nolden
   email                : david.nolden.tdevelop@art-master.de
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "cppevaluation.h"
#include "simplecontext.h"
#include "safetycounter.h"

extern SafetyCounter safetyCounter;

namespace CppEvaluation {

void statusLog( const TQString& str ) {
  ifVerboseMajor( dbgMajor() << str << endl );
  statusBarText( str, 2000 );
}
OperatorSet AllOperators;

///These lines register the operators to the list of all operators
RegisterOperator< DotOperator > DotReg( AllOperators );
RegisterOperator< NestedTypeOperator > NestedReg( AllOperators );
RegisterOperator< ArrowOperator > ArrowReg( AllOperators );
RegisterOperator< StarOperator > StarReg( AllOperators );
RegisterOperator< AddressOperator > AddressReg( AllOperators );
RegisterOperator< IndexOperator > IndexReg( AllOperators );
RegisterOperator< ParenOperator > ParenReg( AllOperators );

template<class To, class From>  
TQValueList<To> convertList( const TQValueList<From>& from ) {
  TQValueList<To> ret;
  for( typename TQValueList<From>::const_iterator it = from.begin(); it != from.end(); ++it ) {
    ret << (To)*it;
  }
  return ret;
}

TQString nameFromType( SimpleType t ) {
  return t->fullTypeResolved();
}

TQString Operator::printTypeList( TQValueList<EvaluationResult>& lst )
{
  TQString ret;
  for( TQValueList<EvaluationResult>::iterator it = lst.begin(); it != lst.end(); ++it ) {
    ret += "\"" + (*it)->fullNameChain() + "\", ";
  }
  ret.truncate( ret.length() - 3 );
  return ret;
}

void Operator::log( const TQString& msg ) {
  statusLog( "\"" + name() + "\": " + msg );
  //ifVerboseMajor( dbgMajor() << "\"" << name() << "\": " << msg << endl );
}

OperatorSet::~OperatorSet() {
  for( TQValueList< Operator* >::iterator it = m_operators.begin(); it != m_operators.end(); ++it ) {
    delete *it;
  }
}

OperatorIdentification OperatorSet::identifyOperator( const TQString& str_ , Operator::BindingSide allowedBindings) {
  TQString str = str_.stripWhiteSpace();
  for( OperatorList::iterator it = m_operators.begin(); it != m_operators.end(); ++it ) {
    if( ((*it)->binding() & allowedBindings) == (*it)->binding() ) {
      if( OperatorIdentification ident = (*it)->identify( str ) ) {
        return ident;
      }
    }
  }
  
  return OperatorIdentification();
}

OperatorIdentification UnaryOperator::identify( TQString& str ) {
  OperatorIdentification ret;
  if( str.startsWith( m_identString ) ) {
    ret.start = 0;
    ret.end = m_identString.length();
    ret.found = true;
    ret.op = this;
  }
  return ret;
}

EvaluationResult UnaryOperator::apply( TQValueList<EvaluationResult> params, TQValueList<EvaluationResult> innerParams ) {
  if( !checkParams( params ) ) {
  log( TQString("parameter-check failed: %1 params: ").tqarg( params.size() ) + printTypeList( params ) );
    return EvaluationResult();
  } else {
    EvaluationResult t = unaryApply( params.front(), innerParams );
    if( !t ) {
      if( params.front() )
        log( "could not apply \"" + name() + "\" to \"" + nameFromType( params.front() ) + "\"");
      else
        log( "operator \"" + name() + "\" applied on \"" + nameFromType( params.front() )  + "\": returning unresolved type \"" + nameFromType( t ) + "\"");
    }
    return t;
  }
}

EvaluationResult NestedTypeOperator::unaryApply( EvaluationResult param, const TQValueList<EvaluationResult>& /*innerParams*/ ) {
  return param;
}

EvaluationResult DotOperator::unaryApply( EvaluationResult param, const TQValueList<EvaluationResult>& /*innerParams*/ ) {
  if( param->totalPointerDepth() == 0 ) {
    return param;
  } else {
    log( "failed to apply dot-operator to " + param->fullNameChain() + " because the pointer-depth is wrong" );
    return EvaluationResult();
  }
}


EvaluationResult ArrowOperator::unaryApply( EvaluationResult param, const TQValueList<EvaluationResult>& innerParams ) {
  if( param->totalPointerDepth() == 1 ) {
    param->setTotalPointerDepth( param->totalPointerDepth() - 1 );
    return param;
  } else {
    if( param->resolved() ) {
      if( param->totalPointerDepth() == 0 ) {
          return param->resolved()->applyOperator( SimpleTypeImpl::ArrowOp , convertList<LocateResult, EvaluationResult>(innerParams) );
      } else {
          log("failed to apply arrow-operator to " + param->fullNameChain() + " because the pointer-depth is wrong" );
          return EvaluationResult();
      }
    } else {
      log( "failed to apply arrow-operator to unresolved type" );
      return EvaluationResult();
    }
  };
}

EvaluationResult StarOperator::unaryApply( EvaluationResult param, const TQValueList<EvaluationResult>& /*innerParams*/ ) {
  if( param->totalPointerDepth() > 0 ) {
    param->setTotalPointerDepth( param->totalPointerDepth() - 1 );
    return param;
  } else {
    if( param->resolved() ) {
      return param->resolved()->applyOperator( SimpleTypeImpl::StarOp );
    } else {
      log( "failed to apply star-operator to unresolved type" );
      return EvaluationResult();
    }
  };
}

EvaluationResult AddressOperator::unaryApply( EvaluationResult param, const TQValueList<EvaluationResult>& /*innerParams*/ ) {
  param->setTotalPointerDepth( param->totalPointerDepth() + 1 );
  return param;
}

OperatorIdentification UnaryParenOperator::identify( TQString& str ) {
  OperatorIdentification ret;
  if( str.startsWith( TQString( identString()[0] ) ) ) {
    ret.start = 0;
    ret.end = findClose( str, 0 );
    if( ret.end == -1 ) {
      ret.found = false;
      ret.end = 0;
    } else {
      if( str[ret.end] == identString()[1] ) {
        ret.found = true;
        ret.end += 1;
        ret.op = this;
        
                    ///Try to extract the parameter-strings.
        ParamIterator it( identString(), str.mid( ret.start, ret.end - ret.start ) );
        
        while( it ) {
          ret.innerParams << (*it).stripWhiteSpace();
          
          ++it;
        }
        
      } else {
        ret.end = 0;
      }
    }
  }
  return ret;
}


EvaluationResult IndexOperator::unaryApply( EvaluationResult param, const TQValueList<EvaluationResult>& innerParams ) {
  if( param->totalPointerDepth() > 0 ) {
    param->setTotalPointerDepth( param->totalPointerDepth() - 1 );
    return param;
  } else {
    if( param->resolved() ) {
      return param->resolved()->applyOperator( SimpleTypeImpl::IndexOp, convertList<LocateResult>( innerParams ) );
    } else {
      log( "failed to apply index-operator to unresolved type" );
      return EvaluationResult();
    }
  };
}

EvaluationResult ParenOperator::unaryApply( EvaluationResult param, const TQValueList<EvaluationResult>& innerParams ) {
  if( param ) {
    if( param->resolved() ) {
      return param->resolved()->applyOperator( SimpleTypeImpl::ParenOp, convertList<LocateResult>(innerParams) );
    } else {
      log( "failed to apply paren-operator to unresolved type" );
      return EvaluationResult();
    }
    
  } else {
    return innerParams[0];
  }
}

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 ) {
  safetyCounter.init();
  
  ifVerboseMajor( dbgMajor( ) << "Initializing evaluation of expression " << expr << endl );
  
  if ( expr.expr().startsWith( "::" ) )
  {
    expr.setExpr( expr.expr().mid( 2 ) );
    m_global = true;
  }
  
          //m_expr = m_data->splitExpression( expr.expr() ).join("");
}

EvaluationResult ExpressionEvaluation::evaluate() {
  EvaluationResult res;
  res = evaluateExpressionInternal( m_expr.expr(), m_ctx->global(), m_ctx, m_ctx, /*m_expr.canBeTypeExpression() cannot be safely determined*/true );
  
  ExpressionInfo ex = res.expr; ///backup and set the type which was chosen while the evaluation-process
  res.expr = m_expr;
  res.expr.t = ex.t;
  
  return res;
}

EvaluationResult ExpressionEvaluation::evaluateExpressionInternal( TQString expr, EvaluationResult scope, SimpleContext * ctx, SimpleContext* innerCtx , bool canBeTypeExpression) {
  LogDebug d( "#evl#" );
  if( expr.isEmpty() || !safetyCounter ) {
    scope.expr.t = ExpressionInfo::NormalExpression;
    return scope;
  }
  
	/*if( !scope->resolved() ) {
    ifVerboseMajor( dbgMajor() << "evaluateExpressionInternal(\"" << expr << "\") scope: \"" << scope->fullTypeStructure() << "\" is unresolved " << endl );
    return EvaluationResult();
  }*/
  
  ifVerboseMajor( dbgMajor() << "evaluateExpressionInternal(\"" << expr << "\") scope: \"" << scope->fullNameChain() << "\" context: " << ctx << endl );
  
  expr = expr.stripWhiteSpace();
  
        ///Find the rightmost operator with the lowest priority, for the first split.
  TQValueList<OperatorIdentification> idents;
  for( uint a = 0; a < expr.length(); ++a ) {
    TQString part = expr.mid( a );
    OperatorIdentification ident = m_operators.identifyOperator( part );
    if( ident ) {
      ifVerboseMajor( dbgMajor() << "identified \"" << ident.op->name() << "\" in string " << part << endl );
      ident.start += a;
      ident.end += a;
      idents << ident;
      a += ident.end;
    } else {
      if( isLeftParen( part[0] ) ) {
        int jump = findClose( part, 0 );
        if( jump != -1 )
          a += jump;
      }
    }
  }
  
  if( !idents.isEmpty() ) {
    OperatorIdentification lowest;
    
    for( TQValueList<OperatorIdentification>::iterator it = idents.begin(); it != idents.end(); ++it ) {
      if( lowest ) {
        if( lowest.op->priority() >= (*it).op->priority() )
          lowest = *it;
      } else {
        lowest = *it;
      }
    }
    
    if( lowest ) {
      TQString leftSide = expr.left( lowest.start ).stripWhiteSpace();
      TQString rightSide = expr.right( expr.length() - lowest.end ).stripWhiteSpace();
      
      EvaluationResult left, right;
      if( !leftSide.isEmpty() ) {
	      left = evaluateExpressionInternal( leftSide, scope, ctx, innerCtx, lowest.op->canBeType( Operator::Left ) );
      } else {
        left = scope;
      }
      
      if( !left && (lowest.op->binding() & Operator::Left) ) {
        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 );
      }
      
      if( !rightSide.isEmpty() && (lowest.op->binding() & Operator::Right) )
	      right = evaluateExpressionInternal( rightSide, SimpleType(), ctx, innerCtx, lowest.op->canBeType( Operator::Right ) );
      
      if( !right && (lowest.op->binding() & Operator::Right) ) {
        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 );
      }
      
      TQValueList<EvaluationResult> innerParams;
      TQValueList<EvaluationResult> params;
      if( lowest.op->binding() & Operator::Left ) params << left;
      if( lowest.op->binding() & Operator::Right ) params << right;
      
      for( TQValueList<TQString>::iterator it = lowest.innerParams.begin(); it != lowest.innerParams.end(); ++it ) {
        ifVerboseMajor(dbgMajor() << "evaluating inner parameter \"" + *it + "\"" );
	      innerParams << evaluateExpressionInternal( (*it), SimpleType(), innerCtx, innerCtx, lowest.op->canBeType( Operator::Neutral ) );
      }
      
      EvaluationResult applied = lowest.op->apply( params, innerParams );
      if( !applied ) {
        statusLog( "\"" + expr + "\": failed to apply the operator \"" + lowest.op->name() + "\"" );
      }
      
      if( ! (lowest.op->binding() & Operator::Left) &&  !leftSide.isEmpty() ) {
                    ///When the operator has no binding to the left, the left side should be empty.
        statusLog( "\"" + expr + "\": problem with the operator \"" + lowest.op->name() + ", it has no binding to the left side, but the left side is \""+ leftSide + "\"" );
      }
      
      if( ! (lowest.op->binding() & Operator::Right) && !rightSide.isEmpty() ) {
				///When the operator has no binding to the right, we should continue evaluating the right side, using the left type as scope.
	      ///Think about this.
	      return evaluateExpressionInternal( rightSide, applied, 0, innerCtx, lowest.op->canBeType( Operator::Right ) );
      }
      
      return applied;
    } else {
      ifVerboseMajor( dbgMajor() << " could not find an operator in " << expr << endl );
      return evaluateAtomicExpression( expr, scope, ctx );
    }
  }
  
        //dbgMajor() << " could not evaluate " << expr << endl;
  ifVerboseMajor( dbgMajor() << "evaluating \"" << expr << "\" as atomic expression" << endl );

  TypeDesc exp = m_ctx->container()->resolveTemplateParams( TypeDesc(expr) );

	ifVerboseMajor( dbgMajor() << "after template-parameter resolution: \"" << exp.fullNameChain() << "\"" << endl );
	
	EvaluationResult res = evaluateAtomicExpression( exp, scope, ctx, canBeTypeExpression );
  return res;
}

/**This function needs a clean workover.
 * An atomic expression is one that only consists of a type-, function- or variable-name(may include '::')
 */
EvaluationResult ExpressionEvaluation::evaluateAtomicExpression( TypeDesc expr, EvaluationResult scope, SimpleContext * ctx, bool canBeTypeExpression ) {
  LogDebug d( "#evt#");
  if( !safetyCounter || !d ) return SimpleType();
	bool canBeItemExpression = true; ///To be implemented

    if( scope ) {
        expr.setIncludeFiles( scope.resultType->includeFiles() );
    } else {
        expr.setIncludeFiles( m_includeFiles );
    }
 
  
  ifVerboseMajor( dbgMajor() << "evaluateAtomicExpression(\"" << expr.name() << "\") scope: \"" << scope->fullNameChain() << "\" context: " << ctx << endl );

	EvaluationResult bestRet;
  int bestDepth = 0;
	
  if( expr.name().isEmpty() )
    return scope;
  
  TypePointer searchIn = scope->resolved();
  if( !searchIn ) {
    statusLog( "scope-type is not resolved" );
    return EvaluationResult();
  }

	if( ctx )
		searchIn = ctx->container().get();
	
	if( ctx && canBeItemExpression ) {
		///Search for variables and functions, first in the current context, and then through the container-classes upwards.
			// find the variable in the current context
		SimpleVariable var = ctx->findVariable( expr.name() );
	
    if ( var.type ) {
        TypeDesc d( var.type );
        d.setIncludeFiles( m_includeFiles );
			EvaluationResult ret = EvaluationResult( ctx->container()->locateDecType( d ), var.toDeclarationInfo( "current_file" ));
      ret.expr.t = ExpressionInfo::NormalExpression;
      return ret;
    }
	
		SimpleType current = ctx->container();
	
		SimpleTypeImpl::TypeOfResult type;
	
		SafetyCounter s( 20 );
		bool ready = false;
    int depth = 0; 
    
		while( !ready && s )
		{
			if( !current ) ready = true;
	
			type = current->typeOf( expr );
      if ( type) {
				bestRet = EvaluationResult( type.type, type.decl );
        bestDepth = depth;
        bestRet.expr = expr.fullNameChain();
        bestRet.expr.t = ExpressionInfo::NormalExpression;
      }

      depth++;
			if( !ready ) current = current->parent();
		}
	}
	/*
  if( scope.expr.t & ExpressionInfo::TypeExpression )
    canBeTypeExpression = true;*/

	if( canBeItemExpression && (!bestRet || bestDepth > 0 ) ) {
		//SimpleTypeImpl::
		SimpleTypeImpl::TypeOfResult res = searchIn->typeOf( expr );
    
    if( res ) {
      bestRet = EvaluationResult( res.type, res.decl );
      bestDepth = 0;
    }
  }
	if( canBeTypeExpression ) {
		///Search for Types
		LocateResult type = searchIn->locateDecType( expr );

		if( !bestRet ||
		/** Did we find a constructor within a class? */
		(type->resolved() && ( bestRet->resolved() && type->resolved()->desc() == bestRet->resolved()->parent()->desc() && bestRet->resolved()->asFunction() ) ) ) {
    /*if ( type && type->resolved() )
    {*/
      EvaluationResult ret = type;
      ret.expr = expr.fullNameChain();
      ret.expr.t = ExpressionInfo::TypeExpression;
      bestRet = ret;
    }
    /*} else {
			bestRet = EvaluationResult( type );
			TQStringList s = split+exprList;
			s.pop_front();
			if( !s.isEmpty() )
				bestRet->append( new TypeDescShared( s.join("::") ) );
    }*/
	}

  if( bestRet )
    return bestRet;
	
  ifVerboseMajor( dbgMajor() << "evaluateAtomicExpression: \"" << scope.resultType->fullNameChain() << "\"could not locate " << expr.fullNameChain() << endl );
  return  bestRet;
}
}

// kate: indent-mode csands; tab-width 2;