Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3c449c8a439081217a2dd193197e2238f72d8673 (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
/*******************************************************************************
 * Copyright (c) 2015 Willink Transformations and others.
 * 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
 *
 * Contributors:
 *     E.D.Willink - initial API and implementation
 *******************************************************************************/
package org.eclipse.qvtd.cs2as.compiler.internal;

import java.util.List;
import java.util.Set;

import org.eclipse.emf.common.util.EList;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.ocl.examples.codegen.analyzer.AnalysisVisitor;
import org.eclipse.ocl.examples.codegen.analyzer.BoxingAnalyzer;
import org.eclipse.ocl.examples.codegen.analyzer.DependencyVisitor;
import org.eclipse.ocl.examples.codegen.analyzer.FieldingAnalyzer;
import org.eclipse.ocl.examples.codegen.analyzer.ReferencesVisitor;
import org.eclipse.ocl.examples.codegen.cgmodel.CGCallExp;
import org.eclipse.ocl.examples.codegen.cgmodel.CGGuardExp;
import org.eclipse.ocl.examples.codegen.cgmodel.CGPackage;
import org.eclipse.ocl.examples.codegen.cgmodel.CGTypeId;
import org.eclipse.ocl.examples.codegen.cgmodel.CGValuedElement;
import org.eclipse.ocl.examples.codegen.cgmodel.CGVariable;
import org.eclipse.ocl.examples.codegen.cgmodel.CGVariableExp;
import org.eclipse.ocl.examples.codegen.cse.GlobalPlace;
import org.eclipse.ocl.examples.codegen.dynamic.OCL2JavaFileObject;
import org.eclipse.ocl.examples.codegen.generator.TypeDescriptor;
import org.eclipse.ocl.examples.codegen.java.CG2JavaPreVisitor;
import org.eclipse.ocl.examples.codegen.java.JavaConstants;
import org.eclipse.ocl.examples.codegen.utilities.CGModelResourceFactory;
import org.eclipse.ocl.pivot.OCLExpression;
import org.eclipse.ocl.pivot.Operation;
import org.eclipse.ocl.pivot.OperationCallExp;
import org.eclipse.ocl.pivot.Parameter;
import org.eclipse.ocl.pivot.utilities.ClassUtil;
import org.eclipse.qvtd.codegen.qvti.QVTiCodeGenOptions;
import org.eclipse.qvtd.codegen.qvti.analyzer.QVTiAS2CGVisitor;
import org.eclipse.qvtd.codegen.qvti.analyzer.QVTiAnalysisVisitor;
import org.eclipse.qvtd.codegen.qvti.analyzer.QVTiAnalyzer;
import org.eclipse.qvtd.codegen.qvti.analyzer.QVTiBoxingAnalyzer;
import org.eclipse.qvtd.codegen.qvti.analyzer.QVTiDependencyVisitor;
import org.eclipse.qvtd.codegen.qvti.analyzer.QVTiFieldingAnalyzer;
import org.eclipse.qvtd.codegen.qvti.analyzer.QVTiReferencesVisitor;
import org.eclipse.qvtd.codegen.qvti.java.QVTiCG2JavaPreVisitor;
import org.eclipse.qvtd.codegen.qvti.java.QVTiCG2JavaVisitor;
import org.eclipse.qvtd.codegen.qvti.java.QVTiCodeGenerator;
import org.eclipse.qvtd.codegen.qvti.java.QVTiGlobalContext;
import org.eclipse.qvtd.codegen.qvticgmodel.CGGuardVariable;
import org.eclipse.qvtd.codegen.qvticgmodel.CGMapping;
import org.eclipse.qvtd.codegen.utilities.QVTiCGUtil;
import org.eclipse.qvtd.cs2as.compiler.CS2ASJavaCompiler;
import org.eclipse.qvtd.cs2as.compiler.CS2ASJavaCompilerParameters;
import org.eclipse.qvtd.cs2as.compiler.cgmodel.CGLookupCallExp;
import org.eclipse.qvtd.cs2as.compiler.cgmodel.CS2ASCGFactory;
import org.eclipse.qvtd.cs2as.compiler.cgmodel.util.CS2ASCGModelVisitor;
import org.eclipse.qvtd.cs2as.compiler.internal.utilities.CS2ASCGModelResourceFactory;
import org.eclipse.qvtd.cs2as.runtime.CS2ASTransformation;
import org.eclipse.qvtd.cs2as.runtime.CS2ASTransformationExecutor;
import org.eclipse.qvtd.pivot.qvtbase.Transformation;
import org.eclipse.qvtd.pivot.qvtbase.evaluation.AbstractTransformer;
import org.eclipse.qvtd.pivot.qvtimperative.evaluation.QVTiEnvironmentFactory;
import org.eclipse.qvtd.pivot.qvtimperative.utilities.QVTimperative;


public class CS2ASJavaCompilerImpl implements CS2ASJavaCompiler {

	protected static class CS2ASJavaCodeGenerator extends QVTiCodeGenerator
	{
		private  @NonNull CS2ASJavaCompilerParameters params;
		
		protected CS2ASJavaCodeGenerator(@NonNull QVTiEnvironmentFactory environmentFactory, @NonNull Transformation transformation,
				@NonNull CS2ASJavaCompilerParameters params) {
			super(environmentFactory, transformation);
			this.params = params;
		}

		@Override
		protected @NonNull QVTiCG2JavaVisitor createCG2JavaVisitor(@NonNull CGPackage cgPackage, @Nullable List<CGValuedElement> sortedGlobals) {
			return new CS2ASCG2JavaVisitor(this, cgPackage, sortedGlobals);
		}
		
		@Override
		protected @NonNull QVTiAS2CGVisitor createAS2CGVisitor(@NonNull QVTiAnalyzer analyzer,
				@NonNull QVTiGlobalContext gContext) {
			return new CS2ASAS2CGVisitor(analyzer, gContext);
		}
		
		@Override
		public @NonNull AnalysisVisitor createAnalysisVisitor() {
			return new CS2ASAnalysisVisitor(cgAnalyzer);
		}
		
		@Override
		public @NonNull BoxingAnalyzer createBoxingAnalyzer() {
		
			return new CS2ASBoxingAnalyser(cgAnalyzer);
		}
		
		@Override
		public @NonNull FieldingAnalyzer createFieldingAnalyzer() {
			return new CS2ASFieldingAnalyser(cgAnalyzer);
		}
		
		@Override
		public @NonNull CG2JavaPreVisitor createCG2JavaPreVisitor() {
			return new CS2ASCG2JavaPreVisitor(getGlobalContext());
		}
		
		@Override
		public @NonNull ReferencesVisitor createReferencesVisitor() {
			return CS2ASReferencesVisitor.INSTANCE;
		}
		
		@Override
		public @NonNull DependencyVisitor createDependencyVisitor() {
			return new CS2ASDependencyVisitor(cgAnalyzer, getGlobalContext(),
					getGlobalPlace());
		}
		
		@Override
		public @NonNull CGModelResourceFactory getCGResourceFactory() {
			return CS2ASCGModelResourceFactory.INSTANCE;
		}

		public @NonNull CS2ASJavaCompilerParameters getCGParameters() {
			return params;
		}
	}

	protected static class CS2ASCG2JavaVisitor extends QVTiCG2JavaVisitor implements CS2ASCGModelVisitor<Boolean>
	{
	
		protected CS2ASCG2JavaVisitor(@NonNull QVTiCodeGenerator codeGenerator, @NonNull CGPackage cgPackage, @Nullable List<CGValuedElement> sortedGlobals) {
			super(codeGenerator, cgPackage, sortedGlobals);
		}

		@Override
		protected void appendGuardFailure(@NonNull CGGuardExp cgGuardExp) {
			CGMapping cgMapping = QVTiCGUtil.getContainingCGMapping(cgGuardExp);
			if (cgMapping != null) {
				List<CGGuardVariable> freeVariables = cgMapping.getFreeVariables();
				if (freeVariables.size() > 0) {
					CGGuardVariable cgGuardVariable = freeVariables.get(0);
					js.append("throwNull(");
					js.appendValueName(cgGuardVariable);
					js.append(", ");
					js.appendString("Null " + cgGuardExp.getMessage());
					js.append(");\n");
					return;
				}
			}
//			CGUtil.createCGParameter(name, typeId)
			super.appendGuardFailure(cgGuardExp);
		}

		protected @NonNull Class<? extends AbstractTransformer> getAbstractTransformationExecutorClass() {
			return CS2ASTransformationExecutor.class;
		}

		@Override
		@Nullable
		public Boolean visitCGLookupCallExp(CGLookupCallExp cgCall) {
			CS2ASJavaCompilerParameters params = ((CS2ASJavaCodeGenerator)getCodeGenerator()).getCGParameters();
			
			CGValuedElement cgSource = cgCall.getSource(); // FIXME to skip env() call. Remove env() call
			TypeDescriptor typeDescriptor = context.getTypeDescriptor(cgCall);
			if (!js.appendLocalStatements(cgSource)) {
				return false;
			}
			
			
			List<CGValuedElement> cgArguments = cgCall.getArguments();
			for (@NonNull CGValuedElement cgArgument : cgArguments) {
				CGValuedElement argument = getExpression(cgArgument);
				if (!js.appendLocalStatements(argument)) {
					return false;
				}
			}
			
			String envClassName = params.getEnviromentClassName();
			String envClassSymbol = getSymbolName(envClassName + cgCall.hashCode(), "_lookupEnv");
			js.appendClassReference(envClassName);
			js.append(" ");
			js.append(envClassSymbol);
			js.append(" = new ");
			js.appendClassReference(envClassName);
			js.append("(");
			js.append(JavaConstants.EXECUTOR_NAME);
			js.append(",");
			List<Parameter> pParameters = cgCall.getReferredOperation().getOwnedParameters();
			int iMax = Math.min(pParameters.size(), cgArguments.size());
			for (int i = 0; i < iMax; i++) {
				if ((i > 0)) {
					js.append(", ");
				}
				CGValuedElement cgArgument = cgArguments.get(i);
				Parameter pParameter = pParameters.get(i);
				CGTypeId cgTypeId = analyzer.getTypeId(pParameter.getTypeId());
				TypeDescriptor parameterTypeDescriptor = context.getUnboxedDescriptor(ClassUtil.nonNullState(cgTypeId.getElementId()));
				CGValuedElement argument = getExpression(cgArgument);
				js.appendReferenceTo(parameterTypeDescriptor, argument);
			}
			js.append(");\n");
			
			String visitorClassName = params.getVisitorClassName();
			String visitorClassSymbol = getSymbolName(visitorClassName + cgCall.hashCode(), "_lookupVisitor");
			js.appendClassReference(visitorClassName);
			js.append(" ");
			js.append(visitorClassSymbol);
			js.append(" = new ");
			js.appendClassReference(params.getVisitorClassName());
			js.append("(");
			js.append(envClassSymbol);
			js.append(");\n");
			
			// FIXME what if there is not common named element class name ?
			String namedElemClassName = params.getNamedElementClassName();
			String resultSymbol = getSymbolName(namedElemClassName + cgCall.hashCode(), "_lookupResult");
			js.appendClassReference(EList.class, false, namedElemClassName); 
			js.append(" ");
			js.append(resultSymbol);	
			js.append(" = ");				
			js.appendReferenceTo(cgSource);
			js.append(".accept(");
			js.append(visitorClassSymbol);
			js.append(").getNamedElements();\n");
			
			js.appendClassReference(typeDescriptor);
			js.append(" ");
			js.appendReferenceTo(cgCall);
			js.append( " = null;\n");
			js.append("if ("+resultSymbol+".size() == 1) {\n");
			js.pushIndentation(null);
			js.appendReferenceTo(cgCall);
			js.append(" = (");
			js.appendClassReference(typeDescriptor);
			js.append(")");
			js.append(resultSymbol+".get(0);\n");
			js.popIndentation();
			// TODO what about ambigous error report ?
			js.append("} else {\n");
			js.pushIndentation(null);
			
			CGValuedElement lookupArg = cgArguments.get(0); // FIXMe improve handleLookupError
			CGValuedElement initialSource = initialSourceCG(lookupArg);
			js.append("handleLookupError(");
			js.appendReferenceTo(initialSource); 
			js.append(",");
			js.appendReferenceTo(lookupArg);
			js.append(");\n");
			
			js.popIndentation();
			js.append("};\n");
			return true;
		}
		
		/**
		 * @param cgValue
		 * @return helper to obtain the initial CGValuedElement corresponding to the source of the lookupCall argument
		 */
		private CGValuedElement initialSourceCG(CGValuedElement cgValue) {
			if (cgValue instanceof CGVariableExp) {
				CGValuedElement cgVarInit = ((CGVariableExp) cgValue).getReferredVariable().getInit();
				if (cgVarInit == null) {
					return cgValue;
				} else {
					return cgVarInit == cgValue ? cgVarInit : initialSourceCG(cgVarInit);	
				}
				
			} else if (cgValue instanceof CGCallExp) {
				CGValuedElement cgSource = ((CGCallExp) cgValue).getSource();
				return initialSourceCG(cgSource);
			}
			return cgValue;
		}
	}

	protected static class CS2ASAS2CGVisitor extends QVTiAS2CGVisitor {

		public CS2ASAS2CGVisitor(QVTiAnalyzer analyzer, QVTiGlobalContext globalContext) {
			super(analyzer, globalContext);
		}
		
		@Override
		protected @NonNull CGValuedElement generateOperationCallExp(@Nullable CGValuedElement cgSource, @NonNull OperationCallExp asOperationCallExp) {
			
			Operation asOperation = asOperationCallExp.getReferredOperation();
			if (isLookupOp(asOperation)) {
				CGLookupCallExp cgLookupCallExp = CS2ASCGFactory.eINSTANCE.createCGLookupCallExp();				
				setAst(cgLookupCallExp, asOperationCallExp);
				cgLookupCallExp.setSource(cgSource);
				for (OCLExpression arg :  asOperationCallExp.getOwnedArguments()) {
					CGValuedElement cgArg = doVisit(CGValuedElement.class, arg);
					cgLookupCallExp.getArguments().add(cgArg);
				}
				cgLookupCallExp.setReferredOperation(asOperation);
				return  cgLookupCallExp;
			}
			return super.generateOperationCallExp(cgSource, asOperationCallExp);
		}
		
		protected boolean isLookupOp(Operation op) {
			
			String opName = op.getName();
			if (opName != null && opName.startsWith("_lookup"))	{// FIXME
				return true;
			}
			return false;
		}
	}
	
	protected static class CS2ASAnalysisVisitor extends QVTiAnalysisVisitor 
		implements CS2ASCGModelVisitor<Object> {

		public CS2ASAnalysisVisitor(QVTiAnalyzer analyzer) {
			super(analyzer);
		}

		@Override
		@Nullable
		public Object visitCGLookupCallExp(CGLookupCallExp object) {
			return visitCGOperationCallExp(object);
		}
	}
	
	protected static class CS2ASBoxingAnalyser extends QVTiBoxingAnalyzer
		implements CS2ASCGModelVisitor<Object> {
		
		public CS2ASBoxingAnalyser(QVTiAnalyzer analyzer) {
			super(analyzer);
		}

		@Override
		@Nullable
		public Object visitCGLookupCallExp(CGLookupCallExp object) {
			return visitCGOperationCallExp(object);
		}
	}
	
	protected static class CS2ASFieldingAnalyser extends QVTiFieldingAnalyzer {
	
		protected static class CS2ASAnalysisVisitor extends QVTiFieldingAnalyzer.QVTiAnalysisVisitor
		implements CS2ASCGModelVisitor<Set<CGVariable>> {
			public CS2ASAnalysisVisitor(QVTiFieldingAnalyzer context) {
				super(context);
			}

			@Override
			@Nullable
			public Set<CGVariable> visitCGLookupCallExp(CGLookupCallExp object) {
				return visitCGOperationCallExp(object);
			}
		}
		
		protected static class CS2ASRewriteVisitor extends QVTiFieldingAnalyzer.QVTiRewriteVisitor
		implements CS2ASCGModelVisitor<Boolean> {
			public CS2ASRewriteVisitor(@NonNull QVTiAnalyzer context, @NonNull Set<CGVariable> caughtVariable) {
				super(context, caughtVariable);
			}

			@Override
			@Nullable
			public Boolean visitCGLookupCallExp(CGLookupCallExp object) {
				return visitCGOperationCallExp(object);
			}
		}
		
		
		
		public CS2ASFieldingAnalyser(QVTiAnalyzer analyzer) {
			super(analyzer);
		}
	
		@Override
		protected @NonNull AnalysisVisitor createAnalysisVisitor() {
			return new CS2ASAnalysisVisitor(this);
		}
		
		@Override
		protected @NonNull RewriteVisitor createRewriteVisitor(@NonNull Set<CGVariable> caughtVariables) {
			return new CS2ASRewriteVisitor((QVTiAnalyzer)analyzer, caughtVariables);
		}
	}

	
	protected static class CS2ASCG2JavaPreVisitor extends QVTiCG2JavaPreVisitor
		implements CS2ASCGModelVisitor<Object> {
		
		public CS2ASCG2JavaPreVisitor(QVTiGlobalContext globalContext) {
			super(globalContext);
		}

		@Override
		@Nullable
		public Object visitCGLookupCallExp(CGLookupCallExp object) {
			return visitCGOperationCallExp(object);
		}
	}
	
	protected static class CS2ASReferencesVisitor extends QVTiReferencesVisitor
		implements CS2ASCGModelVisitor<List<Object>> {
	
		protected static CS2ASReferencesVisitor INSTANCE = new CS2ASReferencesVisitor(new Object());
		
		public CS2ASReferencesVisitor(Object context) {
			super(context);
		}
	
		@Override
		@Nullable
		public List<Object> visitCGLookupCallExp(CGLookupCallExp object) {
			return visitCGOperationCallExp(object);
		}
	}
	
	protected static class CS2ASDependencyVisitor extends QVTiDependencyVisitor
		implements CS2ASCGModelVisitor<Object> {
	
		public CS2ASDependencyVisitor(QVTiAnalyzer analyzer, QVTiGlobalContext globalContext, GlobalPlace globalPlace) {
			super(analyzer, globalContext, globalPlace);
		}

		@Override
		@Nullable
		public Object visitCGLookupCallExp(CGLookupCallExp object) {
			return visitCGOperationCallExp(object);
		}
	}
	
	// Copied from QVTiCompilerTest
	@SuppressWarnings("unchecked")
	protected Class<? extends CS2ASTransformation> compileTransformation(@NonNull QVTiCodeGenerator cg) throws Exception {
		String qualifiedName = cg.getQualifiedName();
		String javaCodeSource = cg.generateClassFile();
		
		Class<?> txClass = OCL2JavaFileObject.loadClass(qualifiedName, javaCodeSource);
		return (Class<? extends CS2ASTransformation>) txClass;
		
	}
	
	
	// Copied from QVTiCompilerTest
	@Override
	public Class<? extends CS2ASTransformation> compileTransformation(@NonNull QVTimperative qvt,
			@NonNull Transformation transformation,	@NonNull CS2ASJavaCompilerParameters params) throws Exception {
				
		QVTiCodeGenerator cg = new CS2ASJavaCodeGenerator(qvt.getEnvironmentFactory(), transformation, params);
		QVTiCodeGenOptions options = cg.getOptions();
		options.setPackagePrefix(params.getPackageName());
		cg.generateClassFile();
		String savePath = params.getSavePath();
		if (savePath != null) {
			cg.saveSourceFile(savePath);
		}
		return compileTransformation(cg);
	}
}

Back to the top