summaryrefslogtreecommitdiffstats
path: root/languages/cpp/simpletype.cpp
blob: db3f9c79ef1bb6d09a842878d44d9e7675a45f2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
/***************************************************************************
  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 "simpletype.h"
#include "safetycounter.h"
#include "simpletypefunction.h"
#include <tdelocale.h>

TQMap<TQString, TQString> BuiltinTypes::m_types;
BuiltinTypes builtin; //Needed so BuiltinTypes::BuiltinTypes is called and the types are initialized

BuiltinTypes::BuiltinTypes() {
  m_types[ "void" ] = i18n( "typeless" );
  m_types[ "bool" ] = i18n("boolean value, 1 byte, ( \"true\" or \"false\" )");
  m_types["char" ] = i18n("signed/unsigned character, 1 byte");
  m_types["signed char" ] =  i18n("signed character, 1 byte, ranged -128 to 127");
  m_types["unsigned char"] = i18n("unsigned character, 1 byte, ranged 0 to 255");
  m_types["wchar_t"] = i18n("wide character, 2 bytes, ranged 0 to 65.535");
  m_types["long"] = m_types["long int"] = m_types["int"] = m_types["signed int"] = i18n("signed integer, 4 bytes, ranged -2.147.483.648 to 2.147.483.647");
  m_types["unsigned"] = m_types["unsigned int"] = i18n("unsigned integer, 4 bytes, ranged 0 to 4.294.967.295");
  m_types["short"] = m_types["short int"] = i18n("short integer, 2 bytes, ranged -32.768 to 32.768");
  m_types["unsigned short int"] = i18n("unsigned short integer, 2 bytes, ranged 0 to 65.535");
  m_types["float"] = i18n("floating point value, 4 bytes, ranged ca. -3,4E+38 to 3,4E+38");
  m_types["double"] = i18n("double floating point value, 8 bytes, ranged ca. -1,8E+308 to 1,8E+308");
  m_types["long double"] = i18n("double long floating point value, 10 bytes, ranged ca. -3,4E+4932 to 3,4E+4932");
  m_types["size_t"] = i18n("unsigned integer, byte-count dependent on operating-system" );
   
}

bool BuiltinTypes::isBuiltin( const TypeDesc& desc ) {
  return m_types.find( desc.name() ) != m_types.end();
}

TQString BuiltinTypes::comment( const TypeDesc& desc ) {
  TQMap<TQString, TQString>::iterator it = m_types.find( desc.name() );
  if( it != m_types.end() ) {
    return *it;
  } else {
    return TQString();
  }
}

extern SafetyCounter safetyCounter;

TypePointer SimpleType::m_globalNamespace;
SimpleType::TypeStore SimpleType::m_typeStore;
SimpleType::TypeStore SimpleType::m_destroyedStore;
TQString globalCurrentFile = "";

//SimpleType implementation

void SimpleType::resolve( Repository rep ) const {
  if ( !m_resolved ) {
    if ( m_globalNamespace ) {
      if ( ( rep == RepoUndefined || rep == RepoBoth ) ) {
        m_resolved = true;
        if ( scope().isEmpty() || str().isEmpty() ) {
          m_type = m_globalNamespace;
          return ;
        } else {
            TypeDesc d( scope().join( "::" ) );
            d.setIncludeFiles( m_includeFiles );
          LocateResult t = m_globalNamespace->locateDecType( d );
          if ( t && t->resolved() ) {
            m_type = t->resolved();
            return ;
          } else {
            ifVerbose( dbg() << "\"" << scope().join( "::" ) << "\": The type could not be located in the global scope while resolving it" << endl );
          }
        }
      }
    } else {
      ifVerbose( dbg() << "warning: no global namespace defined! " << endl );
    }

    TypePointer cm;

    if ( rep == RepoUndefined || rep == RepoCodeModel ) {
      if ( !m_type ) {
        cm = TypePointer( new SimpleTypeCachedCodeModel( scope() ) );
      } else {
        cm = TypePointer( new SimpleTypeCachedCodeModel( &( *m_type ) ) );
      }

      if ( cm->hasNode() || rep == RepoCodeModel ) {
        if ( cm->hasNode() ) {
          ifVerbose( dbg() << "resolved \"" << str() << "\" from the code-model" << endl );
          if ( cm->isNamespace() && rep != RepoCodeModel ) {
            ifVerbose( dbg() << "\"" << str() << "\": is namespace, resolving proxy" << endl );
            resolve( RepoBoth );
            return ;
          }
        } else {
          ifVerbose( dbg() << "forced \"" << str() << "\" to be resolved from code-model" << endl );
        }
        m_type = cm;
        m_resolved = true;
        return ;
      }
    }
    if ( rep == RepoUndefined || rep == RepoCatalog ) {

      if ( !m_type ) {
        cm = TypePointer( new SimpleTypeCachedCatalog( scope() ) );
      } else {
        cm = TypePointer( new SimpleTypeCachedCatalog( &( *m_type ) ) );
      }

      if ( cm->hasNode() || rep == RepoCatalog ) {
        if ( cm->hasNode() ) {
          ifVerbose( dbg() << "resolved \"" << str() << "\" from the catalog" << endl );
          if ( cm->isNamespace() && rep != RepoCatalog ) {
            ifVerbose( dbg() << "\"" << str() << "\": is namespace, resolving proxy" << endl );
            resolve( RepoBoth );
            return ;
          }
        } else {
          ifVerbose( dbg() << "forced \"" << str() << "\" to be resolved from catalog" << endl );
        }
        m_type = cm;
        m_resolved = true;
        return ;
      }
    }

    if ( rep == RepoBoth ) {
      cm = new SimpleTypeCachedNamespace( scope() );
      m_type = cm;
      m_resolved = true;
      return ;
    }

    m_resolved = true;
    ifVerbose( dbg() << "could not resolve \"" << m_type->desc().fullNameChain() << "\"" << endl );
  }
}

void SimpleType::destroyStore() {
  resetGlobalNamespace();
  int cnt = m_typeStore.size();
  kdDebug( 9007 ) << cnt << "types in type-store before destruction" << endl;

  SafetyCounter s( 30000 );
  while ( !m_typeStore.empty() && s ) {
    TypeStore::iterator it = m_typeStore.begin();
    TypePointer tp = *it;
    m_destroyedStore.insert( tp );
    m_typeStore.erase( it );
    tp->breakReferences();
  }

  if ( !m_destroyedStore.empty() ) {
    kdDebug( 9007 ) << "type-store is not empty, " << m_destroyedStore.size() << " types are left over" << endl;
    for ( TypeStore::iterator it = m_destroyedStore.begin(); it != m_destroyedStore.end(); ++it ) {
      kdDebug( 9007 ) << "type left: " << ( *it ) ->describe() << endl;
    }
  }

  ///move them over so they will be cleared again next time, hoping that they will vanish
  m_typeStore = m_destroyedStore;
  m_destroyedStore.clear();
}

///This does not necessarily make the TypeDesc's private, so before editing them
///their makePrivate must be called too
void SimpleType::makePrivate() {
  m_type = m_type->clone();
}

const TQStringList& SimpleType::scope() const {
  return m_type -> scope();
}

const TQString SimpleType::str() const {
  return m_type -> str();
}

void SimpleType::init( const TQStringList& scope, const HashedStringSet& files, Repository rep ) {
    m_includeFiles = files;

  m_type = TypePointer( new SimpleTypeImpl( scope ) );
  if ( rep != RepoUndefined )
    resolve( rep );
}

SimpleType::SimpleType( ItemDom item ) : m_resolved( true ) {
  m_type = TypePointer( new SimpleTypeCachedCodeModel( item ) );
}
/*
SimpleType::SimpleType( Tag tag ) : m_resolved(true) {
 m_type = TypePointer( new SimpleTypeCatalog( tag ) );
}*/
//
//SimpleTypeImpl implementation

TQValueList<LocateResult> SimpleTypeImpl::getBases() {
TQValueList<LocateResult> ret;
  TQStringList bases = getBaseStrings();
  for( TQStringList::const_iterator it = bases.begin(); it != bases.end(); ++it ) {
    TypeDesc d( *it );
    d.setIncludeFiles( m_findIncludeFiles );
    LocateResult res = locateDecType( d, LocateBase );
    //if( res )
      ret << res;
  }
  return ret;
}

void SimpleTypeImpl::setFindIncludeFiles( const IncludeFiles& files ) {
  m_findIncludeFiles = files;
}

IncludeFiles SimpleTypeImpl::getFindIncludeFiles() {
  return m_findIncludeFiles;
}

/**
Searches for a member called "name", considering all types selected through "typ"*/
SimpleTypeImpl::TypeOfResult SimpleTypeImpl::typeOf( const TypeDesc& name, MemberInfo::MemberType typ ) {
  Debug d( "#to#" );
  if ( !d ) {
    ifVerbose( dbg() << "stopping typeOf-evaluation because the recursion-depth is too high" << endl );
    return TypeOfResult( LocateResult( TypeDesc( "CompletionError::too_much_recursion" ) ) );
  }
  ifVerbose( dbg() << "\"" << str() << "\"------------>: searching for type of member \"" << name.fullNameChain() << "\"" << endl );

  TypeDesc td = resolveTemplateParams( name );

  MemberInfo mem = findMember( td, typ );

  if ( mem ) {
    mem.type = resolveTemplateParams( mem.type );

    ifVerbose( dbg() << "\"" << str() << "\": found member " << name.fullNameChain() << ", type: " << mem.type->fullNameChain() << endl );
    if ( mem.memberType == MemberInfo::Function ) {
      ///For functions, find all functions with the same name, so that overloaded functions can be identified correctly
      TypePointer ret = mem.build();
      if ( ret && ret->asFunction() ) {
        return TypeOfResult( LocateResult( ret->desc() ) );
      } else {
        ifVerbose( dbg() << "error, using old function-type-evaluation" << endl );

        TypeDesc d( mem.type );
        if( m_findIncludeFiles.size() != 0 )
          d.setIncludeFiles( m_findIncludeFiles );
        else
          d.setIncludeFiles( name.includeFiles() );

        return TypeOfResult( locateDecType( d ), mem.decl );
      }
    } else if ( mem.memberType == MemberInfo::Variable ) {
        TypeDesc d( mem.type );
        if( m_findIncludeFiles.size() != 0 )
          d.setIncludeFiles( m_findIncludeFiles );
        else
          d.setIncludeFiles( name.includeFiles() );
      
      return TypeOfResult( locateDecType( d ), mem.decl );
    } else {
      ifVerbose( dbg() << "while searching for the type of \"" << name.fullNameChain() << "\" in \"" << str() << "\": member has wrong type: \"" << mem.memberTypeToString() << "\"" << endl );
      return TypeOfResult();
    }
  }

  TypeOfResult ret = searchBases( td );
  if ( !ret ) {
    ifVerbose( dbg() << "\"" << str() << "\"------------>: failed to resolve the type of member \"" << name.fullNameChain() << "\"" << endl );
  } else {
    ifVerbose( dbg() << "\"" << str() << "\"------------>: successfully resolved the type of the member \"" << name.fullNameChain() << "\"" << endl );
  }
  return ret;
}

SimpleTypeFunctionInterface* SimpleTypeImpl::asFunction() {
  return dynamic_cast<SimpleTypeFunctionInterface*> ( this );
}

TQString SimpleTypeImpl::operatorToString( Operator op ) {
  switch ( op ) {
    case NoOp:
    return "NoOp";
    case IndexOp:
    return "index-operator";
    case ArrowOp:
    return "arrow-operator";
    case StarOp:
    return "star-operator";
    case AddrOp:
    return "address-operator";
    case ParenOp:
    return "paren-operator";
    default:
    return TQString( "%1" ).arg( ( long ) op );
  };
}

LocateResult SimpleTypeImpl::getFunctionReturnType( TQString functionName, TQValueList<LocateResult> params ) {
  LocateResult t = typeOf( functionName, MemberInfo::Function ).type;
  if ( t->resolved() && t->resolved() ->asFunction() ) {
    return t->resolved() ->applyOperator( ParenOp, params );
  } else {
    ifVerbose( dbg() << "error : could not find function \"" << functionName << "\" in \"" << str() << "\"" << endl );
    return LocateResult();
  }
}

LocateResult SimpleTypeImpl::applyOperator( Operator op , TQValueList<LocateResult> params ) {
  Debug d( "#applyn#" );
  if ( !d || !safetyCounter )
    return LocateResult();

  ifVerbose( dbg() << "applying operator " << operatorToString( op ) << " to \"" << desc().fullNameChain() << "\"" << endl );
  LocateResult ret;
  if ( op == NoOp )
    return LocateResult( desc() );

  switch ( op ) {
    case IndexOp:
    return getFunctionReturnType( "operator [ ]", params );
    break;
    case StarOp:
    return getFunctionReturnType( "operator *", params );
    break;
    case ArrowOp:
    /** Dereference one more because the type must be a pointer */
    ret = getFunctionReturnType( "operator ->", params );
    if ( ret->totalPointerDepth() ) {
      ret->setTotalPointerDepth( ret->totalPointerDepth() - 1 );
    } else {
      ifVerbose( dbg() << "\"" << str() << "\": " << " \"operator ->\" returns a type with the wrong pointer-depth" << endl );
    }
    return ret;
    break;
    case ParenOp:
    /** Dereference one more because the type must be a pointer */
    return getFunctionReturnType( "operator ( )", params );
    default:
    ifVerbose( dbg() << "wrong operator\n" );
  }

  return LocateResult();
}

TypeDesc SimpleTypeImpl::replaceTemplateParams( TypeDesc desc, TemplateParamInfo& paramInfo ) {
  Debug d( "#repl#" );
  if ( !d || !safetyCounter )
    return desc;

  TypeDesc ret = desc;
  if ( !ret.hasTemplateParams() && !ret.next() ) {
    TemplateParamInfo::TemplateParam t;
    if ( paramInfo.getParam( t, desc.name() ) ) {

      if ( t.value )
        ret = t.value;
      else if ( t.def )
        ret = t.def;

      if ( ret.name() != desc.name() )
        ret.setTotalPointerDepth( ret.totalPointerDepth() + desc.totalPointerDepth() );
    }
  } else {
    TypeDesc::TemplateParams& params = ret.templateParams();
    for ( TypeDesc::TemplateParams::iterator it = params.begin(); it != params.end(); ++it ) {
      *it = new TypeDescShared( replaceTemplateParams( **it, paramInfo ) );
    }
  }

  if ( ret.next() ) {
    ret.setNext( new TypeDescShared( replaceTemplateParams( *ret.next(), paramInfo ) ) );
  }

  return ret;
}

TypeDesc SimpleTypeImpl::resolveTemplateParams( LocateResult desc, LocateMode mode ) {
  Debug d( "#resd#" );
  if ( !d || !safetyCounter )
    return desc;

  LocateResult ret = desc;
  if ( ret->hasTemplateParams() ) {
    TypeDesc::TemplateParams & params = ret->templateParams();
    for ( TypeDesc::TemplateParams::iterator it = params.begin(); it != params.end(); ++it ) {
      if ( !( *it ) ->resolved() && !( *it ) ->hasFlag( ResolutionTried ) ) {
        TypeDesc d( **it );
        if( d.includeFiles().size() == 0 )
          d.setIncludeFiles( this->getFindIncludeFiles() );
        *it = locateDecType( d, mode );
        ( *it ) ->setFlag( ResolutionTried );
      }
    }
  }

  if ( ret->next() ) {
    ret->setNext( new TypeDescShared( resolveTemplateParams( *ret->next(), mode ) ) );
  }

  return ret;
}

class TemplateParamMatch {
  public:
	TemplateParamMatch() : m_matched( false ), m_maxDepth( 0 ), m_candidate( 0 ) {}

	TemplateParamMatch( TypePointer candidate, const TypeDesc& params ) : m_matched( false ), m_maxDepth( 0 ), m_candidate( candidate ) {
	    m_candidateParams = candidate->getTemplateParamInfo();
	    TypeDesc specialization( candidate->specialization() );

	    TypeDesc  cleanParams = params;
	    cleanParams.setName( "" );
	    
	    m_matched = matchParameters( specialization, cleanParams );

		if( m_matched ) {
			//Make sure that all template-parameters were found
			for( int a = 0; a < m_candidateParams.count(); a++ ) {
				SimpleTypeImpl::TemplateParamInfo::TemplateParam t;
				if( m_candidateParams.getParam( t, a ) ) {
					if( !m_hadParameters.contains( t.name ) ) {
						m_matched = false;
					}
				} else {
					m_matched = false;
				}
			}
		}
    }

	///@todo: use all default-parameters if some are missing
	///@todo: also use decoration like "const" or "&" for specialization.
	bool matchParameters( const TypeDesc& specialization, const LocateResult& params, int depth = 0 ) {
		if( depth > m_maxDepth ) m_maxDepth = depth;

		if( specialization.name().isEmpty() ) {
			if( specialization.templateParams().count() != params->templateParams().count() )
				return false;
		} else {
				SimpleTypeImpl::TemplateParamInfo::TemplateParam t;
				if( m_candidateParams.getParam( t, specialization.name() ) ) {
					TypeDesc oldValue = t.value;

					//Check if the decoration of the specialization matches the decoration of the arguments, if not we have a mismatch.

					if( specialization.totalPointerDepth() > params->totalPointerDepth() ) {
						return false; //The decoration does not match the given argument
					} else {
						depth += specialization.totalPointerDepth();
						if( depth > m_maxDepth ) m_maxDepth = depth;
					}
					
					//Fill the template-parameter, or compare if the one we already found out matches this one
					LocateResult val;
					if( specialization.hasTemplateParams() ) {
						val = params->decoratedName();
					} else {
						val = params;  //No more parameters have to be checked, so take the value and return later
					}

					val->setTotalPointerDepth( val->totalPointerDepth() - specialization.totalPointerDepth() );
					
					t.value = val;
					
					if( m_hadParameters.contains( t.name ) && oldValue != t.value ) {
						return false; ///We have a mismatch, two different values for the same template-parameter.
					} else {
						m_candidateParams.addParam( t );
						m_hadParameters[ t.name ] = val;
						if( !specialization.hasTemplateParams() ) return true;
					}
				} else {
					if( m_candidate->locateDecType( specialization.decoratedName() )->decoratedName() != params->decoratedName() ) {
					//We have a mismatch
					return false;
					}
				}
		}
			

		if( specialization.templateParams().count() != params->templateParams().count() ) {
			return false; //mismatch in count of template-parameters
		}
		
		TypeDesc::TemplateParams::const_iterator specialIt = specialization.templateParams().begin();
		TypeDesc::TemplateParams::const_iterator paramsIt = params->templateParams().begin();
		
		while( specialIt != specialization.templateParams().end() && paramsIt != params->templateParams().end() ) {
			if( !matchParameters( (*specialIt).desc(), (*paramsIt), depth+10 ) ) return false;
			
			++paramsIt;
			++specialIt;
		}
		return true;
	}

    operator bool() const {
      return m_matched;
    }

    ///True if this match is better than the given one
    bool operator > ( const TemplateParamMatch& rhs ) const {
	    if( !m_matched ) return false;
	    if(!rhs.m_matched ) return true;
	    return m_maxDepth > rhs.m_maxDepth;
    }

    TypePointer type() {
	    if( m_candidate ) {
		    TypePointer ret = m_candidate->clone();
		    ret->descForEdit().templateParams().clear();
		    for( int a = 0; a < m_candidateParams.count(); a++ ) {
			    SimpleTypeImpl::TemplateParamInfo::TemplateParam tp;
			    if( m_candidateParams.getParam( tp, a ) ) {
				    ret->descForEdit().templateParams().push_back( m_hadParameters[tp.name] );
			    } else {
				    ret->descForEdit().templateParams().push_back( LocateResult() ); //error
			    }
		    }
		    return ret;
	    } else {
		    return 0;
	    }
    }

	SimpleTypeImpl::TemplateParamInfo& templateParams() {
		return m_candidateParams;
	}
	
  private:
	TypePointer m_candidate;
    SimpleTypeImpl::TemplateParamInfo m_candidateParams;
	TQMap<TQString, LocateResult> m_hadParameters;
    bool m_matched;
	int m_maxDepth;
};

void SimpleTypeImpl::chooseSpecialization( MemberInfo& member ) {

  if ( member.memberType != MemberInfo::NestedType )
    return ;
  if ( !member.type->hasTemplateParams() )
    return ;

  TypePointer type = member.build();

  if ( !type )
    return ;

  //Get a list of all candidate-classes
  TypePointer t = this;
  if ( m_masterProxy )
    t = m_masterProxy;

  TQValueList<TypePointer> classes = t->getMemberClasses( type->desc() );

  //Find the specialization that fits the given template-parameters the best

  if ( !type->specialization().isEmpty() ) {
    kdDebug( 9007 ) << "a specialized template-class was suggested as primary class while searching for specialization, search problematic" << endl;
    //return;
  } else {
    TemplateParamInfo params = type->getTemplateParamInfo();

	int dif = params.count() - member.type->templateParams().count();

    if ( dif > 0 ) {
      //fill up missing template-parameters with their default-parameters, maybe should be done in findMember
      for ( int a = member.type->templateParams().count(); a < params.count(); a++ ) {
        LocateResult r;
        TemplateParamInfo::TemplateParam tp;
        if ( params.getParam( tp, a ) ) {
          r = t->locateDecType( tp.value );
        }
		member.type->templateParams().push_back( r );
      }
    }
  }

  //now find the class that is most specialized and matches the template-parameters

  TemplateParamMatch bestMatch;

  for ( TQValueList<TypePointer>::iterator it = classes.begin(); it != classes.end(); ++it ) {
    if ( ( *it ) ->specialization().isEmpty() )
      continue;
    TemplateParamMatch match( ( *it ), member.type.desc() );

    if ( match > bestMatch )
      bestMatch = match;
  }

  if ( bestMatch ) {
    TypePointer tp = bestMatch.type();
	if ( tp ) {
      member.setBuilt( tp );
	}
  }
}


LocateResult SimpleTypeImpl::locateType( TypeDesc name , LocateMode mode , int dir , MemberInfo::MemberType typeMask ) {
  Debug d( "#lo#" );
  if( BuiltinTypes::isBuiltin( name ) )
    return name;

  if ( !name || !safetyCounter || !d ) {
    return desc();
  }
  if ( !d ) {
    ifVerbose( dbg() << "stopping location because the recursion-depth is too high" << endl );
    return TypeDesc( "CompletionError::too_much_recursion" );
  }
    ifVerbose( dbg() << "\(" << uint(this) << ")\"" << str() << "\": locating type \"" << name.fullNameChain() << "\"" << endl );
  if ( name.resolved() && !name.next() ) {
    ifVerbose( dbg() << "\"" << desc().fullName() << "\": type \"" << name.fullNameChain() << "\" is already resolved, returning stored instance" << endl );
    return name;
  }
  /*
     if( name.resolved() && name.length() == name.resolved()->desc().length() ) {
     ifVerbose( dbg() << "\"" << desc().fullName() << "\": type \"" << name.fullNameChain() << "\" is already resolved, returning stored instance" << endl;
       SimpleType ret = SimpleType( name.resolved() );
       
       if( ! (name == ret->desc()) ) {
         ret.makePrivate();  ///Maybe some small parameters like the pointer-depth were changed, so customize those
         ret->parseParams( name );
       }
       
       return ret;
     }*/
  /*
  //This optimization is now disabled, because it allows following a wrong path.
  if( name.next() ) {
    //This is an optimization for better use of the cache: Find the elements separately, so searches
    //For elements that start with the same scope will be speeded up.
    LocateResult r = locateType( name.firstType(), mode, dir, typeMask );
    if( r && r->resolved() && r.locateMode().valid ) {
      ifVerbose( dbg() << "splitting location" );
      TypeDesc d( *name.next() );
      d.setIncludeFiles( name.includeFiles() );
      return r->resolved()->locateType( d, (LocateMode)r.locateMode().mode, r.locateMode().dir );
    }
   }*/

  LocateResult ret = name; ///In case the type cannot be located, this helps to find at least the best match
  //LocateResult ret;

  TypeDesc first = resolveTemplateParams( name.firstType(), mode );

  MemberInfo mem = findMember( first, typeMask );

  switch ( mem.memberType ) {
    case MemberInfo::Namespace:
    if ( mode & ExcludeNamespaces )
      break;
    case MemberInfo::NestedType: {
      if ( mem.memberType == MemberInfo::NestedType && mode & ExcludeNestedTypes )
        break;

      SimpleType sub;
      if ( TypePointer t = mem.build() ) {
        sub = SimpleType( t );
#ifdef PHYSICAL_IMPORT
        setSlaveParent( *sub );
#endif
      } else {
        ///Should not happen..
        kdDebug( 9007 ) << "\"" << str() << "\": Warning: the nested-type " << name.name() << " was found, but has no build-info" << endl;
        return TypeDesc( "CompletionError::unknown" );
      }

      TypeDesc rest;
      LocateMode newMode = addFlag( mode, ExcludeTemplates );
      int newDir = 1;
      if ( name.next() ) {
        ifVerbose( dbg() << "\"" << str() << "\": found nested type \"" << name.name() << "\", passing control to it\n" );
        ret = sub->locateType( resolveTemplateParams( *name.next(), Normal ), newMode, newDir ); ///since template-names cannot be referenced from outside, exclude them for the first cycle
        ret.increaseResolutionCount();
        if ( ret->resolved() )
          return ret.resetDepth();
      } else {
        ifVerbose( dbg() << "\"" << str() << "\": successfully located searched type \"" << name.fullNameChain() << "\"\n" );
        ret->setResolved( sub.get() );
        ret->resolved()->setFindIncludeFiles( name.includeFiles() );
        ret.locateMode().valid = true;
        ret.locateMode().mode = (uint)newMode;
        ret.locateMode().dir = newDir;
        return ret.resetDepth();
      }
      break;
    }
    case MemberInfo::Typedef:
    if ( mode & ExcludeTypedefs )
      break;
    case MemberInfo::Template: {
      if ( mem.memberType == MemberInfo::Template && ( mode & ExcludeTemplates ) )
        break;
      ifVerbose( dbg() << "\"" << str() << "\": found " << mem.memberTypeToString() << " \"" << name.name() << "\" -> \"" << mem.type->fullNameChain() << "\", recursing \n" );
      if ( name.hasTemplateParams() ) {
        ifVerbose( dbg() << "\"" << str() << "\":warning: \"" << name.fullName() << "\" is a " << mem.memberTypeToString() << ", but it has template-params itself! Not matching" << endl );
      } else {
        if ( mem.type->name() != name.name() ) {

          MemberInfo m = mem;
          if ( name.next() ) {
            mem.type->makePrivate();
            mem.type->append( name.next() );
          }
          ret = locateDecType( mem.type, remFlag( mode, ExcludeTemplates ) );

          if ( mem.memberType == MemberInfo::Template )
            ret.addResolutionFlag( HadTemplate );
          if ( mem.memberType == MemberInfo::Typedef )
            ret.addResolutionFlag( HadTypedef );
          ret.increaseResolutionCount();
          // 	          if( mode & TraceAliases && ret->resolved() )
          {
            m.name = "";

            if ( !scope().isEmpty() ) {
              m.name = fullTypeUnresolvedWithScope() + "::";
            }
            m.name += name.nameWithParams();
            //m.name += name.fullNameChain();

            if ( name.next() ) {
              if ( m.type.trace() ) {
                ret.trace() ->prepend( *m.type.trace(), 1 );
              }
              ret.trace() ->prepend( m, *name.next() );
            } else {
              if ( m.type.trace() )
                ret.trace() ->prepend( *m.type.trace(), 1 );
              ret.trace() ->prepend( m );
            }
          }

          if ( ret->resolved() )
            return ret.resetDepth();
        } else {
          ifVerbose( dbg() << "\"" << str() << "\"recursive typedef/template found: \"" << name.fullNameChain() << "\" -> \"" << mem.type->fullNameChain() << "\"" << endl );
        }
      }
      break;
    }
    ///A Function is treated similar to a type
    case MemberInfo::Function: {
      if ( !name.next() ) {
        TypePointer t = mem.build();
        if ( t ) {
          return t->desc();
        } else {
          ifVerbose( dbg() << "\"" << str() << "\"" << ": could not build function: \"" << name.fullNameChain() << "\"" );
        }
      } else {
        ifVerbose( dbg() << "\"" << str() << "\"" << ": name-conflict: searched for \"" << name.fullNameChain() << "\" and found function \"" << mem.name << "\"" );
      }
      break;
    };
    ///Currently there is no representation of a Variable as a SimpleType, so only the type of the variable is used.
    case MemberInfo::Variable: {
      return locateDecType( mem.type, remFlag( mode, ExcludeTemplates ) ).resetDepth();
    }
  }

  ///Ask bases but only on this level
  if ( ! ( mode & ExcludeBases ) ) {

    TQValueList<LocateResult> bases = getBases();
    if ( !bases.isEmpty() ) {
      TypeDesc nameInBase = resolveTemplateParams( name, LocateBase ); ///Resolve all template-params that are at least visible in the scope of the base-declaration

      for ( TQValueList<LocateResult>::iterator it = bases.begin(); it != bases.end(); ++it ) {
        if ( !( *it ) ->resolved() )
          continue;
        LocateResult t = ( *it ) ->resolved() ->locateType( nameInBase, addFlag( addFlag( mode, ExcludeTemplates ), ExcludeParents ), dir ); ///The searched Type cannot directly be a template-param in the base-class, so ExcludeTemplates. It's forgotten early enough.
        if ( t->resolved() )
          return t.increaseDepth();
        else
          if ( t > ret )
            ret = t.increaseDepth();
      }
    }
  }

  ///Ask parentsc
  if ( !scope().isEmpty() && dir != 1 && ! ( mode & ExcludeParents ) ) {
    LocateResult rett = parent() ->locateType( resolveTemplateParams( name, mode & ExcludeBases ? ExcludeBases : mode ), mode & ForgetModeUpwards ? Normal : mode );
    if ( rett->resolved() )
      return rett.increaseDepth();
    else
      if ( rett > ret )
        ret = rett.increaseDepth();
  }

  ///Ask the bases and allow them to search in their parents.
  if ( ! ( mode & ExcludeBases ) ) {
    TypeDesc baseName = resolveTemplateParams( name, LocateBase ); ///Resolve all template-params that are at least visible in the scope of the base-declaration
    TQValueList<LocateResult> bases = getBases();
    if ( !bases.isEmpty() ) {
      for ( TQValueList<LocateResult>::iterator it = bases.begin(); it != bases.end(); ++it ) {
        if ( !( *it ) ->resolved() )
          continue;
        LocateResult t = ( *it ) ->resolved() ->locateType( baseName, addFlag( mode, ExcludeTemplates ), dir ); ///The searched Type cannot directly be a template-param in the base-class, so ExcludeTemplates. It's forgotten early enough.
        if ( t->resolved() )
          return t.increaseDepth();
        else
          if ( t > ret )
            ret = t.increaseDepth();
      }
    }
  }

  ///Give the type a desc, so the nearest point to the searched type is stored
  ifVerbose( dbg() << "\"" << str() << "\": search for \"" << name.fullNameChain() << "\" FAILED" << endl );
  return ret;
}

void SimpleTypeImpl::breakReferences() {
  TypePointer p( this ); ///necessary so this type is not deleted in between
  m_parent = 0;
  m_desc.resetResolved();
  //	m_trace.clear();
  m_masterProxy = 0;
  invalidateCache();
}

TypePointer SimpleTypeImpl::bigContainer() {
  if ( m_masterProxy )
    return m_masterProxy;
  else
    return TypePointer( this );
}

SimpleType SimpleTypeImpl::parent() {
  if ( m_parent ) {
    //ifVerbose( dbg() << "\"" << str() << "\": returning parent" << endl;
    return SimpleType( m_parent );
  } else {
    ifVerbose( dbg() << "\"" << str() << "\": locating parent" << endl );
    invalidateSecondaryCache();
    TQStringList sc = scope();

    if ( !sc.isEmpty() ) {
      sc.pop_back();
      SimpleType r = SimpleType( sc, m_desc.includeFiles() );
      if ( &( *r.get() ) == this ) {
        kdDebug( 9007 ) << "error: self set as parent: " << m_scope.join( "::" ) << "(" << m_scope.count() << ")" << ", " << sc.join( "::" ) << "(" << sc.count() << ")" /* << kdBacktrace()*/ << endl;
        return SimpleType( new SimpleTypeImpl( "" ) );
      }
      m_parent = r.get();
      return r;
    } else {
      ifVerbose( dbg() << "\"" << str() << "\"warning: returning parent of global scope!" << endl );
      return SimpleType( new SimpleTypeImpl( "" ) );
    }
  }
}

const TypeDesc& SimpleTypeImpl::desc() {
  if ( m_desc.name().isEmpty() )
    m_desc.setName( cutTemplateParams( scope().back() ) );
  m_desc.setResolved( this );
  return m_desc;
}

TypeDesc& SimpleTypeImpl::descForEdit() {
  desc();
  invalidateCache();
  return m_desc;
}

TQString SimpleTypeImpl::describeWithParams() {
  TemplateParamInfo pinfo = getTemplateParamInfo();
  int num = 0;
  TemplateParamInfo::TemplateParam param;
  TQString str = desc().name();
  if ( desc().hasTemplateParams() ) {
    str += "< ";

    for ( TypeDesc::TemplateParams::const_iterator it = desc().templateParams().begin(); it != desc().templateParams().end(); ++it ) {
      if ( pinfo.getParam( param, num ) && !param.name.isEmpty() )
        str += param.name;
      else
        str += "[unknown name]";

      str += " = " + ( *it ) ->fullNameChain() + ", ";
      ++num;
    }

    str.truncate( str.length() - 2 );
    str += " >";
  }
  return str;
}

TQString SimpleTypeImpl::fullTypeResolved( int depth ) {
  Debug d( "#tre#" );

  TypeDesc t = desc();
  if ( !scope().isEmpty() ) {
    if ( depth > 10 )
      return "KDevParseError::ToDeep";
    if ( !safetyCounter )
      return "KDevParseError::MaximumCountReached";

    ifVerbose( dbg() << "fully resolving type " << t.fullName() << endl );
    if ( scope().size() != 0 ) {
      t = resolveTemplateParams( t, LocateBase );
    }
  }

  return t.fullNameChain();
}


TQString SimpleTypeImpl::fullTypeUnresolvedWithScope( ) {
  if ( m_parent && !m_parent->scope().isEmpty() ) {
    return m_parent->fullTypeUnresolvedWithScope() + "::" + m_desc.fullNameChain();
  } else {
    return m_desc.fullNameChain();
  }
}

TQString SimpleTypeImpl::fullTypeResolvedWithScope( int depth ) {
  Q_UNUSED( depth );
  if ( !m_scope.isEmpty() && parent() ) {
    return parent() ->fullTypeResolvedWithScope() + "::" + fullTypeResolved();
  } else {
    return fullTypeResolved();
  }
}

void SimpleTypeImpl::checkTemplateParams () {
  invalidateCache();
  if ( ! m_scope.isEmpty() ) {
    TQString str = m_scope.back();
    m_desc = str;
    if ( !m_desc.name().isEmpty() ) {
      m_scope.pop_back();
      m_scope << m_desc.name();
    } else {
      kdDebug() << "checkTemplateParams() produced bad scope-tail: \"" << m_desc.name() << "\", \"" << m_scope.join( "::" ) << "\"" << endl;
    }
  }
}

void SimpleTypeImpl::setScope( const TQStringList& scope ) {
  invalidateCache();
  m_scope = scope;
  if ( m_scope.count() == 1 && m_scope.front().isEmpty() ) {
    //kdDebug() << "bad scope set " << kdBacktrace() << endl;
    m_scope = TQStringList();
  }
}

SimpleTypeImpl::TypeOfResult SimpleTypeImpl::searchBases ( const TypeDesc& name /*option!!*/ ) {
  TQValueList<LocateResult> parents = getBases();
  for ( TQValueList<LocateResult>::iterator it = parents.begin(); it != parents.end(); ++it ) {
    if ( !( *it ) ->resolved() )
      continue;
    TypeOfResult type = ( *it ) ->resolved() ->typeOf( name );
    if ( type )
      return type;
  }
  return TypeOfResult();
}

void SimpleTypeImpl::setSlaveParent( SimpleTypeImpl& slave ) {
  if ( ! m_masterProxy ) {
    slave.setParent( this );
  } else {
    slave.setParent( m_masterProxy );
  }
}

void SimpleTypeImpl::parseParams( TypeDesc desc ) {
  invalidateCache();
  m_desc = desc;
  m_desc.clearInstanceInfo();
}

void SimpleTypeImpl::takeTemplateParams( TypeDesc desc ) {
  invalidateCache();
  m_desc.templateParams() = desc.templateParams();
}

//SimpleTypeImpl::TemplateParamInfo implementation

bool SimpleTypeImpl::TemplateParamInfo::getParam( TemplateParam& target, TQString name ) const {
  TQMap<TQString, TemplateParam>::const_iterator it = m_paramsByName.find( name );
  if ( it != m_paramsByName.end() ) {
    target = *it;
    return true;
  }
  return false;
}

bool SimpleTypeImpl::TemplateParamInfo::getParam( TemplateParam& target, int number ) const {
  TQMap<int, TemplateParam>::const_iterator it = m_paramsByNumber.find( number );
  if ( it != m_paramsByNumber.end() ) {
    target = *it;
    return true;
  }
  return false;
}

void SimpleTypeImpl::TemplateParamInfo::removeParam( int number ) {
  TQMap<int, TemplateParam>::iterator it = m_paramsByNumber.find( number );
  if ( it != m_paramsByNumber.end() ) {
    m_paramsByName.remove( ( *it ).name );
    m_paramsByNumber.remove( it );
  }
}

void SimpleTypeImpl::TemplateParamInfo::addParam( const TemplateParam& param ) {
  m_paramsByNumber[ param.number ] = param;
  m_paramsByName[ param.name ] = param;
}

int SimpleTypeImpl::TemplateParamInfo::count() const {
  TQMap<int, TemplateParam>::const_iterator it = m_paramsByNumber.end();
  if ( it != m_paramsByNumber.begin() ) {
	  --it;
    return ( *it ).number + 1;
  } else {
    return 0;
  }
}

void SimpleTypeConfiguration::setGlobalNamespace( TypePointer globalNamespace ) {
  if ( !globalNamespace->scope().isEmpty() ) {
    kdDebug( 9007 ) << "error while setting global scope\n" << kdBacktrace() << endl;
    SimpleType::setGlobalNamespace( new SimpleTypeImpl( "" ) );
  } else {
    SimpleType::setGlobalNamespace( globalNamespace );
  }
}

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