Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 86bf63baf922cffe0bc5af085d872841611d2443 (plain) (blame)
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
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
/**********************************************************************
 * Copyright (c) 2004 Rational Software Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: 
 * IBM Rational Software - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.internal.core.parser.pst;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.cdt.internal.core.parser.pst.IDerivableContainerSymbol.IParentSymbol;
import org.eclipse.cdt.internal.core.parser.pst.ParserSymbolTable.Cost;
import org.eclipse.cdt.internal.core.parser.scanner2.ObjectSet;

/**
 * @author aniefer
 */
public final class TemplateEngine {

	static protected ITypeInfo instantiateTypeInfo( ITypeInfo info, ITemplateSymbol template, Map  argMap ) throws ParserSymbolTableException{
		if( argMap == null )
			return info;

		if( info.isType( ITypeInfo.t_type ) && info.getTypeSymbol() == null )
			return info;
		if( info.isType( ITypeInfo.t_type ) && info.getTypeSymbol() instanceof IDeferredTemplateInstance ){
			IDeferredTemplateInstance deferred = (IDeferredTemplateInstance) info.getTypeSymbol();
			ITypeInfo newInfo = TypeInfoProvider.newTypeInfo( info );
			//newInfo.setTypeSymbol( deferred.instantiate( template, argMap ) );
			template.registerDeferredInstatiation( newInfo, deferred, ITemplateSymbol.DeferredKind.TYPE_SYMBOL, argMap );
			newInfo.setTypeSymbol( deferred );
			return newInfo;
		} else if( info.isType( ITypeInfo.t_type ) && 
				   info.getTypeSymbol().isType( ITypeInfo.t_templateParameter ) &&
				   argMap.containsKey( info.getTypeSymbol() ) )
		{
			ITypeInfo targetInfo = TypeInfoProvider.newTypeInfo( (ITypeInfo) argMap.get( info.getTypeSymbol() ) );
			if( info.hasPtrOperators() ){
				targetInfo.addPtrOperator( info.getPtrOperators() );
			}
			
			if( info.checkBit( ITypeInfo.isConst ) )
				targetInfo.setBit( true, ITypeInfo.isConst );
			
			if( info.checkBit( ITypeInfo.isVolatile ) )
				targetInfo.setBit( true, ITypeInfo.isVolatile );
			
			return targetInfo;
		} else if( info.isType( ITypeInfo.t_type ) && info.getTypeSymbol().isType( ITypeInfo.t_function ) ){
			ITypeInfo newInfo = TypeInfoProvider.newTypeInfo( info );
			newInfo.setTypeSymbol( info.getTypeSymbol().instantiate( template, argMap ) );
			return newInfo;
		}
		return info;
	
	}
	
	static protected void instantiateDeferredTypeInfo( ITypeInfo info, ITemplateSymbol template, Map argMap ) throws ParserSymbolTableException {
		info.setTypeSymbol( info.getTypeSymbol().instantiate( template, argMap ) );
	}
	
	/**
	 * @param info
	 * @param symbol
	 * @param map
	 */
	public static void discardDeferredTypeInfo(ITypeInfo info, TemplateSymbol template, Map map) {
		ISymbol instance = info.getTypeSymbol();
		if( !(instance instanceof IDeferredTemplateInstance ) )
			template.removeInstantiation( (IContainerSymbol) instance );
		info.setTypeSymbol( null );
	}
	
	static protected ITemplateSymbol matchTemplatePartialSpecialization( ITemplateSymbol template, List args ) throws ParserSymbolTableException{
		if( template == null ){
			return null;
		}
		
		List specs = template.getSpecializations();
		int size = ( specs != null ) ? specs.size() : 0;
		if( size == 0 ){
			return template;
		}
		
		ISpecializedSymbol bestMatch = null;
		boolean bestMatchIsBest = true;
		ISpecializedSymbol spec = null;
		List specArgs = null;
		for( int i = 0; i < size; i++ ){
			spec = (ISpecializedSymbol) specs.get(i);
			specArgs = spec.getArgumentList();
			if( specArgs == null || specArgs.size() != args.size() ){
				continue;
			}
			
			int specArgsSize = specArgs.size();			
			HashMap map = new HashMap();
			ITypeInfo info1 = null, info2 = null;

			boolean match = true;
			for( int j = 0; j < specArgsSize; j++ ){
				info1 = (ITypeInfo) specArgs.get(j);
				info2 = (ITypeInfo) args.get(j);
				
				ISymbol sym1 = template.getSymbolTable().newSymbol( ParserSymbolTable.EMPTY_NAME );
				sym1.setTypeInfo( info1 );
				
				if( !deduceTemplateArgument( map, sym1, info2 ) ){
					match = false;
					break;
				}
			}
			if( match ){
				int compare = orderSpecializations( bestMatch, spec );
				if( compare == 0 ){
					bestMatchIsBest = false; 
				} else if( compare < 0 ) {
					bestMatch = spec;
					bestMatchIsBest = true;
				}
			}
		}
		
		//14.5.4.1 If none of the specializations is more specialized than all the other matchnig
		//specializations, then the use of the class template is ambiguous and the program is ill-formed.
		if( !bestMatchIsBest ){
			throw new ParserSymbolTableException( ParserSymbolTableException.r_Ambiguous );
		}
		
		return bestMatch;
	}
	
	static protected boolean matchTemplateParameterAndArgument( ISymbol param, ITypeInfo arg ){
		if( !isValidArgument(param, arg) ){
			return false;
		}
		
		if( param.getTypeInfo().getTemplateParameterType() == ITypeInfo.t_typeName ){
			return true;	
		} else if( param.getTypeInfo().getTemplateParameterType() == ITypeInfo.t_template ){
			
			ISymbol symbol = arg.getTypeSymbol();
			if( !arg.isType( ITypeInfo.t_type ) || symbol == null || !symbol.isType( ITypeInfo.t_template ) ){
				return false;
			}
			
			IParameterizedSymbol p = (IParameterizedSymbol) param;
			IParameterizedSymbol a = (IParameterizedSymbol) symbol;
			
			List pList = p.getParameterList();
			List aList = a.getParameterList();
			int size = pList.size();
			if( aList.size() != size){
				return false;
			}
			
			for( int i = 0; i < size; i++){
				ISymbol pParam = (ISymbol) pList.get(i);
				ISymbol aParam = (ISymbol) aList.get(i);
				
				if( pParam.getType() != aParam.getType() || 
						pParam.getTypeInfo().getTemplateParameterType() != aParam.getTypeInfo().getTemplateParameterType() )
				{
					return false;
				}
			}
			
			return true;
		} else {
			Cost cost = null;
		    TypeInfoProvider provider = param.getSymbolTable().getTypeInfoProvider();
			try{
				ITypeInfo info = provider.getTypeInfo( param.getTypeInfo().getTemplateParameterType() );
				try {
					info.copy( param.getTypeInfo() );
					info.setType( info.getTemplateParameterType() );
					cost = param.getSymbolTable().checkStandardConversionSequence( arg, info );
					provider.returnTypeInfo( info );
				} catch (ParserSymbolTableException e) {
				    //nothing
				} finally {
				    provider.returnTypeInfo( info );
				}
				
				if( cost == null || cost.rank != Cost.NO_MATCH_RANK ){
					return false;
				}
			} finally{
				if( cost != null )
					cost.release( provider );
			}
		}
		return true;
	}

	static private boolean isValidArgument(ISymbol param, ITypeInfo arg) {
		if( param.getTypeInfo().getTemplateParameterType() == ITypeInfo.t_typeName ){
			//14.3.1, local type, type with no name
			if( arg.isType( ITypeInfo.t_type ) && arg.getTypeSymbol() != null ){
				ISymbol symbol = arg.getTypeSymbol();
				if( symbol.getName().equals( ParserSymbolTable.EMPTY_NAME ) ){
					return false;
				} else if( hasNoLinkage( arg ) ){
					return false;
				}
			}
		} else if ( param.getTypeInfo().getTemplateParameterType() == ITypeInfo.t_template ){
			//TODO
		} else {
			List ptrs = param.getPtrOperators();
			ITypeInfo.PtrOp op = ( ptrs.size() > 0 ) ? (ITypeInfo.PtrOp) ptrs.get(0) : null;
			
			//if the parameter has reference type
			if( op != null && op.getType() == ITypeInfo.PtrOp.t_reference ){
				if( arg.isType( ITypeInfo.t_type )  && arg.getTypeSymbol() != null ){
					if( arg.getTypeSymbol().getName().equals( ParserSymbolTable.EMPTY_NAME ) ){
						return false;
					}
				}
				return hasExternalLinkage( arg );
			}
			
			List argPtrs = arg.getPtrOperators();
			ITypeInfo.PtrOp argOp = (argPtrs.size() > 0 ) ? (ITypeInfo.PtrOp)argPtrs.get(0) : null;
			
			//address of an object with external linkage exluding nonstatic class members
			//name of an object with external linkage excluding nonstatic class members
			if( (argOp != null && argOp.getType() == ITypeInfo.PtrOp.t_pointer ) ||
					( arg.isType( ITypeInfo.t_type ) ) )
			{
				ISymbol symbol = arg.getTypeSymbol();
				if ( symbol != null && symbol.getContainingSymbol().isType( ITypeInfo.t_class, ITypeInfo.t_union ) ){
					if( !symbol.isType( ITypeInfo.t_class, ITypeInfo.t_union ) ){
						if( !symbol.getTypeInfo().checkBit( ITypeInfo.isStatic ) ){
							return false;
						}
					}
				}
				
				return hasExternalLinkage( arg );
			}
			
			//integral or enumeration type
			if( op == null && ( arg.isType( ITypeInfo.t_bool, ITypeInfo.t_int ) || 
					arg.isType( ITypeInfo.t_enumerator )           )  )
			{	
				return true;
			}
			
			//name of a non-type template parameter
			if( arg.isType( ITypeInfo.t_templateParameter ) && 
					arg.getTemplateParameterType() != ITypeInfo.t_typeName &&
					arg.getTemplateParameterType() != ITypeInfo.t_template )
			{
				return true;
			}
			return false;
		}
		return true;	
	}
	
	static protected boolean hasExternalLinkage( ITypeInfo type ){
		if( ! type.isType( ITypeInfo.t_type ) )
			return false;
		
		return !hasNoLinkage( type );
	}
	
	static protected boolean hasInternalLinkage( ITypeInfo type ){
		return !hasNoLinkage( type );
	}
	
	static protected boolean hasNoLinkage( ITypeInfo type ){
		if( type.isType( ITypeInfo.t_type ) ){
			ISymbol symbol = type.getTypeSymbol();
			if( symbol.getContainingSymbol() == null ){
				return true;	//a temporary 
			}
			
			return symbol.getContainingSymbol().isType( ITypeInfo.t_function );	
		}
		
		return false;
	}
	
	/**
	 * 14.8.2.1-2 If P is a cv-qualified type, the top level cv-qualifiers of P's type are ignored for type
	 * deduction.  If P is a reference type, the type referred to by P is used for Type deduction.
	 * @param pSymbol
	 * @return
	 */	
	static private ITypeInfo getParameterTypeForDeduction( ISymbol pSymbol ){
		ITypeInfo p = TypeInfoProvider.newTypeInfo( pSymbol.getTypeInfo () );
		List pPtrs = p.getPtrOperators();
		if( pPtrs.size() > 0 ){
			ITypeInfo.PtrOp pOp = (ITypeInfo.PtrOp) pPtrs.get( 0 );
			if( pOp.getType() == ITypeInfo.PtrOp.t_reference || pOp.getType() == ITypeInfo.PtrOp.t_undef_ptr ){
				pPtrs.remove( 0 );	
			} else {
				ITypeInfo.PtrOp newOp = new ITypeInfo.PtrOp( pOp.getType(), false, false );
				pPtrs.set( 0, newOp );
			}
		} else {
			p.setBit( false, ITypeInfo.isConst );
			p.setBit( false, ITypeInfo.isVolatile );
		}
		

		return p;
	}
	
	/**
	 * 14.8.2.1-2
	 * if P is not a reference type
	 * - If A is an array type, the pointer type produced by the array-to-pointer conversion is used instead
	 * - If A is a function type, the pointer type produced by the function-to-pointer conversion is used instead
	 * - If A is a cv-qualified type, the top level cv-qualifiers are ignored for type deduction 
	 * @param aInfo
	 * @return
	 */
	static private ITypeInfo getArgumentTypeForDeduction( ITypeInfo aInfo, boolean pIsAReferenceType ) throws ParserSymbolTableException{
		
		ITypeInfo a = ParserSymbolTable.getFlatTypeInfo( aInfo, null );
		
		if( !pIsAReferenceType ){
			ISymbol aSymbol = a.getTypeSymbol();
			
			if( a.getType() == ITypeInfo.t_type ){
				if( aSymbol == null ){
					throw new ParserSymbolTableException( ParserSymbolTableException.r_BadTemplateArgument );
				} else if( aSymbol.isType( ITypeInfo.t_function ) &&  a.getPtrOperators().size() == 0 ){
					a.addPtrOperator( new ITypeInfo.PtrOp( ITypeInfo.PtrOp.t_pointer ) );	
				}
			}
			List aPtrs = a.getPtrOperators();
			if( aPtrs.size() > 0 ){
				ITypeInfo.PtrOp pOp = (ITypeInfo.PtrOp) aPtrs.get( 0 );
				
				if( pOp.getType() == ITypeInfo.PtrOp.t_array ){
					aPtrs.set( 0, new ITypeInfo.PtrOp( ITypeInfo.PtrOp.t_pointer, false, false ) );
				} else {
					aPtrs.set( 0, new ITypeInfo.PtrOp( pOp.getType(), false, false ) );
				}
			} else {
				a.setBit( false, ITypeInfo.isConst );
				a.setBit( false, ITypeInfo.isVolatile );
			}
		}
		
		return a;
	}
	
	static private List getSourceList( ISymbol symbol ){
		ITemplateSymbol template = null;
		
		if( symbol instanceof IDeferredTemplateInstance ){
			IDeferredTemplateInstance deferred = (IDeferredTemplateInstance) symbol;
			return deferred.getArguments();
		} 
		ISymbol instantiated = symbol.getInstantiatedSymbol();
		template = (ITemplateSymbol) instantiated.getContainingSymbol();
		
		
		if( template instanceof ISpecializedSymbol ){
			return ((ISpecializedSymbol)template).getArgumentList();
		} 
		return template.getParameterList();
	}
	
	static private List getTargetList( ISymbol symbol ){
		if( symbol instanceof IDeferredTemplateInstance ){
			IDeferredTemplateInstance deferred = (IDeferredTemplateInstance) symbol;
			return deferred.getArguments();
		} 
		ISymbol instantiated = symbol.getInstantiatedSymbol();
		if( instantiated != null ){
			ITemplateSymbol template = (ITemplateSymbol) instantiated.getContainingSymbol();
			return template.findArgumentsFor( (IContainerSymbol) symbol );
		}
		return null;
	}
	
	/**
	 * @param aSymbol
	 * @param p
	 * @param derivable
	 * @return
	 */
	private static ISymbol findMatchingBaseClass( ISymbol p, IDerivableContainerSymbol a ) {
		ISymbol aSymbol = null;
		ITemplateSymbol pTemplate = null;
		ITemplateSymbol parentTemplate = null;
		
		if( p instanceof IDeferredTemplateInstance ){
			pTemplate = ((IDeferredTemplateInstance)p).getTemplate();
		} else {
			pTemplate = (ITemplateSymbol) p.getInstantiatedSymbol().getContainingSymbol();
		}
		if( pTemplate instanceof ISpecializedSymbol ){
			pTemplate = ((ISpecializedSymbol)pTemplate).getPrimaryTemplate();
		}
		List parents = a.getParents();
		int size = parents.size();
		for( int i = 0; i < size; i++ ){
			IParentSymbol wrapper = (IParentSymbol) parents.get(i);
			ISymbol parent = wrapper.getParent();
			if( parent instanceof IDeferredTemplateInstance ){
				IDeferredTemplateInstance parentInstance = (IDeferredTemplateInstance) parent;
				parentTemplate = parentInstance.getTemplate();
				if( parentTemplate instanceof ISpecializedSymbol ){
					parentTemplate = ((ISpecializedSymbol)parentTemplate).getPrimaryTemplate();
				}
				if( pTemplate == parentTemplate ){
					aSymbol = parent;
					break;
				}
				//In general, we don't have enough information to proceed further down this branch
			} else {
				parentTemplate = (ITemplateSymbol) parent.getInstantiatedSymbol().getContainingSymbol();
				if( parentTemplate instanceof ISpecializedSymbol ){
					parentTemplate = ((ISpecializedSymbol)parentTemplate).getPrimaryTemplate();
				}
				if( pTemplate == parentTemplate ){
					aSymbol = parent;
					break;
				}
				aSymbol = findMatchingBaseClass( p, (IDerivableContainerSymbol) parent );
			}
			if( aSymbol != null )
				return aSymbol;
		}
		
		return aSymbol;
	}
	
	static private boolean deduceTemplateArgument( Map map, ISymbol pSymbol, ITypeInfo a ) throws ParserSymbolTableException{
		ISymbol symbol;
		
		boolean pIsAReferenceType = false;
		
		List ptrOps = pSymbol.getPtrOperators();
		if( ptrOps.size() > 0 && ((ITypeInfo.PtrOp)ptrOps.get(0)).getType() == ITypeInfo.PtrOp.t_reference ){
			pIsAReferenceType = true;
		}
		
		ITypeInfo p = getParameterTypeForDeduction( pSymbol );
		
		a = getArgumentTypeForDeduction( a, pIsAReferenceType );
		
		if( p.isType( ITypeInfo.t_type ) ){
			symbol = p.getTypeSymbol();
			ISymbol aSymbol = a.getTypeSymbol();
			if( symbol == null || ( a.isType( ITypeInfo.t_type) && aSymbol == null ) || a.isType( ITypeInfo.t_undef ))
				throw new ParserSymbolTableException( ParserSymbolTableException.r_BadTypeInfo );
			if( symbol instanceof IDeferredTemplateInstance || symbol.isTemplateInstance() ){
			    if( aSymbol == null )
			        return false;
				return deduceFromTemplateTemplateArguments(map, symbol, aSymbol);	
			} 
			if( symbol.isType( ITypeInfo.t_templateParameter ) ){
				if( symbol.getTypeInfo().getTemplateParameterType() == ITypeInfo.t_typeName ){
					//a = getFlatTypeInfo( a );
					List aPtrs = a.getPtrOperators();
					List pPtrs = p.getPtrOperators();
					
					if( pPtrs != null && pPtrs.size() > 0){
						if( aPtrs == null ){
							return false;
						}

						int pSize = pPtrs.size();
						int aSize = aPtrs.size();
						if( pSize != aSize )
							return false;
						
						ITypeInfo.PtrOp pOp = null;
						ITypeInfo.PtrOp aOp = null;

						int aIdx = 0;
						for( int i = 0; i < pSize; i++ ){
							pOp = (ITypeInfo.PtrOp) pPtrs.get(i);
							aOp = (ITypeInfo.PtrOp) aPtrs.get(aIdx++);
							if( pOp.getType() == aOp.getType() ){
								if( !pOp.equals( aOp ) )
									return false;
								aPtrs.remove( --aIdx );
							} else {
								return false;
							}
						} 
					}
					//cvlist T
					if( p.checkBit( ITypeInfo.isConst ) ){
						if( !a.checkBit( ITypeInfo.isConst ) )
							return false;
						a.setBit( false, ITypeInfo.isConst);
					}
					if( p.checkBit( ITypeInfo.isVolatile ) ){
						if( !a.checkBit( ITypeInfo.isVolatile ) )
							return false;
						a.setBit( false, ITypeInfo.isVolatile);
					}
					
					//T
					return deduceArgument( map, symbol, a );
						
				} else if ( symbol.getTypeInfo().getTemplateParameterType() == ITypeInfo.t_template ){
					//TODO
				} else {
					//non-type parameter
					if( symbol.getTypeInfo().getTemplateParameterType() == a.getType() ){
						return deduceArgument( map, symbol, a );
					} 
					return false;
				}
			} 
			//T (*) ( ), T ( T::* ) ( T ), & variations
			else if( symbol.isType( ITypeInfo.t_function ) ){
				if( !(aSymbol instanceof IParameterizedSymbol)|| 
						!aSymbol.isType( ITypeInfo.t_function ) )
				{
					return false;
				}
				
				IParameterizedSymbol pFunction = (IParameterizedSymbol)symbol;
				IParameterizedSymbol aFunction = (IParameterizedSymbol)aSymbol;

				if( !deduceTemplateArgument( map, pFunction.getReturnType(), aFunction.getReturnType().getTypeInfo() ) ){
					return false;
				}
				
				List pPtrs = p.getPtrOperators();
				if( pPtrs.size() != 0 ){
					ITypeInfo.PtrOp op = (ITypeInfo.PtrOp) pPtrs.get(0);
					if( op.getType() == ITypeInfo.PtrOp.t_memberPointer ){
						ITypeInfo info = TypeInfoProvider.newTypeInfo( ITypeInfo.t_type, 0, aFunction.getContainingSymbol() );
						if( !deduceTemplateArgument( map, op.getMemberOf(), info ) ){
							return false;
						}
					}
				}

				List pParams = pFunction.getParameterList();
				List aParams = aFunction.getParameterList();
				if( pParams.size() != aParams.size() ){
					return false;
				} 
				int size = pParams.size();
				for( int i = 0; i < size; i++ ){
					ITypeInfo info = ((ISymbol)aParams.get( i )).getTypeInfo();
					if( !deduceTemplateArgument( map, (ISymbol)pParams.get(i), info ) ){
						return false;
					}
				}
				return true;
			} 
			 
		} 
		if( p.isType( ITypeInfo.t_templateParameter ) ){
			return deduceArgument( map, pSymbol, a );
		}
		if( p.getType() == a.getType() ){
			if( p.getDefault() != null ){
				return ( p.getDefault().equals( a.getDefault() ) );
			}
			return true;
		}
		
		return false;
	}
	
	/**
	 * @param map
	 * @param pSymbol
	 * @param transformationMap
	 * @param symbol
	 * @param aSymbol
	 * @return
	 */
	private static boolean deduceFromTemplateTemplateArguments(Map map, ISymbol pSymbol, ISymbol aSymbol) {
		//template-name<T> or template-name<i>, where template-name is a class template
		ITemplateSymbol p = ( pSymbol instanceof IDeferredTemplateInstance ) ? 
							(ITemplateSymbol) ((IDeferredTemplateInstance) pSymbol ).getTemplate() :
							(ITemplateSymbol) pSymbol.getInstantiatedSymbol().getContainingSymbol();	
		
		if( p instanceof ISpecializedSymbol )
			p = ((ISpecializedSymbol)p).getPrimaryTemplate();
		
		ISymbol a = ( aSymbol.isTemplateInstance() ) ? aSymbol.getInstantiatedSymbol().getContainingSymbol() :
					                                   aSymbol.getContainingSymbol();
			
		if( a instanceof ISpecializedSymbol )
			a = ((ISpecializedSymbol)a).getPrimaryTemplate();
		
		if( p != a ){
			if( aSymbol instanceof IDerivableContainerSymbol ){
				aSymbol = findMatchingBaseClass( pSymbol, (IDerivableContainerSymbol) aSymbol );
			} else {
				aSymbol = null;
			}
			if( aSymbol == null ) {
				return false;
			}
		}
	
		List pList = getSourceList( pSymbol );
		List aList = getTargetList( aSymbol );
		
		//TODO: default args?
		if( pList == null || aList == null || pList.size() != aList.size()){
			return false;				
		}

		int size = pList.size();
		for( int i = 0; i < size; i++ ){
			Object obj = pList.get( i );
			ISymbol sym = null;
			if( obj instanceof ISymbol ){
				sym = (ISymbol) obj;
			} else {
				sym = pSymbol.getSymbolTable().newSymbol( ParserSymbolTable.EMPTY_NAME );
				sym.setTypeInfo( (ITypeInfo) obj );
			}
			
			ITypeInfo arg = transformTypeInfo( aList.get( i ), null );
			
			try {
				if( !deduceTemplateArgument( map, sym, arg ) ){
					return false;
				}
			} catch (ParserSymbolTableException e) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 
	 * @param template
	 * @param argList - the arguments passed in the function call
	 * @return
	 * 
	 * A type that is specified in terms of template parameters (P) is compared with an actual 
	 * type (A), and an attempt is made to find template argument vaules that will make P, 
	 * after substitution of the deduced values, compatible with A.
	 */
	static private Map deduceTemplateArgumentsUsingParameterList( ITemplateSymbol template, IParameterizedSymbol function ){

		List aList = function.getParameterList();
		int size = aList.size();
		ArrayList args = new ArrayList( size );
		 
		for( int i = 0; i < size; i++ ){
			ISymbol symbol = (ISymbol) aList.get(i);
			args.add( symbol.getTypeInfo() );
		}
		
		return deduceTemplateArguments( template, args );
	}
	/**
	 * 
	 * @param template
	 * @param args
	 * @return
	 * 
	 * A type that is specified in terms of template parameters (P) is compared with an actual 
	 * type (A), and an attempt is made to find template argument vaules that will make P, 
	 * after substitution of the deduced values, compatible with A.
	 */
	static private Map deduceTemplateArguments( ITemplateSymbol template, List arguments ){
		if( template.getContainedSymbols() == Collections.EMPTY_MAP || template.getContainedSymbols().size() != 1 ){
			return null;
		}

		ISymbol templateSymbol = template.getTemplatedSymbol();
		if( !templateSymbol.isType( ITypeInfo.t_function ) ){
			return null;
		}

		IParameterizedSymbol templateFunction = (IParameterizedSymbol) templateSymbol;
		
		List pList = templateFunction.getParameterList();
//		TODO: ellipses?
		if( pList == null || arguments == null || pList.size() != arguments.size() ){
			return null;
		}
		
		HashMap map = new HashMap();
		
		int size = pList.size();
		for( int i = 0; i < size; i++ ){
			try {
				if( !deduceTemplateArgument( map, (ISymbol) pList.get(i), (ITypeInfo) arguments.get(i) ) ){
					return null;
				}
			} catch (ParserSymbolTableException e) {
				return null;
			}
		}
		
		return map;			
	}

	static private boolean deduceArgument( Map map, ISymbol p, ITypeInfo a ){
		
		a = ParserSymbolTable.getFlatTypeInfo( a, null );
		
		if( map.containsKey( p ) ){
			ITypeInfo current = (ITypeInfo)map.get( p );
			return current.equals( a );
		} 
		map.put( p, a );
		return true;
	}
	/**
	 * Compare spec1 to spec2.  Return > 0 if spec1 is more specialized, < 0 if spec2
	 * is more specialized, = 0 otherwise.
	 * @param spec1
	 * @param spec2
	 * @return
	 */
	static private int orderSpecializations( ISpecializedSymbol spec1, ISpecializedSymbol spec2 ) throws ParserSymbolTableException{
		if( spec1 == null ){
			return -1;	
		}
		
		ISymbol decl = spec1.getTemplatedSymbol();
		
		//to order class template specializations, we need to transform them into function templates
		ITemplateSymbol template1 = spec1;
		ITemplateSymbol template2 = spec2;
		
		if( decl.isType( ITypeInfo.t_class, ITypeInfo.t_union ) ) {
			template1 = classTemplateSpecializationToFunctionTemplate( spec1 );
			template2 = classTemplateSpecializationToFunctionTemplate( spec2 );	
		}
		
		return orderTemplateFunctions( template1, template2);
	}
	
	/**
	 * Compare spec1 to spec2.  Return > 0 if spec1 is more specialized, < 0 if spec2
	 * is more specialized, = 0 otherwise.
	 * 
	 * Both spec1 and spec2 are expected to be template functions
	 * 
	 */
	static protected int orderTemplateFunctions( ITemplateSymbol spec1, ITemplateSymbol spec2 ) throws ParserSymbolTableException{
		//Using the transformed parameter list, perform argument deduction against the other
		//function template
		Map map = createMapForFunctionTemplateOrdering( spec1 );
		
		IContainerSymbol templatedSymbol = spec1.getTemplatedSymbol();
		if( !( templatedSymbol instanceof IParameterizedSymbol ) )
			throw new ParserSymbolTableError( ParserSymbolTableError.r_InternalError );
		
		IParameterizedSymbol function = (IParameterizedSymbol)templatedSymbol;
		function = (IParameterizedSymbol) function.instantiate( spec1, map );
		((TemplateSymbol)spec1).processDeferredInstantiations();
		
		Map m1 = deduceTemplateArgumentsUsingParameterList( spec2, function);
		
		map = createMapForFunctionTemplateOrdering( spec2 );
		
		templatedSymbol = spec2.getTemplatedSymbol();
		if( !( templatedSymbol instanceof IParameterizedSymbol ) )
			throw new ParserSymbolTableError( ParserSymbolTableError.r_InternalError );
		
		function = (IParameterizedSymbol)templatedSymbol;
		function = (IParameterizedSymbol) function.instantiate( spec2, map );
		((TemplateSymbol)spec2).processDeferredInstantiations();
		
		Map m2 = deduceTemplateArgumentsUsingParameterList( spec1, function );
		
		//The transformed  template is at least as specialized as the other iff the deduction
		//succeeds and the deduced parameter types are an exact match
		//A template is more specialized than another iff it is at least as specialized as the
		//other template and that template is not at least as specialized as the first.
		boolean d1 = ( m1 != null );
		boolean d2 = ( m2 != null );
		
		if( d1 && d2 || !d1 && !d2 )
			return 0;
		else if( d1 && !d2 )
			return 1;
		else 
			return -1;
	}
	
	/**
	 * transform a function template for use in partial ordering, as described in the
	 * spec 14.5.5.2-3 
	 * @param template
	 * @return
	 * -for each type template parameter, synthesize a unique type and substitute that for each
	 * occurence of that parameter in the function parameter list
	 * -for each non-type template parameter, synthesize a unique value of the appropriate type and
	 * susbstitute that for each occurence of that parameter in the function parameter list
	 * for each template template parameter, synthesize a unique class template and substitute that
	 * for each occurence of that parameter in the function parameter list
	 */

	static private Map createMapForFunctionTemplateOrdering( ITemplateSymbol template ){
		HashMap map = new HashMap();
		ITypeInfo val = null;
		List paramList = template.getParameterList();
		int size = paramList.size();
		for( int i = 0; i < size; i++ ){
			ISymbol param = (ISymbol) paramList.get( i );
			//template type parameter
			if( param.getTypeInfo().getTemplateParameterType() == ITypeInfo.t_typeName ){
				val = TypeInfoProvider.newTypeInfo( ITypeInfo.t_type, 0, template.getSymbolTable().newSymbol( "", ITypeInfo.t_class ) ); //$NON-NLS-1$
			} 
			//template parameter
			else if ( param.getTypeInfo().getTemplateParameterType() == ITypeInfo.t_template ) {
				//TODO
			}
			//non type parameter 
			else {
				val = TypeInfoProvider.newTypeInfo( param.getTypeInfo().getTemplateParameterType() );
			}
			map.put( param, val );
		}
		return map;
	}
	

	/**
	 * transform the class template to a function template as described in the spec
	 * 14.5.4.2-1
	 * @param template
	 * @return IParameterizedSymbol
	 * the function template has the same template parameters as the partial specialization and
	 * has a single function parameter whose type is a class template specialization with the template 
	 * arguments of the partial specialization
	 */
	static private ITemplateSymbol classTemplateSpecializationToFunctionTemplate( ISpecializedSymbol specialization ) throws ParserSymbolTableException{
		ISpecializedSymbol transformed = (ISpecializedSymbol) specialization.clone();
		transformed.getArgumentList().clear();
		transformed.getContainedSymbols().clear();
		//TODO clean up this
		((ContainerSymbol)transformed).getContents().clear();
		
		IParameterizedSymbol function = specialization.getSymbolTable().newParameterizedSymbol( transformed.getName(), ITypeInfo.t_function );
		try{
			transformed.addSymbol( function );
		} catch ( ParserSymbolTableException e ){
			//we shouldn't get this because there aren't any other symbols in the template
		}
		ISymbol param = specialization.getSymbolTable().newSymbol( "", ITypeInfo.t_type ); //$NON-NLS-1$
		
		param.setTypeSymbol( specialization.instantiate( specialization.getArgumentList() ) );
				
		function.addParameter( param );
		
		return transformed;
	}
	
	static private ITypeInfo transformTypeInfo( Object obj, Map argumentMap ){
		ITypeInfo info = null;
		if( obj instanceof ISymbol ){
			info = TypeInfoProvider.newTypeInfo( ITypeInfo.t_type, 0, (ISymbol) obj );
		} else {
			info = (ITypeInfo) obj;
		}
		
		if( argumentMap == null )
			return info;
		
		if( info.isType( ITypeInfo.t_type ) && 
			info.getTypeSymbol().isType( ITypeInfo.t_templateParameter ) &&
			argumentMap.containsKey( info.getTypeSymbol() ) )
		{
			ITypeInfo newType = TypeInfoProvider.newTypeInfo( (ITypeInfo) argumentMap.get( info.getTypeSymbol() ) );
			if( info.hasPtrOperators() )
				newType.addPtrOperator( info.getPtrOperators() );
			
			return newType;
		}
		
		return info;
	}
	
	static protected List selectTemplateFunctions( ObjectSet templates, List functionArguments, List templateArguments ) throws ParserSymbolTableException{
		if( templates == null || templates.size() == 0 )
			return null;
		
		List instances = null;
		
		//Iterator iter = templates.iterator();
		int size = templates.size();
		
		outer: for( int idx = 0; idx < size; idx++ ){
			IParameterizedSymbol fn = (IParameterizedSymbol) templates.keyAt( idx );
			ITemplateSymbol template = (ITemplateSymbol) fn.getContainingSymbol();
			
			Map map = deduceTemplateArguments( template, functionArguments );
			
			if( map == null )
				continue;
			
			List templateParams = template.getParameterList();
			int numTemplateParams = templateParams.size();
			int numTemplateArgs = ( templateArguments != null ) ? templateArguments.size() : 0;
			List instanceArgs = new ArrayList( templateParams.size() );
			for( int i = 0; i < numTemplateParams; i++ ){
				ISymbol param = (ISymbol) templateParams.get(i);
				ITypeInfo arg = (ITypeInfo) (  i < numTemplateArgs ? templateArguments.get(i) : null);
				ITypeInfo mapped = (ITypeInfo) map.get( param );
				
				if( arg != null && mapped != null )
					if( arg.equals( mapped ) )
						instanceArgs.add( arg );
					else
						continue outer;
				else if( arg == null && mapped == null )
					continue outer;
				else 
					instanceArgs.add( (arg != null) ? arg : mapped );
			}
			
			IContainerSymbol instance = (IContainerSymbol) template.instantiate( instanceArgs );
			
			if( instance != null ){
				if( instances == null )
					instances = new ArrayList(4);
				instances.add( instance );		
			}
		}
		
		return instances;
	}
	
	/**
	 * @param look
	 * @param parameters
	 * @param arguments
	 * @throws ParserSymbolTableException
	 */
	static protected ITemplateSymbol selectTemplateOrSpecialization( ITemplateSymbol template, List parameters, List arguments ) throws ParserSymbolTableException {
		if( template != null  ){
			//primary definition or specialization?
			boolean forPrimary = true;
			
			if( parameters.size() == 0 ){
				forPrimary = false;
			} else if( arguments != null ){
				if( arguments.size() != parameters.size() ){
					forPrimary = false;
				} else if( !parameters.isEmpty() ){
					int size = parameters.size();
					for( int i = 0; i < size; i++ ){
						if( parameters.get(i) != ((ITypeInfo) arguments.get(i)).getTypeSymbol() ){
							forPrimary = false;
							break;
						}
					}
				}
			}
			
			ITemplateSymbol primary = template;
			
			if( forPrimary ){
				//make sure parameters match up with found template
				if( checkTemplateParameterListsAreEquivalent( primary.getParameterList(), parameters ) ){
					return template;
				} 
				throw new ParserSymbolTableException( ParserSymbolTableException.r_BadTemplateParameter );
			}
			
			//specialization 
			if( parameters.isEmpty() ){
				return primary;
			}
			
			//partial specialization
			ISpecializedSymbol spec = findPartialSpecialization( template, parameters, arguments );
			
			if( spec != null )
				return spec;
			
			//TODO			
			throw new ParserSymbolTableException( ParserSymbolTableException.r_BadTemplate );	
			
		}
		return null;
	}
	
	static private boolean checkTemplateParameterListsAreEquivalent( List p1, List p2 ){
		if( p1.size() != p2.size() ){
			return false;
		}
		
		int size = p1.size();
		for( int i = 0; i < size; i++ ){
			ISymbol param1 = (ISymbol) p1.get( i );
			ISymbol param2 = (ISymbol) p2.get( i );
			if( param1.getTypeInfo().getTemplateParameterType() != param2.getTypeInfo().getTemplateParameterType() ){
				return false;
			}
		}
		return true;
	}
	
	static private boolean checkTemplateArgumentListsAreEquivalent( List p1, List p2, List a1, List a2 ){
		if( a1.size() !=  a2.size() || p1.size() != p2.size() ){
			return false;
		}
		
		Map m [] = { new HashMap(), new HashMap() };
		
		for( List list = p1; list != null; list = p2 ){
			int size = list.size();
			int index = 0;
			for( int i = 0; i < size; i++ ) {
				m[ ( list == p2 )? 1 : 0 ].put( list.get( i ), new Integer( index++ ) );
			}
			
			if( list == p2 ){
				break;
			}
		}
		
		int a1Size = a1.size();
		for( int i = 0; i < a1Size; i++ ){
			ITypeInfo t1 = (ITypeInfo) a1.get( i );
			ITypeInfo t2 = (ITypeInfo) a2.get( i );
			
			if( t1.equals( t2 ) ){
				continue;
			} else if( t1.isType( ITypeInfo.t_type ) && t2.isType( ITypeInfo.t_type ) ) {
				ISymbol s1 = t1.getTypeSymbol(), s2 = t2.getTypeSymbol();
				if( m[0].containsKey( s1 ) && m[1].containsKey( s2 ) && m[0].get( s1 ).equals( m[1].get( s2 ) ) )
					continue;
			} 
			
			return false;
		}
		return true;
	}
	
	static private ISpecializedSymbol findPartialSpecialization( ITemplateSymbol template, List parameters, List arguments ){
		List specs = template.getSpecializations();
		int size = specs.size();
		ISpecializedSymbol spec = null;
		for( int i = 0; i < size; i++ ){
			spec = (ISpecializedSymbol) specs.get(i);
			
			if( ! checkTemplateParameterListsAreEquivalent( spec.getParameterList(), parameters ) ){
				continue;
			}
			
			if( checkTemplateArgumentListsAreEquivalent( spec.getParameterList(), parameters, 
			                                             spec.getArgumentList(),  arguments  ) )
			{
				return spec;
			}
		}
		return null;
	}
	
	static protected ISymbol translateParameterForDefinition ( ISymbol templatedSymbol, ISymbol param, Map defnMap ){
		if( defnMap == Collections.EMPTY_MAP ){
			return param;
		}
		
		ISymbol mappedParam = param;
		while( mappedParam.isTemplateInstance() ){
			mappedParam = mappedParam.getInstantiatedSymbol();
		}
			
		if( defnMap.containsKey( templatedSymbol ) ){
			Map map = (Map) defnMap.get( templatedSymbol );
			
			Iterator i = map.keySet().iterator();
			while( i.hasNext() ){
				ISymbol key = (ISymbol) i.next();
				if( map.get( key ) == mappedParam ){
					return key;
				}
			}
		}
		
		return param;
	}

	/**
	 * 14.6.1-1 (2) Within the scope of a class template (class template specialization or partial specialization), when 
	 * the name of the template is neither qualified nor followed by <. it is equivalent to the name of the template 
	 * followed by the template-parameters (template-arguments) enclosed in <>
	 * 
	 * @param symbol
	 * @return
	 * @throws ParserSymbolTableException
	 */
	static protected ISymbol instantiateWithinTemplateScope( IContainerSymbol container, ITemplateSymbol symbol ) throws ParserSymbolTableException
	{
		if( symbol.getTemplatedSymbol().isType( ITypeInfo.t_function ) ){
			return symbol;
		}
		
		ISymbol instance = null;
		ITemplateSymbol template = null;
		IContainerSymbol containing = container.getContainingSymbol();
		boolean instantiate = false;
		while( containing != null ){
			if( containing == symbol  || 
				( containing instanceof ISpecializedSymbol && ((ISpecializedSymbol)containing).getPrimaryTemplate() == symbol ) )
			{
				instantiate = true;
				template = (ITemplateSymbol) containing;
				break;
			}
			containing = containing.getContainingSymbol();
			
			if( containing != null && !containing.isTemplateMember() || !containing.isType( ITypeInfo.t_template ) ){
				break;
			}
		}
		
		if( instantiate ){
			if( template instanceof ISpecializedSymbol ){
				ISpecializedSymbol spec = (ISpecializedSymbol) template;
				instance = spec.instantiate( spec.getArgumentList() );
			} else {
				List params = template.getParameterList();
				int size = params.size();
				List args = new ArrayList( size );
				for( int i = 0; i < size; i++ ){
					args.add( TypeInfoProvider.newTypeInfo( ITypeInfo.t_type, 0, (ISymbol) params.get(i) ) );
				}
				
				instance = template.instantiate( args );
			}
		}
		
		return ( instance != null ) ? instance : (ISymbol) symbol;
	}
	
	static protected boolean alreadyHasTemplateParameter( IContainerSymbol container, String name ){
		while( container != null ){
			if( container instanceof ITemplateSymbol ){
				ITemplateSymbol template = (ITemplateSymbol) container;
				if( template.getParameterMap().containsKey( name ) ){
					return true;
				}
			}
			container = container.getContainingSymbol();
		}
		return false;
	}
	
	static protected boolean canAddTemplate( IContainerSymbol containing, ITemplateSymbol template ){
		//14-2 A template-declaration can appear only as a namespace scope or class scope declaration
		if( !containing.isType( ITypeInfo.t_namespace ) && !containing.isType( ITypeInfo.t_class, ITypeInfo.t_union ) ){
			return false;
		}	
		
		//14.5.2-3  A member function template shall not be virtual
		if( containing.isTemplateMember() && containing.getContainingSymbol().isType( ITypeInfo.t_template ) ){
			ISymbol symbol = template.getTemplatedSymbol();
			if( symbol != null && symbol.isType( ITypeInfo.t_function ) && symbol.getTypeInfo().checkBit( ITypeInfo.isVirtual ) ){
				return false;
			}
		}
		
		return true;
	}
	
	static protected List verifyExplicitArguments( ITemplateSymbol template, List arguments, ISymbol symbol ) throws ParserSymbolTableException{
		List params = template.getParameterList();
		
		int numParams = params.size();
		int numArgs = arguments.size();
		List actualArgs = new ArrayList( numParams );
		for( int i = 0; i < numParams; i++ ){
			ISymbol param = (ISymbol) params.get(i);
			if( i < numArgs ){
				ITypeInfo arg = (ITypeInfo) arguments.get(i);
				if( matchTemplateParameterAndArgument( param, arg ) ){
					actualArgs.add( arg );
				} else {
					throw new ParserSymbolTableException( ParserSymbolTableException.r_BadTemplateArgument );
				}
			} else {
				//14.7.3-11 a trailing template-argument can be left unspecified in the template-id naming an explicit
				//function template specialization provided it can be deduced from the function argument type
				if( template.getTemplatedSymbol() instanceof IParameterizedSymbol &&
					symbol instanceof IParameterizedSymbol && template.getTemplatedSymbol().getName().equals( symbol.getName() ) )
				{
					Map map = deduceTemplateArgumentsUsingParameterList( template, (IParameterizedSymbol) symbol ); 
					if( map != null && map.containsKey( param ) ){
						actualArgs.add( map.get( param ) );
					} else {
						throw new ParserSymbolTableException( ParserSymbolTableException.r_BadTemplateArgument );
					}
				}
			}
		}
		return actualArgs;
	}
	
	static protected ITemplateSymbol resolveTemplateFunctions( Set functions, List args, ISymbol symbol ) throws ParserSymbolTableException{
		ITemplateSymbol template = null;
		
		Iterator iter = functions.iterator();
		
		outer: while( iter.hasNext() ){
			IParameterizedSymbol fn = (IParameterizedSymbol) iter.next();
			ITemplateSymbol tmpl = (ITemplateSymbol) fn.getContainingSymbol();
			
			Map map = deduceTemplateArgumentsUsingParameterList( tmpl, (IParameterizedSymbol) symbol );
			
			if( map == null )
				continue;			 
			List params = tmpl.getParameterList();
			int numParams = params.size();
			int numArgs = args.size();
			for( int i = 0; i < numParams && i < numArgs; i++ ){
				ISymbol param = (ISymbol) params.get(i);
				ITypeInfo arg = (ITypeInfo) args.get(i);
				if( map.containsKey( param ) ) {
					if( !map.get( param ).equals( arg )){
						continue outer;
					}
				} else if( !matchTemplateParameterAndArgument( param, arg )){
					continue outer;
				}
			}
			//made it this far, its a match
			if( template != null ){
				throw new ParserSymbolTableException(ParserSymbolTableException.r_Ambiguous );
			} 
			template = tmpl;
		}
		
		return template;
	}
	
	static protected List resolveTemplateFunctionArguments( List args, ITemplateSymbol template, IParameterizedSymbol fn )
	{
		List resultList = new ArrayList();
		
		List params = template.getParameterList();
		Map map = null;
		
		int numParams = params.size();
		int numArgs = ( args != null ) ? args.size() : 0;
		for( int i = 0; i < numParams; i++ ){
			ISymbol param = (ISymbol) params.get(i);
			ITypeInfo arg = null;
			if( i < numArgs ){
				arg = (ITypeInfo) args.get(i);
			} else {
				if( map == null ){
					map = deduceTemplateArgumentsUsingParameterList( template, fn );
					if(map == null )
						return null;
				}
				if( map.containsKey( param ) ){
					arg = (ITypeInfo) map.get( param );
				}
			}
			
			if( arg == null || !matchTemplateParameterAndArgument( param, arg ) )
				return null;
			
			resultList.add( arg );
		}
		
		return resultList;
	}
	
	static protected ISymbol checkForTemplateExplicitSpecialization( ITemplateSymbol template, ISymbol symbol, List arguments ){
		if( !template.getExplicitSpecializations().isEmpty() ){
			//TODO: could optimize this if we had a TypeInfo.hashCode()
			Iterator iter = template.getExplicitSpecializations().keySet().iterator();
			List args = null;
			while( iter.hasNext() ){
				args = (List) iter.next();
				
				if( args.equals( arguments ) ){
					Map explicitMap = (Map) template.getExplicitSpecializations().get( args );
					if( explicitMap.containsKey( symbol ) ){
						return (ISymbol) explicitMap.get( symbol );
					}
				}
			}
		}
		
		return null;
	}
	
	static protected boolean templateParametersAreEquivalent( ISymbol p1, ISymbol p2 ){
		if( !p1.isType( ITypeInfo.t_templateParameter ) || !p2.isType( ITypeInfo.t_templateParameter ) ||
			 p1.getTypeInfo().getTemplateParameterType() != p2.getTypeInfo().getTemplateParameterType() )
		{
			return false;
		}
		
		ITemplateSymbol t1 = getContainingTemplate( p1 );
		ITemplateSymbol t2 = getContainingTemplate( p2 );
		
		if( t1 == null || t2 == null )
		    return false;
		
		if( p1.getTypeInfo().getTemplateParameterType() == ITypeInfo.t_typeName )
		{
			List l1 = t1.getParameterList(), l2 = t2.getParameterList();
			return ( l1 != null && l2 != null &&  l1.indexOf( p1 ) == l2.indexOf( p2 ) ); 
		} else if( p1.getTypeInfo().getTemplateParameterType() == ITypeInfo.t_template ){
			ITemplateSymbol pt1 = (ITemplateSymbol)p1.getTypeSymbol();
			ITemplateSymbol pt2 = (ITemplateSymbol)p2.getTypeSymbol();
			return checkTemplateParameterListsAreEquivalent( pt1.getParameterList(), pt2.getParameterList() );
		} else {
			return p1.getTypeInfo().equals( p2.getTypeInfo() );
		}
	}
	
	static protected ITemplateSymbol getContainingTemplate( ISymbol symbol ){
		if( ! symbol.isTemplateMember() ){
			return null;
		}
		
		while( !( symbol.getContainingSymbol() instanceof ITemplateSymbol ) ){
			symbol = symbol.getContainingSymbol();
		}
		return (ITemplateSymbol) symbol.getContainingSymbol();
	}

	/**
	 * @param instance
	 * @param instance2
	 * @return
	 */
	protected static boolean deferedInstancesAreEquivalent(IDeferredTemplateInstance instance, IDeferredTemplateInstance instance2) {
		if( instance.getTemplate() != instance2.getTemplate() )
			return false;
		
		List args = instance.getArguments();
		List args2 = instance2.getArguments();
		int size = args.size();
		if( size != args2.size() )
			return false;
		
		for( int i = 0; i < size; i++ ){
			ITypeInfo info1 = (ITypeInfo) args.get(i);
			ITypeInfo info2 = (ITypeInfo) args2.get(i);
			
			if( ! info1.equals( info2 ) )
				return false;
		}
		return true;
	}
}

Back to the top