Skip to main content
summaryrefslogtreecommitdiffstats
blob: fd703674fac3d8010337ec868a8ff201b64706ea (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
1367
1368
1369
1370
1371
/**********************************************************************
 * This file is part of "Object Teams Development Tooling"-Software
 *
 * Copyright 2003, 2011 Fraunhofer Gesellschaft, Munich, Germany,
 * for its Fraunhofer Institute for Computer Architecture and Software
 * Technology (FIRST), Berlin, Germany and Technical University Berlin,
 * Germany.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * $Id: CalloutImplementor.java 23416 2010-02-03 19:59:31Z stephan $
 *
 * Please visit http://www.eclipse.org/objectteams for updates and contact.
 *
 * Contributors:
 * Fraunhofer FIRST - Initial API and implementation
 * Technical University Berlin - Initial API and implementation
 **********************************************************************/
package org.eclipse.objectteams.otdt.internal.core.compiler.mappings;

import static org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants.AccAbstract;
import static org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants.AccNative;
import static org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants.AccPrivate;
import static org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants.AccProtected;
import static org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants.AccPublic;
import static org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants.AccStatic;

import java.util.ArrayList;

import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
import org.eclipse.jdt.internal.compiler.ast.Argument;
import org.eclipse.jdt.internal.compiler.ast.CastExpression;
import org.eclipse.jdt.internal.compiler.ast.Expression;
import org.eclipse.jdt.internal.compiler.ast.MessageSend;
import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
import org.eclipse.jdt.internal.compiler.ast.NameReference;
import org.eclipse.jdt.internal.compiler.ast.Statement;
import org.eclipse.jdt.internal.compiler.ast.ThisReference;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration.WrapperKind;
import org.eclipse.jdt.internal.compiler.ast.Expression.DecapsulationState;
import org.eclipse.jdt.internal.compiler.lookup.Binding;
import org.eclipse.jdt.internal.compiler.lookup.ExtraCompilerModifiers;
import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
import org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.ProblemReasons;
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
import org.eclipse.jdt.internal.compiler.lookup.Scope;
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
import org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding;
import org.eclipse.jdt.internal.compiler.parser.TerminalTokens;
import org.eclipse.jdt.internal.compiler.problem.AbortCompilation;
import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
import org.eclipse.objectteams.otdt.core.compiler.IOTConstants;
import org.eclipse.objectteams.otdt.core.compiler.InferenceKind;
import org.eclipse.objectteams.otdt.core.compiler.OTNameUtils;
import org.eclipse.objectteams.otdt.core.compiler.Pair;
import org.eclipse.objectteams.otdt.core.exceptions.InternalCompilerError;
import org.eclipse.objectteams.otdt.internal.core.compiler.ast.AbstractMethodMappingDeclaration;
import org.eclipse.objectteams.otdt.internal.core.compiler.ast.CalloutMappingDeclaration;
import org.eclipse.objectteams.otdt.internal.core.compiler.ast.FieldAccessSpec;
import org.eclipse.objectteams.otdt.internal.core.compiler.ast.MethodSpec;
import org.eclipse.objectteams.otdt.internal.core.compiler.ast.ParameterMapping;
import org.eclipse.objectteams.otdt.internal.core.compiler.ast.PotentialLowerExpression;
import org.eclipse.objectteams.otdt.internal.core.compiler.ast.PrivateRoleMethodCall;
import org.eclipse.objectteams.otdt.internal.core.compiler.ast.ResultReference;
import org.eclipse.objectteams.otdt.internal.core.compiler.control.Config;
import org.eclipse.objectteams.otdt.internal.core.compiler.control.Dependencies;
import org.eclipse.objectteams.otdt.internal.core.compiler.lookup.CallinCalloutBinding;
import org.eclipse.objectteams.otdt.internal.core.compiler.lookup.CallinCalloutScope;
import org.eclipse.objectteams.otdt.internal.core.compiler.lookup.DependentTypeBinding;
import org.eclipse.objectteams.otdt.internal.core.compiler.lookup.ITeamAnchor;
import org.eclipse.objectteams.otdt.internal.core.compiler.lookup.TThisBinding;
import org.eclipse.objectteams.otdt.internal.core.compiler.lookup.WeakenedTypeBinding;
import org.eclipse.objectteams.otdt.internal.core.compiler.model.FieldModel;
import org.eclipse.objectteams.otdt.internal.core.compiler.model.MethodModel;
import org.eclipse.objectteams.otdt.internal.core.compiler.model.RoleModel;
import org.eclipse.objectteams.otdt.internal.core.compiler.model.TeamModel;
import org.eclipse.objectteams.otdt.internal.core.compiler.statemachine.transformer.AbstractStatementsGenerator;
import org.eclipse.objectteams.otdt.internal.core.compiler.util.AstClone;
import org.eclipse.objectteams.otdt.internal.core.compiler.util.AstEdit;
import org.eclipse.objectteams.otdt.internal.core.compiler.util.AstGenerator;
import org.eclipse.objectteams.otdt.internal.core.compiler.util.IProtectable;
import org.eclipse.objectteams.otdt.internal.core.compiler.util.TSuperHelper;
import org.eclipse.objectteams.otdt.internal.core.compiler.util.TypeAnalyzer;

/**
 * This Class transforms all callout-mappings (aka calloutbindings)
 * for a given role. The callout-mappings remain in the role as AST
 * nodes. The role-methods that match these mappings are transformed
 * similar to the following example:
 * void myCallout()
 * {
 *   __OT__base.myBaseMethod();
 * }
 *
 * Special tricks:
 * ===============
 * What:  Linking bestnames of arguments (relevant for anchored types in signatures).
 * Why:   Three signatures exist: roleMethodSpec, baseMethodSpec and wrapper,
 *        Each has its own scope (a temporary scope for baseMethodSpec) and
 *        set of locals.
 * Where: createCallout links roleMethodSpec and wrapper
 *        getArgument links baseMethodSpec and wrapper (if not parameter mappings are given).
 *        
 * What:  Callout to private members of a role-as-base require access via two bridges
 * Why:   private methods are not exposed in the ifc-part, implicit inheritance needs
 *        redirection via the team instance, fields need accessors anyway
 * Where: see anonymous subclass of MessageSend in transformCalloutMethodBody
 *
 * @author haebor
 */
public class CalloutImplementor extends MethodMappingImplementor
{
	private static final int INTERFACE = 0;
	private static final int CLASS = 1;

	private RoleModel _role;

	/**
	 * Generates a callout method for every callout mapping in the given RoleModel.
	 * @return false if errors had been reported during transformation, true else.
	 */
	public static boolean transformCallouts(RoleModel role) {
		boolean success = true;
		TypeDeclaration roleDecl = role.getAst();
		if (roleDecl != null && !roleDecl.isPurelyCopied && !roleDecl.binding.isSynthInterface()) { // no source level bindings present any way
	    	boolean needMethodBodies = Dependencies.needMethodBodies(roleDecl) && !role.hasBaseclassProblem() && !role.isIgnoreFurtherInvestigation();
    		// synth interfaces have no callouts anyway ;-)
            CalloutImplementor calloutImplementor = new CalloutImplementor(role);
            success &= calloutImplementor.transform(needMethodBodies);
		}
		return success;
	}

	public CalloutImplementor(RoleModel role)
	{
		this._role = role;
	    this.bindingDirection = TerminalTokens.TokenNameBINDOUT;
	}

	private boolean transform(boolean needMethodBodies)
	{
		AbstractMethodMappingDeclaration[] methodMappings =
			this._role.getAst().callinCallouts;

		boolean result = true;

		if (methodMappings != null && methodMappings.length > 0)
		{
			for (int idx = 0; idx < methodMappings.length; idx++)
			{
				AbstractMethodMappingDeclaration methodMapping = methodMappings[idx];
				if(methodMapping.isCallout())
				{
					boolean createStatements= needMethodBodies && !methodMapping.hasErrors();
					result &= createCallout((CalloutMappingDeclaration) methodMapping, createStatements, false/*inferred*/);
				}
			}
		}

		return result;
	}

    /** This method drives the creation of a callout implementation for one callout mapping. */
    private boolean createCallout(CalloutMappingDeclaration calloutMappingDeclaration, boolean needBody, boolean isInferred)
    {
		CallinCalloutScope calloutScope = calloutMappingDeclaration.scope;

		calloutMappingDeclaration.updateTSuperMethods();

		// This binding is part of the interface part of a role:
		MethodBinding roleMethodBinding = calloutMappingDeclaration.getRoleMethod();

        if(roleMethodBinding == null) // CLOVER: never true in jacks suite
        {
        	// problemreporting already done in find-Base/Role-MethodBinding
            assert(calloutMappingDeclaration.ignoreFurtherInvestigation);
			return false;
        }
        if (!roleMethodBinding.isValidBinding()) {
        	if (roleMethodBinding.problemId() != ProblemReasons.NotFound) {
        		// CLOVER: never true in jacks suite
	        	calloutMappingDeclaration.tagAsHavingErrors();
	        	// hopefully error has been reported!
	        	return false;
	        } else { // shorthand style callout
	        	// help sourceMethod() below to find another callout method
	        	MethodBinding existingMethod = calloutScope.enclosingSourceType().getExactMethod(roleMethodBinding.selector, roleMethodBinding.parameters, calloutScope.compilationUnitScope());
	        	if (existingMethod != null)
	        		roleMethodBinding = existingMethod;
	        }
        }

    	MethodDeclaration roleMethodDeclaration = null;
    	if (roleMethodBinding.declaringClass == calloutScope.enclosingSourceType())
    		roleMethodDeclaration = (MethodDeclaration)roleMethodBinding.sourceMethod();

        // have a binding but no declaration for method? -> requires creation of declaration
        boolean foundRoleDecl = roleMethodDeclaration != null;
        MethodBinding overriddenTSuper = null;
        // The following code allows to use tsuper in parameter mappings
        // (see 3.2.32-otjld-tsuper-access-1)
        if (   foundRoleDecl
        	&& roleMethodDeclaration.isCopied											// foundRoleDecl => (roleMethodDeclaration != null)
			&& (roleMethodDeclaration.modifiers & AccAbstract) == 0
			&& !TSuperHelper.isTSuper(roleMethodDeclaration.binding))
        {
        	// mapping conflicts with an implicitly inherited method.
        	// make the latter a tsuper version, now.
        	overriddenTSuper = roleMethodBinding;
        	// save a clone of the method binding before adding the marker arg, for use below.
        	roleMethodBinding = new MethodBinding(roleMethodBinding, roleMethodBinding.declaringClass); // clone

        	TSuperHelper.addMarkerArg(
        			roleMethodDeclaration,
					roleMethodDeclaration.binding.copyInheritanceSrc.declaringClass.enclosingType());
        	foundRoleDecl = false; // re-create the method;
        }

    	if (!foundRoleDecl) {
    		roleMethodDeclaration =
    			createAbstractRoleMethodDeclaration(roleMethodBinding,
    				calloutMappingDeclaration);
    		if (overriddenTSuper != null)
    			roleMethodDeclaration.binding.addOverriddenTSuper(overriddenTSuper);
    	} else {
    		roleMethodDeclaration.isReusingSourceMethod = true;							// foundRoleDecl => (roleMethodDeclaration != null)
    		// mark existing method as generated by the callout mapping:
    		roleMethodDeclaration.isMappingWrapper = WrapperKind.CALLOUT;

    		// match locator may want to know this for the interface part, too:
    		// (SH: unsure, if this is really needed, but it doesn't hurt either ;-)
    		if (roleMethodDeclaration.interfacePartMethod != null) {
    			roleMethodDeclaration.interfacePartMethod.isReusingSourceMethod = true;
        		roleMethodDeclaration.interfacePartMethod.isMappingWrapper = WrapperKind.CALLOUT;
    		}
    	}

		if (calloutMappingDeclaration.hasSignature)
    	{
			// Adjust arguments:
    		Argument[] args = calloutMappingDeclaration.roleMethodSpec.arguments;
    		if (args != null)
    		{
				for (int i=0;i<args.length;i++) {
		    		// if we already have a declaration and if we have signatures
		    		// in the mapping declaration, use the argument names from the
		    		// method mapping rather than those from the original declaration
		    		// (needed for parameter mapping!).
					if (foundRoleDecl)
						roleMethodDeclaration.arguments[i].updateName(args[i].name);

					// also link best names of arguments of roleMethodSpec and actual wrapper
					// ( requires wrapper argument to be bound, do this first.
					//   Note that all args must be bound in order!).
					roleMethodDeclaration.arguments[i].bind(roleMethodDeclaration.scope, args[i].binding.type, false);
					args[i].binding.setBestNameFromStat(roleMethodDeclaration.arguments[i]);
				}
    		}
    	}

    	if (roleMethodDeclaration != null) // try again
    	{
    		// Note: do not query the binding (as isAbstract() would do).
    		// Binding may have corrected modifiers, but take the raw modifiers.
            if (   !roleMethodDeclaration.isCopied
            	&& !roleMethodDeclaration.isGenerated
            	&& (roleMethodDeclaration.modifiers & AccAbstract) == 0)
            {
            	// bad overriding of existing / non-existant methods is handled in MethodMappingResolver already
            	roleMethodDeclaration.ignoreFurtherInvestigation = true; // don't throw "abstract method.. can only be defined by abstract class" error
            	calloutScope.problemReporter().calloutOverridesLocal(
	            		this._role.getAst(),
						calloutMappingDeclaration,
						roleMethodDeclaration.binding);
	            return false;
            }

            // fix flags (even if not needing body):
            roleMethodDeclaration.isCopied = false;
            int flagsToRemove = AccAbstract | ExtraCompilerModifiers.AccSemicolonBody | AccNative;
            roleMethodDeclaration.modifiers &= ~flagsToRemove;
            roleMethodDeclaration.binding.modifiers &= ~flagsToRemove;
            roleMethodDeclaration.isGenerated = true; // even if not generated via AstGenerator.
            if (needBody && calloutMappingDeclaration.binding.isValidBinding()) {
    	        // defer generation of statements:
    	        final CalloutMappingDeclaration mappingDeclaration = calloutMappingDeclaration;
            	MethodModel methodModel = MethodModel.getModel(roleMethodDeclaration);
            	if (isInferred)
            		methodModel._inferredCallout = mappingDeclaration;
				methodModel.setStatementsGenerator(
            		new AbstractStatementsGenerator() {
            			public boolean generateStatements(AbstractMethodDeclaration methodDecl) {
            				createCalloutMethodBody((MethodDeclaration)methodDecl, mappingDeclaration);
            				return true;
            			}
            		});
            } else if (calloutMappingDeclaration.ignoreFurtherInvestigation) {
            	roleMethodDeclaration.binding.bytecodeMissing = true; // will not be generated, so don't complain later.
            }
    	}
    	else // roleMethodDeclaration still null
    	{
    		// CLOVER: never reached in jacks suite ;-)
			throw new InternalCompilerError("OT-Compiler Error: couldn't create method declaration for callout! "  //$NON-NLS-1$
									+ calloutMappingDeclaration.toString());
    	}
        return true;
    }

    private MethodDeclaration createAbstractRoleMethodDeclaration(
		MethodBinding templateBinding,
		CalloutMappingDeclaration calloutBindingDeclaration)
	{
		IProtectable baseFeature = null; 
		MethodSpec baseMethodSpec = calloutBindingDeclaration.baseMethodSpec;
		if (baseMethodSpec != null) // else syntax error?
			baseFeature = calloutBindingDeclaration.isCalloutToField()
									  ? ((FieldAccessSpec)baseMethodSpec).resolvedField
									  : baseMethodSpec.resolvedMethod;
		int modifiers = calloutBindingDeclaration.declaredModifiers;
		if (modifiers == 0) {
			// no modifiers declared in callout, look for base feature, last is the role method template
			modifiers = ((baseFeature != null) && (templateBinding.modifiers == 0))
							? baseFeature.modifiers()
							: templateBinding.modifiers;
			modifiers &= (ExtraCompilerModifiers.AccVisibilityMASK | AccStatic);
		}
		boolean isOverridingVisibility =    calloutBindingDeclaration.isCalloutOverride()
										 && (calloutBindingDeclaration.declaredModifiers != 0);
    	if (   templateBinding.isValidBinding() // short-hand callout has a Problem(NotFound) here
    		&& templateBinding.declaringClass.isRole())
    	{
    		// try to find the ifc-part for 'templateBinding',
    		// if found, use its visibility modifiers for the new method declaration,
    		// because class part visibility modifiers are actually useless (always public..)
    		ReferenceBinding ifcPart = templateBinding.declaringClass.roleModel.getInterfacePartBinding();
	    	if (ifcPart != null) {
	    		MethodBinding ifcMethod = TypeAnalyzer.findMethod(
	    				calloutBindingDeclaration.scope, ifcPart, templateBinding.selector, templateBinding.parameters);
	    		if (   ifcMethod != null && ifcMethod.isValidBinding())
	    		{
	    			if (!isOverridingVisibility) {
	    				// no modifiers in callout, use interface modifiers of found role method:
		    			modifiers &= ~ExtraCompilerModifiers.AccVisibilityMASK;
		    			modifiers |= ifcMethod.modifiers & ExtraCompilerModifiers.AccVisibilityMASK;
	    			}
	    		}
	    	}
    	}

    	// overriding an method explicitly inherited from non-role superclass? (see TPX-416)
    	boolean overridesExplicitNonRole = false;
    	ReferenceBinding superRole = null;
    	ReferenceBinding roleClass = this._role.getClassPartBinding();
    	if (roleClass != null) {
    		superRole = roleClass.superclass();
	    	if (   superRole != null									   // have a super class
	    		&& superRole.enclosingType() != roleClass.enclosingType()) // not a role from current team
	    	{
	    		MethodBinding superMethod = TypeAnalyzer.findMethod(
	    				calloutBindingDeclaration.scope, superRole, templateBinding.selector, templateBinding.parameters);
	    		if (superMethod != null && superMethod.isValidBinding())
	    			overridesExplicitNonRole = true; // TODO(SH): need compatibility checks? (a la MethodVerifier??)
	    	}
    	}

    	if (calloutBindingDeclaration.binding.inferred == InferenceKind.NONE) { // don't advertise inferred callout via the interface.
    		if (templateBinding.isStatic())		 // no real ifc part for static method, fake it! 
	    		createInterfaceFakeStatic(templateBinding, calloutBindingDeclaration);
    		else if (((modifiers & AccPrivate) == 0) && !overridesExplicitNonRole)  // also no ifc part for privates and methods from explicit non-role super
			    createAbstractRoleMethodDeclarationPart(templateBinding,
				    calloutBindingDeclaration,
				    modifiers,
				    INTERFACE);
    	}
		return createAbstractRoleMethodDeclarationPart(templateBinding,
			calloutBindingDeclaration,
			modifiers,
			CLASS);
	}

    /** In order to resolve static role methods via the interface create a method
     *  binding without a declaration.  */
    private void createInterfaceFakeStatic(MethodBinding template, CalloutMappingDeclaration calloutDecl)
    {
    	MethodBinding newMethod = new MethodBinding(template, this._role.getInterfacePartBinding());
    	this._role.getInterfacePartBinding().addMethod(newMethod);
    }

	/**
     * Creates method in interface or class-part of _role.
	 * @param templateBinding this method is used as a template for the method that
	 *        will be created.
	 * @param calloutBindingDeclaration
	 * @param part CLASS or INTERFACE
	 * @return an empty method declaration
	 */
	private MethodDeclaration createAbstractRoleMethodDeclarationPart(
		MethodBinding templateBinding,
		CalloutMappingDeclaration calloutBindingDeclaration,
		int modifiers,
		int part)
	{
		assert(templateBinding != null);

		AstGenerator gen = new AstGenerator(calloutBindingDeclaration.sourceStart, calloutBindingDeclaration.sourceEnd);
		TypeBinding returnType;
		if (calloutBindingDeclaration.roleMethodSpec.returnType != null)
			// if this one is given, it might be instantiated:
			returnType = calloutBindingDeclaration.roleMethodSpec.returnType.resolvedType;
		else
			// CLOVER: never reached in jacks suite
			// this one should exist in any case:
			returnType = calloutBindingDeclaration.roleMethodSpec.resolvedMethod.returnType;
		MethodDeclaration newMethod = gen.method(
				calloutBindingDeclaration.compilationResult,
				modifiers, // start from these, adapt below.
				returnType,
				templateBinding.selector,
				copyArguments(gen,
						calloutBindingDeclaration.scope,
						templateBinding.parameters,
						calloutBindingDeclaration.roleMethodSpec)
			);
		newMethod.typeParameters= getTypeParameters(calloutBindingDeclaration.hasSignature,
				                                    templateBinding,
				                                    calloutBindingDeclaration.roleMethodSpec,
				                                    gen);
		if (templateBinding.problemId() == ProblemReasons.NotFound) {
			// this is a short hand callout declaration:
			MethodSpec baseMethodSpec = calloutBindingDeclaration.baseMethodSpec;
			if (baseMethodSpec != null) { // null if missing in source code (e.g. during completion)
				if (baseMethodSpec.isStatic())
					newMethod.modifiers |= AccStatic;
				if (baseMethodSpec.resolvedMethod != null) {
					newMethod.thrownExceptions = AstClone.copyExceptions(baseMethodSpec.resolvedMethod, gen);
				}
			}
		} else {
			newMethod.thrownExceptions = AstClone.copyExceptions(templateBinding, gen);
		}
	    newMethod.isMappingWrapper = WrapperKind.CALLOUT;

		if (part == INTERFACE)
		{
			// generated callout method must also be added to the interface-part since
			// role-splitting already happened
			// Note: Interface part has the access modifiers!
            newMethod.modifiers |= ExtraCompilerModifiers.AccSemicolonBody|AccAbstract;
           	AstEdit.addMethod(this._role.getInterfaceAst(), newMethod);
		}
		else // == CLASS
		{
			if ((modifiers & AccPrivate) != 0) { // don't advertize in ifc
				// FIXME(SH): need to generate bridge methdods?
			} else if (calloutBindingDeclaration.binding.inferred.isAdvertisedInInterface()) { // only if actually advertised in the ifc-part
				// generated callout method must be public in the classPart.
				// access control is done only via the interface part.
				MethodModel.getModel(newMethod).storeModifiers(newMethod.modifiers);
				newMethod.modifiers &= ~(AccProtected);
				newMethod.modifiers |= AccPublic;
			}
			// abstract will be cleared once we are done.
            AstEdit.addMethod(this._role.getAst(), newMethod);
		}
		calloutBindingDeclaration.updateRoleMethod(newMethod.binding);
		return newMethod;
	}

	/**
	 * Create the methodbody for callouts
	 */
	void createCalloutMethodBody(
		MethodDeclaration         roleMethodDeclaration,
		CalloutMappingDeclaration calloutBindingDeclaration)
	{
		if (!transformCalloutMethodBody(
						roleMethodDeclaration,
						calloutBindingDeclaration))
			roleMethodDeclaration.tagAsHavingErrors();
	}

	private boolean transformCalloutMethodBody(
            MethodDeclaration         roleMethodDeclaration,
            CalloutMappingDeclaration calloutDecl)
	{
		/* Type myCallout(int r)
		 * {
		 *   return __OT__base.myBaseMethod(r);
		 * }
		 */

        // NOTE (SH): Do not manually set any resolvedType fields.
        // If this field is set, we assume resolve has completed for that element.
        // So we rather rely on resolveType() to fill in the details later.

		// Note(SH): do not use roleMethodBinding.returnType as a short-cut:
		//           the method spec holds more specific type information (weakening!)
        TypeBinding returnType =
                calloutDecl.roleMethodSpec.returnType.resolvedType;

        int sStart ;
        int sEnd;

        // the generated method implements the callout declaration,
        // so use its source location for the wrapper method:
        sStart = calloutDecl.sourceStart;
        sEnd   = calloutDecl.sourceEnd;
        // these two are needed to generate correct line numbers:
        roleMethodDeclaration.bodyStart   = sStart;
        roleMethodDeclaration.bodyEnd     = sEnd;
        if (!roleMethodDeclaration.isReusingSourceMethod) {
	        roleMethodDeclaration.sourceStart = sStart;
	        roleMethodDeclaration.sourceEnd   = sEnd;
			roleMethodDeclaration.declarationSourceStart = sStart;
			roleMethodDeclaration.declarationSourceEnd   = sEnd;
        } // Else we have a source method in the same class.
		  // => Keep its source positions, because otherwise search et al.
		  // can't distinguish the abstract declaration and the callout

		Expression[] arguments;
        if(calloutDecl.hasSignature)
		{
        	arguments = makeWrapperCallArguments(
                    		calloutDecl,
							roleMethodDeclaration,
							calloutDecl.roleMethodSpec,
							(calloutDecl.baseMethodSpec instanceof FieldAccessSpec),
							false /*hasResultArg*/);
			if (   arguments == null
                || hasParamMappingProblems(calloutDecl, returnType, roleMethodDeclaration.scope.problemReporter()))
			{
                return false;
			}
		} else {
			arguments = makeArguments(calloutDecl, roleMethodDeclaration, calloutDecl.baseMethodSpec);
		}

        // From this point use the source location of its base method spec,
        // because the method body is a call to the method specific by the spec.
        sStart = calloutDecl.baseMethodSpec.sourceStart;
        sEnd   = calloutDecl.baseMethodSpec.sourceEnd;
        final AstGenerator gen = new AstGenerator(sStart, sEnd);

        char[] selector = calloutDecl.baseMethodSpec.selector;
        ReferenceBinding baseType = this._role.getBaseTypeBinding();
		Expression receiver = gen.castExpression(
							gen.baseNameReference(IOTConstants._OT_BASE),
							gen.baseclassReference(baseType),
							CastExpression.DO_WRAP);

		Expression baseAccess = null;
		if (calloutDecl.baseMethodSpec.isPrivate() && baseType.isRole()) {
    		// tricky case: callout to a private role method (base-side)
    		// requires the indirection via two wrapper methods (privateBridgeMethod)

    		// compensate weakening:
    		if (baseType instanceof WeakenedTypeBinding)
    			baseType = ((WeakenedTypeBinding)baseType).getStrongType();

    		// generated message send refers to public bridge, report decapsulation now:
    		calloutDecl.scope.problemReporter().decapsulation(calloutDecl.baseMethodSpec, baseType, calloutDecl.scope);

    		boolean isCalloutToField = calloutDecl.isCalloutToField();
    		MethodBinding targetMethod = calloutDecl.baseMethodSpec.resolvedMethod;
	    	baseAccess = new PrivateRoleMethodCall(receiver, selector, arguments, isCalloutToField,
	    										   calloutDecl.scope, baseType, targetMethod, gen);
    	} else {
    		if (calloutDecl.baseMethodSpec.isStatic())
    			// we thought we should use an instance
    			// but callout-to-static is sent to the base *class*
    			receiver = gen.baseNameReference(baseType.getRealClass());
    		switch (calloutDecl.baseMethodSpec.implementationStrategy) {
				case DIRECT:
					if (calloutDecl.isCalloutToField()) {
						// not using a decapsulation accessor but direct field access:
						FieldAccessSpec fieldSpec = (FieldAccessSpec) calloutDecl.baseMethodSpec;
						FieldBinding baseField = fieldSpec.resolvedField;
						if (baseField.isStatic())
							baseAccess = gen.qualifiedNameReference(baseField);
						else
							baseAccess = gen.qualifiedNameReference(new char[][] {IOTConstants._OT_BASE, baseField.name });
						if (fieldSpec.isSetter()) {
							int pos = fieldSpec.isStatic() ? 0 : 1;
							baseAccess = gen.assignment((NameReference)baseAccess, arguments[pos]);
							returnType = TypeBinding.VOID; // signal that no result processing is necessary
						}
					} else {
		    			baseAccess = gen.messageSend(receiver, selector, arguments);
					}
					break;
				case DECAPS_WRAPPER:
					if (!calloutDecl.baseMethodSpec.isStatic() && calloutDecl.isCalloutToField())
						// we thought we should use an instance
						// but decapsulating c-t-f (non-role-base) is sent to the base *class*
						receiver = gen.baseNameReference(baseType.getRealClass());
		    		baseAccess = gen.messageSend(receiver, selector, arguments);
		    		break;
				case DYN_ACCESS:
					baseAccess = CalloutImplementorDyn.baseAccessExpression(calloutDecl.scope, this._role, baseType, receiver, calloutDecl.baseMethodSpec, arguments, gen);
					break;
    		}			
		}	 

        boolean success = true;
        ArrayList<Statement> statements = new ArrayList<Statement>(3);
        if(returnType == TypeBinding.VOID)
		{
       		// just the method call
        	statements.add(baseAccess);

        	// generate empty return statement so that it gets a proper source position
        	statements.add(gen.returnStatement(null));
		} else {
			if (calloutDecl.mappings == null)
			{
				// return the result of the method call
				statements.add(
						gen.returnStatement(
								gen.potentialLift(
										null,     // use default receiver
										baseAccess,
										returnType,
										false))); // no reversing required
			}
			else
			{
				// return result with parameter mapping
				success = transformCalloutMethodBodyResultMapping(
					statements,
					baseAccess,
					calloutDecl,
					roleMethodDeclaration);
			}
		}
        if (success) {
        	roleMethodDeclaration.setStatements(
        			statements.toArray(new Statement[statements.size()]));
        } else {
        	roleMethodDeclaration.statements = new Statement[0];
        }
		return success;
	}

	/**
     * Make the arguments of this message send.
     * Note: it is not guaranteed that the parameters match!
     * @param baseMethodSpec spec for the base method (must be resolved).
     */
    private Expression[] makeArguments(
    			CalloutMappingDeclaration methodMapping,
				AbstractMethodDeclaration roleMethodDecl,
				MethodSpec baseMethodSpec)
    {
        assert (!methodMapping.hasSignature);
        Argument[]    roleArgs   = roleMethodDecl.arguments;

        MethodSpec roleMethodSpec = methodMapping.roleMethodSpec;
		AstGenerator gen = new AstGenerator(roleMethodSpec.sourceStart, roleMethodSpec.sourceEnd);
        int minArguments;
        Expression[] arguments = null;
		int offset=0;
        if (baseMethodSpec instanceof FieldAccessSpec) {
        	// field access is mapped to static method with additional first parameter _OT$base:
        	if (((FieldAccessSpec) baseMethodSpec).isSetter())
				minArguments = 1;
			else
				minArguments = 0;
        	if (!baseMethodSpec.isStatic()) {
        		arguments = new Expression[minArguments+1];
	        	// cast needed against weakened _OT$base reference
        		//   and if base is a role, to go to the class type (FIXME)
	        	gen.retargetFrom(baseMethodSpec);
	    		arguments[0] = gen.castExpression(
						gen.singleNameReference(IOTConstants._OT_BASE),
						gen.baseclassReference(this._role.getBaseTypeBinding().getRealClass()),
						this._role.getBaseTypeBinding().isRole() ? CastExpression.NEED_CLASS : CastExpression.RAW); // FIXME(SH): change to RAW and let OTRE do the cast?
	    		gen.retargetFrom(roleMethodSpec);
	    		offset = 1;
        	} else {
        		arguments = new Expression[minArguments];
        	}
        	if (((FieldAccessSpec) baseMethodSpec).isSetter())
        		arguments[offset] = new PotentialLowerExpression(
						gen.singleNameReference(roleArgs[0].name),
						adjustBaseSideType(baseMethodSpec.resolvedType()));
        	return arguments;
        } else {
        	if (roleArgs == null)
        		return new Expression[0];
        	TypeBinding[] baseParams =  baseMethodSpec.resolvedMethod.parameters;
            minArguments = Math.min(baseParams.length, roleArgs.length);
            assert(minArguments == baseParams.length);
        	arguments = new Expression[minArguments];
        	for(int i=0; i<minArguments; i++)
        	{
        		arguments[i+offset] = new PotentialLowerExpression(
        				gen.singleNameReference(roleArgs[i].name),
        				adjustBaseSideType(baseParams[i+offset]));
        	}
        	return arguments;
        }
    }

    private boolean transformCalloutMethodBodyResultMapping(
    	ArrayList<Statement>      statements,
    	Expression                resultExpr,
		CalloutMappingDeclaration calloutDecl,
		MethodDeclaration         roleMethodDeclaration)
    {
		Expression resultMapper = null;
		ParameterMapping[] mappings = calloutDecl.mappings;
		boolean resultFound = false;
		int sStart = 0;
		int sEnd = 0;
		int resultStart = 0;
		int resultEnd = 0;

		for (int i=0; i<mappings.length; i++)
		{
			if (!mappings[i].isUsedFor(calloutDecl.roleMethodSpec))
			{
				if (CharOperation.equals(mappings[i].ident.token, IOTConstants.RESULT))
				{
					if (resultFound)
					{
						roleMethodDeclaration.scope.problemReporter().duplicateParamMapping(
							   mappings[i],
							   IOTConstants.RESULT,
							   /*isCallout*/true);
						return false;
					}
					resultMapper = mappings[i].expression;
					sStart = mappings[i].sourceStart;
					sEnd   = mappings[i].sourceEnd;
					resultStart = mappings[i].ident.sourceStart;
					resultEnd   = mappings[i].ident.sourceEnd;
					resultFound = true;
				}
			}
		}
		if (!resultFound) // CLOVER: never true in jacks suite
		{
			roleMethodDeclaration.scope.problemReporter().unmappedParameter(
					IOTConstants.RESULT,
					calloutDecl.roleMethodSpec,
					/*isCallout*/true);
			return false;
		}
		assert(resultMapper != null);
		assert (calloutDecl.baseMethodSpec.hasSignature);

		AstGenerator gen = new AstGenerator(resultStart, resultEnd);

		Statement callStatement = resultExpr;

		TypeBinding baseReturnType = calloutDecl.baseMethodSpec.returnType.resolvedType;
		if (baseReturnType != TypeBinding.VOID) {
			char[] localName = IOTConstants.RESULT;
			if (calloutDecl.baseMethodSpec instanceof FieldAccessSpec)
				localName = ((FieldAccessSpec)calloutDecl.baseMethodSpec).getFieldName();
			calloutDecl.resultVar = gen.localVariable(
				localName,
				calloutDecl.baseMethodSpec.returnType.resolvedType,
				resultExpr);
			calloutDecl.resultVar.type.setBaseclassDecapsulation(DecapsulationState.REPORTED);
			callStatement = calloutDecl.resultVar;
		}

		gen.sourceStart = sStart;
		gen.sourceEnd   = sEnd;

		statements.add(callStatement);
		if (!roleMethodDeclaration.isStatic()) {
			// for "base" in parameter mappings append: BaseType base = _OT$base;
			// do this _after_ the actual forwarding so that 'base' will not shadow anything (see 3.3.21-otjld-callout-to-field-anchored-type-3)
			FieldBinding baseField = TypeAnalyzer.findField(this._role.getBinding(), IOTConstants._OT_BASE, /*static*/false, false);
			if (baseField != null) // CLOVER: never false in jacks suite
				statements.add(
						gen.localVariable(IOTConstants.BASE,
								gen.baseclassReference(baseField.type),
								gen.singleNameReference(IOTConstants._OT_BASE)));
		}
		statements.add(gen.returnStatement(
						gen.potentialLift(
							null, 	   // use default receiver
							resultMapper,
							calloutDecl.roleMethodSpec.returnType.resolvedType,
							false)));  // no reversing required
		return true;
    }

    /**
     * After building mapped arguments the parameter mapping should be empty
     * except for a result mapping. Check if this is so. Also check for
     * result mapping in void method.
     * @param calloutMappingDeclaration
     * @param returnType
     * @param problemReporter
     * @return the answer
     */
    private static boolean hasParamMappingProblems(
            CalloutMappingDeclaration calloutMappingDeclaration,
            TypeBinding returnType,
            ProblemReporter problemReporter)
    {
        boolean hasArgError = false;
        ParameterMapping[] mappings = calloutMappingDeclaration.mappings;
        if (mappings != null) {
            for (int i = 0; i < mappings.length; i++) {
            	if (CharOperation.equals(mappings[i].ident.token, IOTConstants.RESULT))
            	{
            		if (mappings[i].direction == TerminalTokens.TokenNameBINDOUT)
            		{
            			problemReporter.wrongBindingDirection(
            					calloutMappingDeclaration, mappings[i]);
            			hasArgError = true;
            		}
                    else if (returnType == TypeBinding.VOID)
                    {
                        problemReporter.resultMappingForVoidMethod(
                                calloutMappingDeclaration,
								calloutMappingDeclaration.roleMethodSpec,
                                mappings[i]);
                        hasArgError = true;
                    }
            	}
            	else if (mappings[i].expression instanceof ResultReference)
            	{
                	problemReporter.mappingResultToOther(
                			calloutMappingDeclaration, mappings[i]);
            	}
            	else
            	{
            		if (!mappings[i].isUsedFor(calloutMappingDeclaration.roleMethodSpec))
            		{
	                    if (mappings[i].direction == TerminalTokens.TokenNameBINDOUT)
	                    {
	                    	// TODO(SH): can this actually happen? Clover says: YES.
	                    	// would have to be an inexistent param ident?
	                        problemReporter.unusedParamMap(
	                                calloutMappingDeclaration,
	                                mappings[i]);
	                        // warning only
	                    }
	                    else
	                    {
                    		problemReporter.wrongBindingDirection(
		                                calloutMappingDeclaration, mappings[i]);
	                        hasArgError = true;
	                    }
                    }
                }
            }
        }
        return hasArgError;
    }
	// =======================================================

    /**
	 * Note that as a side effect, this method modifies methodMapping.mappings!
	 * @param methodMapping    lookup method spec and parameter mapping here
	 * @param wrapperDeclaration      use these if no mapping is involved
	 * @param implParameters   parameters of the implemented method to invoke
	 * @param idx              argument position on the target side
	 * @param hasResultArgument (ignored)
	 * @param sourceMethodSpec this signature defines the provided args
	 * @return a mapped argument expression or null
	 */
	Expression getArgument(
					AbstractMethodMappingDeclaration methodMapping,
					MethodDeclaration                wrapperDeclaration,
					TypeBinding[]                    implParameters,
					int                              idx,
					boolean                          hasResultArgument,
					MethodSpec                       sourceMethodSpec)
	{
		MethodSpec implementationMethodSpec = methodMapping.getImplementationMethodSpec();
		Argument[] specifiedArgs = implementationMethodSpec.arguments;

		ParameterMapping[] parameterMappings = methodMapping.mappings;
	    Expression mappedArgExpr = null;
	    char[] targetArgName = null;
		if (parameterMappings == null)
	    {
	        targetArgName = wrapperDeclaration.arguments[idx].name;
	        mappedArgExpr = genSimpleArgExpr(
	        					targetArgName,
								((CalloutMappingDeclaration)methodMapping).baseMethodSpec);
	        // if parameters are not mapped, identify provided and expected args.
	        // Note that expected args means those of the baseMethodSpec
	        // which are otherwise unreachable.
	        // (stored in a temporary scope, see AbstractMethodMappingDeclaration.resolveMethodSpecs)
	        if (idx < specifiedArgs.length) // CLOVER: never false in jacks suite
	        	specifiedArgs[idx].binding.setBestNameFromStat(wrapperDeclaration.arguments[idx]);
	    }
		else
	    {
	    	if (methodMapping.mappingExpressions == null) {
	    		assert !methodMapping.hasParsedParamMappings : "expect lack of parsing as cause for missing expressions"; //$NON-NLS-1$
		    	return null; // this indicates an error (required param mappings have not been parsed)
	    	}
	    	targetArgName = implementationMethodSpec.arguments[idx].name;
	    	Pair<Expression, Integer> mapper = methodMapping.mappingExpressions[idx];
	    	mappedArgExpr = mapper.first;
	    }
	    if (mappedArgExpr != null) {
	    	if (idx >= implParameters.length) // CLOVER: never true in jacks suite
	    		return mappedArgExpr; // arg is invisible to receiver, don't lower
			TypeBinding expectedType = implParameters[idx];
			
			// if type is anchored to another arg of the same binding, we have to translate:
			// 1.) is it a param-anchored type?
			if (expectedType.leafComponentType() instanceof DependentTypeBinding) {
				DependentTypeBinding dependentExpectedLeaf = (DependentTypeBinding)expectedType.leafComponentType();
				int anchorArgPos = dependentExpectedLeaf._argumentPosition;
				if (anchorArgPos != -1) {
					// 2.) need to translate the position using the param mapping?
					if (methodMapping.positions != null)
						anchorArgPos = methodMapping.positions[anchorArgPos]-1;
					// 3.) with this position retrieve the source argument and re-anchor the type to this anchor:
					ITeamAnchor mappedAnchor = sourceMethodSpec.arguments[anchorArgPos].binding;
					expectedType = mappedAnchor.getRoleTypeBinding(dependentExpectedLeaf, expectedType.dimensions());
				}
			}

			return new PotentialLowerExpression(mappedArgExpr, adjustBaseSideType(expectedType));
	    }

	    wrapperDeclaration.scope.problemReporter()
				.unmappedParameter(
		            targetArgName,
		            implementationMethodSpec,
					methodMapping.isCallout());
	    return null;
	}

	TypeBinding adjustBaseSideType(TypeBinding givenType) {
		ReferenceBinding baseBinding = this._role.getBaseTypeBinding();
		if (baseBinding == null) return givenType;
		if (givenType.leafComponentType().isBaseType()) return givenType;
		ReferenceBinding givenLeaf = (ReferenceBinding)givenType.leafComponentType();
		int dimensions = givenType.dimensions();
		TypeVariableBinding[] arguments = null;
		if (givenType.isParameterizedType())
			arguments = ((ParameterizedTypeBinding)givenType).typeVariables();
		if (DependentTypeBinding.isDependentType(baseBinding)) {
			ITeamAnchor anchor = ((DependentTypeBinding)baseBinding).getAnchor();
			if (anchor.isTeamContainingRole(givenLeaf)) {
				if (anchor.getResolvedType() != givenLeaf.enclosingType())
					givenLeaf = (ReferenceBinding) TeamModel.strengthenRoleType((ReferenceBinding) anchor.getResolvedType(), givenLeaf);
				return anchor.getDependentTypeBinding(givenLeaf, -1, arguments, dimensions);
			}
		}
		return givenType;
	}

	@Override
	TypeBinding[] getImplementationParamters(AbstractMethodMappingDeclaration methodMapping, MethodDeclaration wrapperMethod)
	{
		TypeBinding[] result= super.getImplementationParamters(methodMapping, wrapperMethod);
		// if we have type variables replace them with the version of the wrapper method:
		int l= result.length;
		if (result == Binding.NO_PARAMETERS || l == 0)
			return result;
		System.arraycopy(result, 0, result= new TypeBinding[l], 0, l);
		TypeVariableBinding[] variables= wrapperMethod.binding.typeVariables();
		for (int i = 0; i < result.length; i++)
			result[i] = substituteVariables(result[i], variables);
		return result;
	}

	/** Given a binding of a callout mapping as inherited from a super interface,
	 *  create the wrapper method for the current role class.
	 */
	public void generateFromBinding(CallinCalloutBinding mapping)
	{
		TypeDeclaration roleClass = this._role.getClassPartAst();
		boolean needBody = Dependencies.needMethodBodies(roleClass);
		AstGenerator gen = new AstGenerator(roleClass.sourceStart, roleClass.sourceEnd);
		CalloutMappingDeclaration callout = gen.calloutMappingDeclaration(roleClass.compilationResult);
		callout.roleMethodSpec = fromMethodBinding(mapping._roleMethodBinding, gen);
		if (mapping._baseMethods.length > 0)
			callout.baseMethodSpec = fromMethodBinding(mapping._baseMethods[0], gen);
		else
			assert !needBody : "Role needing method bodies should have base method set"; //$NON-NLS-1$
		// fake resolving:
		callout.scope = new CallinCalloutScope(roleClass.scope, callout);
		callout.scope.createBinding(callout);
		// these create a return type reference from the return type binding:
		callout.checkReturnCompatibility(callout.roleMethodSpec);
		if (callout.baseMethodSpec != null)
			callout.checkReturnCompatibility(callout.baseMethodSpec);
		// translate to method declaration:
		createCallout(callout, needBody, MethodModel.getImplementingInferredCallout(mapping._roleMethodBinding)!=null/*isInferred*/);
	}

	/**
	 * Try to infer a callout binding that implements a given abstract method.
	 *
	 * @param roleClass      type into which the callout might be generated (guaranteed to be a role)
	 * @param abstractMethod inherited abstract method
	 * @return whether or not inference was successful
	 */
	public boolean generateInferredCallout(TypeDeclaration roleClass, MethodBinding abstractMethod) {
		ReferenceBinding baseType = this._role.getBaseTypeBinding();
		if (baseType == null || !baseType.isValidBinding())
			return false;
		AstGenerator gen = new AstGenerator(roleClass.sourceStart, roleClass.sourceEnd);
		MethodSpec roleMethod = fromMethodBinding(abstractMethod, gen);
		TypeBinding[] roleParams = abstractMethod.parameters;

		return internalGenerateInferredCallout(roleClass, baseType,
											   roleMethod, roleParams, InferenceKind.INTERFACE,
											   gen);
	}

	/**
	 * Try to infer a callout binding from an otherwise unresolved message send.
	 *
	 * @param roleClass    unchecked
	 * @param messageSend
	 * @param roleParams
	 * @return whether or not inference succeeded
	 */
	public static boolean inferMappingFromCall(TypeDeclaration roleClass,
											   MessageSend     messageSend,
											   TypeBinding[]   roleParams)
	{
		if (roleClass == null || !roleClass.isRole())
			return false;

		if (!messageSend.receiver.isThis())
			return false;

		ReferenceBinding baseType = roleClass.binding.baseclass();
		if (baseType == null)
			return false;

		AstGenerator gen = new AstGenerator(messageSend.sourceStart, messageSend.sourceEnd);
		MethodSpec roleMethod = fromMessageSend(roleClass, messageSend, roleParams, gen);

		CalloutImplementor coi = new CalloutImplementor(roleClass.getRoleModel());
		if (coi.internalGenerateInferredCallout(roleClass, baseType,
												roleMethod, roleParams, InferenceKind.SELFCALL,
												gen))
		{
			messageSend.binding = roleMethod.resolvedMethod;
			return true;
		}
		return false;
	}

	/**
	 * After a role method spec has been created perform remaining tasks of
	 * inferring a base method and creating the callout mapping.
	 * (not used for inferred callout-to-field).
	 *
	 * @param roleClass      guaranteed to be a bound role
	 * @param baseType       non-null
	 * @param roleMethodSpec freshly created and manually resolved
	 * @param roleParams
	 * @param kind 			 infer from superInterface or from an unresolved self-call?
	 * @param gen            positioned AstGenerator
	 * @return whether or not inference succeeded
	 */
	private boolean internalGenerateInferredCallout(TypeDeclaration  roleClass,
													ReferenceBinding baseType,
													MethodSpec 		 roleMethodSpec,
													TypeBinding[]    roleParams,
													InferenceKind 	 kind,
													AstGenerator     gen)
	{
		CalloutMappingDeclaration callout;
		callout= gen.calloutMappingDeclaration(roleClass.compilationResult);
		callout.roleMethodSpec= roleMethodSpec;
		// fake resolving:
		callout.scope= new CallinCalloutScope(roleClass.scope, callout);

		// find matching candidate:
		MethodBinding candidate;
		candidate= inferBaseMethod(callout, baseType, roleMethodSpec.selector, roleParams);
		if (candidate == null)
			return false;

		// modifiers adjustment:
		if (kind == InferenceKind.SELFCALL && candidate.isStatic())
			// no problem to adjust method binding which is generated in #fromMessageSend()
			roleMethodSpec.resolvedMethod.modifiers |= AccStatic;

		// return type adjustments:
		TypeBinding expectedType= roleMethodSpec.resolvedType();
		if (DependentTypeBinding.isDependentType(expectedType)) {
			DependentTypeBinding roleBinding = (DependentTypeBinding)expectedType;
			if (roleBinding.getAnchor() instanceof TThisBinding)
				expectedType= roleBinding.baseclass();
		}

		// adjust return type from base method:
		if (   expectedType != null
			&& !candidate.returnType.isCompatibleWith(expectedType))
		{
			roleMethodSpec.returnType.resolvedType = candidate.returnType;
			roleMethodSpec.resolvedMethod.returnType = candidate.returnType;
		}

		// add thrown exceptions:
		roleMethodSpec.resolvedMethod.thrownExceptions = candidate.thrownExceptions;

		if (kind == InferenceKind.SELFCALL) {
			// adjust parameters from base method:
			for (int i = 0; i < roleParams.length; i++) {
				if (roleParams[i] != candidate.parameters[i]) {
					Config.requireTypeAdjustment(); // reset
					if (   roleParams[i].isCompatibleWith(candidate.parameters[i])
						&& !Config.getLoweringRequired())
					{
						roleMethodSpec.resolvedMethod.parameters[i] = candidate.parameters[i];
					}
				}
			}
		}

		// continue generating:
		callout.baseMethodSpec = fromMethodBinding(candidate, gen);

		// now we have all information for checking 3.4(d) (see Trac #96):
		if (!callout.checkVisibility(callout.baseMethodSpec, baseType))
			return false; // don't generate illegal access.

		CallinCalloutBinding calloutBinding;
		calloutBinding= callout.scope.createBinding(callout);
		calloutBinding.inferred= kind;
		calloutBinding._baseMethods = new MethodBinding[] {candidate};
		roleClass.binding.addCallinCallouts(new CallinCalloutBinding[]{calloutBinding});
		// these create a return type reference from the return type binding:
		callout.checkReturnCompatibility(callout.roleMethodSpec);
		callout.checkReturnCompatibility(callout.baseMethodSpec);
		// translate to method declaration:
		createCallout(callout, true/*needBody*/, true/*isInferred*/);
		return true;
	}

	/**
	 *
	 * @param callout    role method spec is set, base is unset.
	 * @param baseType   non-null
	 * @param selector
	 * @param roleParams
	 * @return whether or not inference succeeded
	 */
	private MethodBinding inferBaseMethod(CalloutMappingDeclaration callout,
										  ReferenceBinding 			baseType,
										  char[] 	       			selector,
										  TypeBinding[]    			roleParams)
	{
		ArrayList<MethodBinding> candidates = new ArrayList<MethodBinding>();
		while (baseType != null) {
			try {
				for (MethodBinding method : baseType.methods())
					if (   CharOperation.equals(method.selector, selector)
						&& method.parameters.length == roleParams.length)
					{
						candidates.add(method);
					}
			} catch (AbortCompilation ac) {
				if (baseType.isBinaryBinding() && ac.problem.getID() == IProblem.IsClassPathCorrect)
					return null; // binary type with missing dependencies, nothing we can infer
				throw ac; // unknown reason, don't silently swallow
			}

			for (MethodBinding candidate : candidates)
				if (callout.internalCheckParametersCompatibility(null, roleParams, candidate.parameters))
					return candidate;
			baseType= baseType.superclass();
		}
		return null;
	}

	private MethodSpec fromMethodBinding(MethodBinding method, AstGenerator gen) {
		MethodSpec result = gen.methodSpec(method.selector);
		result.resolvedMethod = method;
		setInferredReturnType(result, method.returnType, gen);
		result.initTranslationBits(); // needed for internalCheckParametersCompatibility()
		return result;
	}

	private static MethodSpec fromMessageSend(TypeDeclaration roleClass,
											  MessageSend     send,
											  TypeBinding[]   roleParams,
											  AstGenerator 	  gen)
	{
		MethodSpec result = gen.methodSpec(send.selector);
		TypeBinding expectedType = send.expectedType;
		result.resolvedMethod = new MethodBinding(AccPrivate, send.selector, expectedType, roleParams, null, roleClass.binding);
		setInferredReturnType(result, expectedType, gen);
		result.initTranslationBits(); // needed for internalCheckParametersCompatibility()
		return result;
	}
	private static void setInferredReturnType(MethodSpec methodSpec, TypeBinding expectedType, AstGenerator gen)
	{
		methodSpec.returnType = gen.singleTypeReference("<inferredType>".toCharArray()); //$NON-NLS-1$
		if (expectedType != null)
			methodSpec.returnType.resolvedType = expectedType;
		else
			methodSpec.returnType.resolvedType = TypeBinding.VOID;
	}

	/**
	 * API: If an expression failed to resolve a field,
	 * try whether using a callout-to-field could be substituted
	 * (potentially also inferring the callout itself).
	 *
	 * @param scope
	 * @param receiver  can be null
	 * @param location
	 * @param fieldName
	 * @param isSetter
	 * @param expectedType what type does the caller of this inferred callout expect? May be null.
	 * @return whether or not inference succeeded
	 */
	public static CalloutMappingDeclaration inferCalloutAccess(Scope scope, Expression receiver, Expression location, char[] fieldName, boolean isSetter, TypeBinding expectedType) {
		if (receiver != null && !(receiver instanceof ThisReference))
			return null;
		TypeDeclaration type= scope.referenceType();
		if (!type.isRole() || !type.getRoleModel().isBound())
			return null;
		char[] accessorName = OTNameUtils.accessorName(isSetter, fieldName);

		// search callout-to-field:
		CalloutMappingDeclaration callout = null;
		if (type.callinCallouts != null) {
			for (AbstractMethodMappingDeclaration mapping : type.callinCallouts) {
				if (!mapping.isCallout())
					continue;
				CalloutMappingDeclaration candidate = (CalloutMappingDeclaration)mapping;
				if (!candidate.isCalloutToField())
					continue;
				FieldAccessSpec fieldAccessSpec = (FieldAccessSpec)candidate.baseMethodSpec;
				if (fieldAccessSpec.isSetter() != isSetter)
					continue;
				FieldBinding baseField= fieldAccessSpec.resolvedField;
				if (baseField == null || !baseField.isValidBinding())
					continue;
				if (   CharOperation.equals(baseField.name, fieldName)
					&& CharOperation.equals(candidate.roleMethodSpec.selector, accessorName))
				{
					// found
					callout = candidate;
					break;
				}
			}
		}
		if (callout == null) { // second chance: infer the callout binding, too:
			AstGenerator gen= new AstGenerator(location.sourceStart, location.sourceEnd);
			callout = inferCalloutToField(type, fieldName, accessorName, isSetter, expectedType, gen);
		}

		if (callout != null) {
			if ((location.bits & ASTNode.IsCompoundAssigned) != 0) {
				// not legal in this context
				scope.problemReporter().inferredCalloutInCompoundAssignment(location, fieldName);
				return null;
			}
			scope.problemReporter().inferredUseOfCalloutToField(isSetter, location, fieldName, ((FieldAccessSpec)callout.baseMethodSpec).resolvedField);
		}
		return callout;
	}

	private static CalloutMappingDeclaration inferCalloutToField(TypeDeclaration roleClass,
														   		 char[] 		 fieldName,
														   		 char[]          accessorName,
														   		 boolean 		 isSetter,
														   		 TypeBinding     expectedType,
														   		 AstGenerator    gen)
	{
		ReferenceBinding baseclass= roleClass.binding.baseclass();
		if (baseclass == null || !baseclass.isValidBinding())
			return null;

		// find matching candidate:
		FieldBinding baseField= TypeAnalyzer.findField(baseclass, fieldName, /*static*/false, false);
		if (baseField == null)
			return null;

		FieldModel fieldModel= FieldModel.getModel(baseField);
		CalloutMappingDeclaration callout= isSetter ?
									fieldModel._setterCallout : fieldModel._getterCallout;
		if (callout != null)
			return callout; // already generated

			// generate:
		callout= gen.calloutMappingDeclaration(roleClass.compilationResult);
		callout.hasSignature= true;
		if (isSetter) {
			fieldModel._setterCallout= callout;
			fillInferredCalloutSetToField(callout, accessorName, baseField, gen);
		} else {
			fieldModel._getterCallout= callout;
			fillInferredCalloutGetToField(callout, accessorName, baseField, expectedType, gen);
		}

		// resolve:
		callout.scope= new CallinCalloutScope(roleClass.scope, callout);
		CallinCalloutBinding calloutBinding= callout.scope.createBinding(callout);
		callout.resolveMethodSpecs(roleClass.getRoleModel(), baseclass, true);
		
		calloutBinding.inferred= isSetter ? InferenceKind.FIELDSET : InferenceKind.FIELDGET;
		if (callout.baseMethodSpec.resolvedMethod != null) {			
			calloutBinding._baseMethods = new MethodBinding[] {callout.baseMethodSpec.resolvedMethod};
		} else {
			calloutBinding._baseField = ((FieldAccessSpec)callout.baseMethodSpec).resolvedField;
		}
		roleClass.binding.addCallinCallouts(new CallinCalloutBinding[]{calloutBinding});

		// translate to method declaration:
		new CalloutImplementor(roleClass.getRoleModel()).createCallout(callout, true/*needBody*/, true/*isInferred*/);

		return callout;
	}

	/* Create method specs for a callout "get" to field. */
	private static void fillInferredCalloutGetToField(CalloutMappingDeclaration callout,
												      char[] 					accessorName,
												      FieldBinding 				baseField,
												      TypeBinding 			    expectedType,
												      AstGenerator 			 	gen)
	{
		MethodSpec roleMethodSpec;
		roleMethodSpec = gen.methodSpec(accessorName);
		roleMethodSpec.hasSignature= true;
		callout.roleMethodSpec= roleMethodSpec;
		roleMethodSpec.returnType= gen.typeReference(expectedType != null ? expectedType : baseField.type);
		roleMethodSpec.arguments= new Argument[0];

		FieldAccessSpec fieldAccessSpec;
		fieldAccessSpec = gen.fieldAccessSpec(baseField.name, baseField.type, false);
		fieldAccessSpec.hasSignature= true;
		callout.baseMethodSpec= fieldAccessSpec;
	}

	/* Create method specs for a callout "set" to field. */
	private static void fillInferredCalloutSetToField(CalloutMappingDeclaration callout,
													  char[] 					accessorName,
													  FieldBinding 				baseField,
													  AstGenerator 				gen)
	{
		MethodSpec  roleMethodSpec;
		roleMethodSpec = gen.methodSpec(accessorName);
		roleMethodSpec.hasSignature= true;
		callout.roleMethodSpec= roleMethodSpec;
		roleMethodSpec.returnType= gen.singleTypeReference(TypeConstants.VOID);
		roleMethodSpec.arguments= new Argument[] {
			gen.argument(baseField.name, gen.typeReference(baseField.type))
		};

		FieldAccessSpec fieldAccessSpec;
		fieldAccessSpec = gen.fieldAccessSpec(baseField.name, baseField.type, true);
		fieldAccessSpec.hasSignature= true;
		callout.baseMethodSpec= fieldAccessSpec;
	}
}

Back to the top