Skip to main content
summaryrefslogtreecommitdiffstats
blob: 60fde271b84b0b80a3e3e5df542dcdbb08646a81 (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
/**********************************************************************
 * This file is part of "Object Teams Development Tooling"-Software
 *
 * Copyright 2004, 2012 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: ArrayTranslations.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.lifting;

import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.ast.*;
import org.eclipse.jdt.internal.compiler.ast.Expression.DecapsulationState;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.impl.Constant;
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
import org.eclipse.objectteams.otdt.core.compiler.IOTConstants;
import org.eclipse.objectteams.otdt.core.exceptions.InternalCompilerError;
import org.eclipse.objectteams.otdt.internal.core.compiler.model.TeamModel;
import org.eclipse.objectteams.otdt.internal.core.compiler.util.AstConverter;
import org.eclipse.objectteams.otdt.internal.core.compiler.util.AstEdit;
import org.eclipse.objectteams.otdt.internal.core.compiler.util.AstGenerator;

/**
 * This class handles the common part of array lifting and lowering.
 *
 * @author stephan
 * @version $Id: ArrayTranslations.java 23416 2010-02-03 19:59:31Z stephan $
 */
public abstract class ArrayTranslations {

	private static final char[] ROLE_ARRAY_ARG = "_OT$roleArray".toCharArray(); //$NON-NLS-1$
	BlockScope _scope;
	TypeBinding _providedType;
	TypeBinding _requiredType;
	Expression _teamExpr;
	Expression _expression;
	boolean     _isLifting;

	/**
	 * This method handles lowering of arrays of roles by generating an
	 * extra method that returns the lowered array.
	 *
	 * The following example shows code that will be generated
	 * for a threedimensional Role-type array:
	 *
	 * void myCallout(Role[][][] r)
	 * {
	 *   __OT$base.myBaseMethod(transformArray(r));
	 * }
	 *
	 * Base[][][] transformArray(Role[][][] role)
	 * {
	 *   Base[][][] result;
	 *
	 *   result = new Base[role.length][][];
	 *   for(;i0 < role.length;i0++){
	 * 	    result[i0] = new Base[role[i0].length][];
	 * 	    for(i1 < role[i0].length;){
	 *   		result[i0][i1] = new Base[role[i0][i1].length];
	 * 	     	for(i2 < role[i0][i1].length;){
	 * 	    		result[i0][i1][i2] = translate(role[i0][i1][i2]);
	 * 			}
	 * 	 	}
	 * 	 }
	 *   return result;
	 * }
	 * @param scope
	 * @param expression wrap this expression with the desired translation
	 * @param providedType given type of expression
	 * @param requiredType this should be produced by translation
	 * @param isLifting is a lifting translation required (else == lowering)
	 * @return a message send to the appropriate translation method.
	 */
	Expression translateArray(BlockScope  scope, Expression  expression, TypeBinding providedType, TypeBinding requiredType, boolean     isLifting) {
		this._scope      = scope;
		this._expression = expression;

		MethodBinding methodBinding = ensureTransformMethod(
										scope, this._teamExpr, providedType, requiredType, isLifting);

		AstGenerator gen = new AstGenerator(expression.sourceStart, expression.sourceEnd);
		MessageSend send = gen.messageSend(
				this._teamExpr,
				methodBinding.selector,
				new Expression[] {expression});

		// manual resolving since expression is already resolved:
        send.binding = methodBinding;
	    send.actualReceiverType = expression.resolvedType;
	    send.resolvedType   = methodBinding.returnType;
	    send.constant = Constant.NotAConstant;
	    return send;
	}

	public MethodBinding ensureTransformMethod(
							BlockScope  scope,
							Expression  teamExpr,
							TypeBinding providedType,
							TypeBinding requiredType,
							boolean     isLifting)
	{
		this._providedType = providedType;
		this._requiredType = requiredType;
		this._isLifting    = isLifting;
		this._teamExpr     = teamExpr;
		this._scope        = scope;

		ReferenceBinding roleType = isLifting ?
				  ((ReferenceBinding)requiredType.leafComponentType())
				: ((ReferenceBinding)providedType.leafComponentType());

		char[] transformMethodName = getTransformMethodName(roleType, providedType.dimensions(), isLifting);

	    ReferenceBinding enclosingTeam = roleType.enclosingType().getRealType();
		MethodBinding[] transformMethods = enclosingTeam.getMethods(transformMethodName);
	    MethodBinding methodBinding = null;
	    if ((transformMethods != null) && (transformMethods.length != 0)) {
	        if (transformMethods.length > 1) {
	            throw new InternalCompilerError("duplicate transform methods generated");                //$NON-NLS-1$
	        }
	        methodBinding = transformMethods[0];
	    } else {
	    	TeamModel teamModel = roleType.roleModel.getTeamModel();
			TypeDeclaration teamDecl = teamModel.getAst();
	    	if (teamDecl == null)
				throw new InternalCompilerError(
						"need to create transform method, but have no source type: "+new String(teamModel.getBinding().readableName())); //$NON-NLS-1$

	        MethodDeclaration transformMethod =
	                generateTransformArrayMethod(teamDecl, transformMethodName, providedType.dimensions());
	        if (teamDecl.isRole())
	        	transformMethod.modifiers |= ClassFileConstants.AccPublic;
	        AstEdit.addMethod(teamDecl, transformMethod);
	        methodBinding = transformMethod.binding;
	        if (teamDecl.isRole()) {
	        	// if team is a role, also generate a ifc-part for the method and use that
	        	TypeDeclaration ifcPart = teamDecl.getRoleModel().getInterfaceAst();
	        	MethodDeclaration ifcMethod = AstConverter.genRoleIfcMethod(ifcPart, transformMethod);
	        	AstEdit.addMethod(ifcPart, ifcMethod);
	        	methodBinding = ifcMethod.binding;
	        }
	    }
		return methodBinding;
	}

	public static char[] getTransformMethodName(ReferenceBinding roleType, int dimensions, boolean isLifting) {
		return
			CharOperation.concat(
	                CharOperation.concat(
	                    isLifting ? IOTConstants._OT_LIFT_TO : IOTConstants.OT_TRANSFORM_ARRAY,
	                    roleType.sourceName()),
	                CharOperation.concat(
	                    IOTConstants.OT_DOLLAR_NAME,
	                    (""+dimensions).toCharArray())); //$NON-NLS-1$
	}
	// === the following methods mark ast nodes as allowing baseclass decapsulation ===
	// for lifting the input side allows decapsulation, for lowering its the output side.
	/** Mark nameRef for baseclass decapsulation, if input is the base side. */
	private NameReference decapsulationInput(NameReference nameRef) {
		if (this._isLifting)
			nameRef.baseclassDecapsulation = DecapsulationState.REPORTED;
		return nameRef;
	}
	/** Mark nameRef for baseclass decapsulation, if output is the base side. */
	private NameReference decapsulationOutput(NameReference nameRef) {
		if (!this._isLifting)
			nameRef.baseclassDecapsulation = DecapsulationState.REPORTED;
		return nameRef;
	}
	/** Mark typeRef for baseclass decapsulation, if input is the base side. */
	private void decapsulationInput(TypeReference typeRef) {
		if (this._isLifting)
			typeRef.setBaseclassDecapsulation(DecapsulationState.REPORTED);
	}
	/** Mark typeRef for baseclass decapsulation, if output is the base side. */
	private void decapsulationOutput(TypeReference typeRef) {
		if (!this._isLifting)
			typeRef.setBaseclassDecapsulation(DecapsulationState.REPORTED);
	}

	/**
	 * generate code for prevent null-pointer exceptions
	 * if(r1 == null)return null;
	 * if(r1[i0][i1] == null)continue;
	 * @param currentDimension
	 * @param arrayDimensions
	 * @return an if statement
	 */
	private IfStatement generateIfStatement(int currentDimension, int arrayDimensions) {

		SingleNameReference condLeft = new SingleNameReference(ROLE_ARRAY_ARG,0);
		decapsulationInput(condLeft);

		Expression lastArrayReference = condLeft;

		for (int idx = 0; idx < currentDimension; idx++) {
			SingleNameReference pos = new SingleNameReference(generateArrayIndexName(idx),0);
			ArrayReference nextArray = new ArrayReference(lastArrayReference,pos);
			lastArrayReference = nextArray;
		}

 		Expression condRight = new NullLiteral(0,0);
		Expression condition = new EqualExpression(lastArrayReference,condRight,OperatorIds.EQUAL_EQUAL);

		Statement thenStatement=null;
		if(currentDimension == 0)
		{
			thenStatement = new ReturnStatement(new NullLiteral(0,0), 0, 0);
		}
		else
		{
			thenStatement = new ContinueStatement(null,0,0);
		}
		IfStatement ifStatement = new IfStatement(condition,thenStatement,0,0);
		return ifStatement;
	}

	/**
	 * generates an array index name . e.g. "i0","i1"...
	 * @param dimension
	 * @return the name
	 */
	private static char[] generateArrayIndexName(int dimension)
	{
		return new String(IOTConstants.OT_DOLLAR).concat("i".concat(String.valueOf(dimension))).toCharArray();	 //$NON-NLS-1$
	}
	/**
	 * 	for(int i0=0;i0 < role.length;i0++)
	 *	{
	 *		result = new Base[role.length][][];
	 *		for(...){
	 *			...
	 *				for(int i2=0;i2 < role[i0][i1].length;i2++){
	 * 		    		result[i0][i1][i2] = translate(role[i0][i1][i2]);
	 * 				}
	 * 			...
	 * 		}
	 *	}
	 * @param currentDimension counter for recursion only. invoke initially with 0.
	 * @param arrayDimensions maximum of dimensions of the array
	 * @return the new created ForStatement with all subcycles
	 */
	private ForStatement generateForStatement(int currentDimension, int arrayDimensions, AstGenerator gen)
	{

		Statement[] init = new Statement[1];
		char[] name = generateArrayIndexName(currentDimension);

		LocalDeclaration initializer = new LocalDeclaration(name,0,0);
		initializer.initialization = IntLiteral.buildIntLiteral("0".toCharArray(),0,0); //$NON-NLS-1$
		initializer.type = new SingleTypeReference(TypeConstants.INT, 0);
		init[0] = initializer;

		SingleNameReference condLeft = new SingleNameReference(name,0);

		FieldReference  condRight = new FieldReference(IOTConstants.LENGTH,0);
		SingleNameReference roleNameReference = gen.singleNameReference(ROLE_ARRAY_ARG);
		decapsulationInput(roleNameReference);

		Expression lastArrayReference = roleNameReference;

		for (int idx = 0; idx < currentDimension; idx++) {
			SingleNameReference pos = new SingleNameReference(generateArrayIndexName(idx),0);
			ArrayReference nextArray = new ArrayReference(lastArrayReference,pos);
			lastArrayReference = nextArray;
		}
		condRight.receiver =  lastArrayReference;

		Expression 	cond = new BinaryExpression(condLeft,condRight,OperatorIds.LESS);
		Statement[] inc = new Statement[1];

		inc[0]= new PostfixExpression(new SingleNameReference(name,0),IntLiteral.One,OperatorIds.PLUS,0);

		Block action = new Block(0);

		// result = new Base[role.length][][];
		Assignment arrayInstantiation =
			generateArrayInstantiation(currentDimension+1, arrayDimensions, gen);

		// if(r1 == null)continue;
		IfStatement ifStatement =
			generateIfStatement(currentDimension+1, arrayDimensions);

		if(currentDimension<arrayDimensions-1)
		{
			ForStatement innerForStatement = generateForStatement(currentDimension + 1, arrayDimensions, gen);
			action.statements = new Statement[3];
			action.statements[0] = ifStatement;
			action.statements[1] = arrayInstantiation;
			action.statements[2] = innerForStatement;
		}
		else
		{
			action.statements = new Statement[2];
			action.statements[0] = ifStatement;
			action.statements[1] = arrayInstantiation;
		}

		// for(;i0 < role.length;i0++)
		ForStatement outerForStatement = new ForStatement(init,cond, inc, action, true,0,0);

		return outerForStatement;
	}
	/**
	 * creates AST-Element
	 *
	 * if currentDimension == 1 -> result = new Base[role.length][][];
	 * if currentDimension == 2 -> result[i0] =  new Base[role[i0].length][][];
	 * if currentDimension == arrayDimensions -> result[i0][i1][i2] = role[i0][i1][i2]._OT$Base;
	 * @param currentDimension some dimensions must have special treatment
	 * @param arrayDimensions maximum of dimensions of the array
	 * @return the new created assignment
	 */
	private Assignment generateArrayInstantiation(int currentDimension, int arrayDimensions, AstGenerator gen)
	{

		SingleNameReference resultNameReference = new SingleNameReference(IOTConstants.OT_RESULT,0);
		decapsulationOutput(resultNameReference);

		Expression lastArrayReference = resultNameReference;

		for (int idx = 0; idx < currentDimension; idx++) {
			SingleNameReference pos = new SingleNameReference(generateArrayIndexName(idx),0);
			ArrayReference nextArray = new ArrayReference(lastArrayReference,pos);
			lastArrayReference = nextArray;
		}
		Expression lhsReference = lastArrayReference;

		lastArrayReference = decapsulationInput(gen.singleNameReference(ROLE_ARRAY_ARG));

		for (int idx = 0; idx < currentDimension; idx++) {
			SingleNameReference pos = new SingleNameReference(generateArrayIndexName(idx),0);
			ArrayReference nextArray = new ArrayReference(lastArrayReference,pos);
			lastArrayReference = nextArray;
		}
		Expression rhsReference = lastArrayReference;

		if(currentDimension == arrayDimensions)
		{
			return new Assignment(
					lhsReference,
					translation(rhsReference, this._providedType.leafComponentType(), this._requiredType.leafComponentType()),
					0);
		}
		else
		{
			//new Required[role[i0].length][][];
			FieldReference lengthFieldReference = new FieldReference(IOTConstants.LENGTH,0);
			lengthFieldReference.receiver = rhsReference;


			TypeReference reqTypeReference = gen.typeReference(this._requiredType.leafComponentType());
			decapsulationOutput(reqTypeReference);

			ArrayAllocationExpression reqAllocationExpression = new ArrayAllocationExpression();
			reqAllocationExpression.type = reqTypeReference;
			reqAllocationExpression.dimensions = new Expression[arrayDimensions-currentDimension];
			reqAllocationExpression.dimensions[0] = lengthFieldReference;

			Assignment assignment = new Assignment(lhsReference,reqAllocationExpression,0);
			return assignment;
		}
	}

	/**
	 * Hook method to fill in the atomic translation of one object.
	 * @param rhs
	 * @param providedType
	 * @param requiredType
	 * @return expression translating a single object
	 */
	abstract Expression translation(Expression rhs, TypeBinding providedType, TypeBinding requiredType);
	private MethodDeclaration generateTransformArrayMethod(
				TypeDeclaration teamType,
				char[] transformMethodName,
				int arrayDimensions)
	{
		AstGenerator gen = new AstGenerator(teamType.sourceStart, teamType.sourceEnd);

		MethodDeclaration transformArrayMethod = gen.method(
								teamType.compilationResult(),
								0, // modifiers
								this._requiredType,
								transformMethodName,
								new Argument[] {
										gen.argument(
						                    ROLE_ARRAY_ARG,
						                    gen.typeReference(this._providedType))
								});
		decapsulationInput(transformArrayMethod.arguments[0].type);
		decapsulationOutput(transformArrayMethod.returnType);

        // if(r1 == null)return null;
        IfStatement ifStatement =
            generateIfStatement(0, arrayDimensions);

        //  Base[][][] result = null;
        LocalDeclaration reqArrayDeclaration =
            gen.localVariable(IOTConstants.OT_RESULT, this._requiredType, gen.nullLiteral());
        decapsulationOutput(reqArrayDeclaration.type);

        //  result = new Base[role.length][][];
        Assignment arrayInstantiation =
            generateArrayInstantiation(0,arrayDimensions, gen);

        // for(...){...}
        ForStatement forStatement =
            generateForStatement(0,arrayDimensions, gen);

        // return result;
        ReturnStatement returnStatement =
            gen.returnStatement(gen.singleNameReference(IOTConstants.OT_RESULT));


        transformArrayMethod.setStatements(new Statement[] {
        		ifStatement,
        		reqArrayDeclaration,
        		arrayInstantiation,
        		forStatement,
        		returnStatement});

		return  transformArrayMethod;
	}
}

Back to the top