Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 83c28c007d17cda38867b9301ad805445aa69753 (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
/*******************************************************************************
 * Copyright (c) 2009, 2011 Borland Software Corporation 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:
 *     Borland Software Corporation - initial API and implementation
 *     Alex Paperno - bugs 416584
 *******************************************************************************/
package org.eclipse.m2m.internal.qvt.oml.compiler;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.common.util.UniqueEList;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EOperation;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.Diagnostician;
import org.eclipse.m2m.internal.qvt.oml.QvtMessage;
import org.eclipse.m2m.internal.qvt.oml.ast.binding.ASTBindingHelper;
import org.eclipse.m2m.internal.qvt.oml.ast.binding.IModuleSourceInfo;
import org.eclipse.m2m.internal.qvt.oml.ast.env.QVTOTypeResolver;
import org.eclipse.m2m.internal.qvt.oml.ast.env.QvtOperationalEnvFactory;
import org.eclipse.m2m.internal.qvt.oml.ast.env.QvtOperationalFileEnv;
import org.eclipse.m2m.internal.qvt.oml.ast.env.QvtOperationalModuleEnv;
import org.eclipse.m2m.internal.qvt.oml.ast.env.QvtOperationalStdLibrary;
import org.eclipse.m2m.internal.qvt.oml.ast.parser.ExecutableXMIHelper;
import org.eclipse.m2m.internal.qvt.oml.ast.parser.QvtOperationalParserUtil;
import org.eclipse.m2m.internal.qvt.oml.cst.CSTFactory;
import org.eclipse.m2m.internal.qvt.oml.cst.UnitCS;
import org.eclipse.m2m.internal.qvt.oml.expressions.ImperativeOperation;
import org.eclipse.m2m.internal.qvt.oml.expressions.Module;
import org.eclipse.m2m.internal.qvt.oml.expressions.ModuleImport;
import org.eclipse.ocl.Environment;
import org.eclipse.ocl.utilities.ASTNode;

public class CompiledUnit {
	
	private List<String> fQname;
	private URI fUri;	
	private List<QvtMessage> fAllProblems;
	private List<CompiledUnit> fImports;	
	private List<QvtOperationalModuleEnv> moduleEnvs;

	// FIXME - add compilationUnit CST element
	UnitCS fUnitCST;	
	
	CompiledUnit(List<String> qualifiedName, URI uri, List<? extends QvtOperationalModuleEnv> modules) {
		if(qualifiedName == null || modules == null || uri == null) {
			throw new IllegalArgumentException();
		}
		
		this.fUri = uri;
		this.fQname = qualifiedName;		
		this.moduleEnvs = new ArrayList<QvtOperationalModuleEnv>(modules);
		
		ArrayList<QvtMessage> problems = new ArrayList<QvtMessage>();
		this.fAllProblems = problems;
		
		for (QvtOperationalModuleEnv next : modules) {
			fAllProblems.addAll(next.getAllProblemMessages());
		}

		if(!problems.isEmpty()) {
			problems.trimToSize();			
		}
	}
	
	
	public static ResourceSetImpl createResourceSet() {
		return createResourceSet(null);
	}
	
	static ResourceSetImpl createResourceSet(EPackage.Registry registry) {		
		ResourceSetImpl rs = new ResourceSetImpl() {			
			@Override
			protected void demandLoad(Resource resource) throws java.io.IOException {
				super.demandLoad(resource);
				if(ExeXMISerializer.COMPILED_XMI_FILE_EXTENSION.equals(resource.getURI().fileExtension())) {
					ExecutableXMIHelper.fixResourceOnLoad(resource);
				}
			}
		};
		
		if(registry != null) {
			rs.setPackageRegistry(registry);
		}
		return rs;
	}

	public CompiledUnit(URI xmiURI, ResourceSet resourceSet) {
		this(resourceSet.getResource(xmiURI, true), new HashMap<URI, CompiledUnit>());
	}

	CompiledUnit(Resource unitXMIResource, Map<URI, CompiledUnit> unitMap) {
		this.fUri = unitXMIResource.getURI();
		this.moduleEnvs = new LinkedList<QvtOperationalModuleEnv>();
						
		this.fAllProblems = new ArrayList<QvtMessage>();
		this.fImports = new UniqueEList<CompiledUnit>();		

		unitMap.put(fUri, this);

		computeImports(unitXMIResource, unitMap);
		
		// Note: Environment initialization should be optional as it make sense to be used
		// only in case this unit is to be a compilation dependency to parsed CST Unit  
		for(EObject rootElement : new ArrayList<EObject>(unitXMIResource.getContents())) {
			if(rootElement instanceof Module) {
				Module nextModule = (Module) rootElement;
				QvtOperationalModuleEnv nextModuleEnv = QvtOperationalEnvFactory.INSTANCE.createModuleEnvironment(nextModule);
				QVTOTypeResolver typeResolver = nextModuleEnv.getTypeResolver();
 
				// FIXME - 
				// 1) workaround to make Environment available with the module for
				// non-transformation execution context
				// 2) move this initialization code to QVTEnvironment related classes
				ASTBindingHelper.createCST2ASTBinding(CSTFactory.eINSTANCE.createLibraryCS(), nextModule, nextModuleEnv);				
				for (EOperation nextOper : nextModule.getEOperations()) {
					if(nextOper instanceof ImperativeOperation) {
						ImperativeOperation imperativeOper = (ImperativeOperation) nextOper;
						EClassifier ctxType = QvtOperationalParserUtil.getContextualType(imperativeOper);
						if(ctxType != null) {
							typeResolver.resolveAdditionalOperation(ctxType, imperativeOper);
						}
					}
				}
				
				this.moduleEnvs.add(nextModuleEnv);
			}
		}

		//validate(unitXMIResource, this.fAllProblems);
	}

	/*
	 * TODO - EcoreValidator is quite strict concerning operation signatures etc, 
	 * causing a lot of errors. Next, override those validateXXX operations, eventually
	 * the OCLValidator if too strict for the QVTO purposes.
	 * Move to a separate validation related class
	 */
	@SuppressWarnings("unused")
	private void validate(Resource res, List<QvtMessage> problems) {
		BasicDiagnostic diagnostics = new BasicDiagnostic();
		IModuleSourceInfo sourceInfo = null;
		if(res instanceof ExeXMIResource) {
			sourceInfo = ((ExeXMIResource) res).getSourceInfo();
		}
		
		Diagnostician diagnostician = new Diagnostician();
		Map<Object, Object> context = new HashMap<Object, Object>();
		context.put(Environment.class, new QvtOperationalEnvFactory().createEnvironment());
		
		for (EObject root : res.getContents()) {
			diagnostician.validate(root, diagnostics, context);
			
			List<Diagnostic> children = diagnostics.getChildren();			
			if(!children.isEmpty()) {				
				for (Diagnostic diagnostic : children) {
					int resourceSeverity = diagnostic.getSeverity();
					// FIXME - support other severity levels in QVT, or use Diagnostic too?
					int messageSeverity = (resourceSeverity == Diagnostic.ERROR) ? 
							QvtMessage.SEVERITY_ERROR : QvtMessage.SEVERITY_WARNING;
					
					int offset = -1;
					int len = 0;
					List<?> data = diagnostic.getData();
					if(!data.isEmpty()) {
						Object obj = data.get(0);
						if(obj instanceof ASTNode) {
							ASTNode astNode = (ASTNode) obj;
							offset = astNode.getStartPosition();
							len = astNode.getEndPosition() - offset;
						}
					}
					
					int lineNum = -1;
					if(offset >= 0 && sourceInfo != null) {
						lineNum = sourceInfo.getLineNumberProvider().getLineNumber(offset);
					}
					
					QvtMessage problem = new QvtMessage(
							diagnostic.getMessage(), messageSeverity, offset,
							len, lineNum);
					addProblem(problem);
 				}
			}
		}
	}
	
	private void computeImports(Resource unitResource, Map<URI, CompiledUnit> unitMap) {
		for (EObject next : unitResource.getContents()) {
			if(next instanceof Module) {
				computeImports((Module) next, unitMap);
			}
		}
	}
	
	private void computeImports(Module module, Map<URI, CompiledUnit> unitMap) {
		EList<ModuleImport> imports = module.getModuleImport();		
		for (ModuleImport nextImport : imports) {
			Module importedModule = nextImport.getImportedModule();
			
			if(importedModule != null && importedModule.eResource() != null) {
				Resource importedResource = importedModule.eResource();
				URI importedResourceURI = importedResource.getURI();
				// Note: skip QVT Standard Library as it's imported implicitly
				if(!importedResourceURI.equals(QvtOperationalStdLibrary.NS_URI)) {
					CompiledUnit importedUnit = unitMap.get(importedResourceURI);
					if(importedUnit == null) {
						importedUnit = new CompiledUnit(importedResource, unitMap);
						unitMap.put(importedResourceURI, importedUnit);
					} 

					fImports.add(new CompiledUnit(importedResource, unitMap));					
				}
			} else {
				throw new IllegalArgumentException("imported module must be in a resource"); //$NON-NLS-1$ 
			}
		}
	}
	
	void addProblem(QvtMessage problem) {
		if(problem == null) {
			throw new IllegalArgumentException();
		}
		
		fAllProblems.add(problem);
	}
				
	void setImports(List<CompiledUnit> imports) {
		this.fImports = imports; 
	}
	
	public String getName() {
		return fQname.get(fQname.size() - 1);
	}
	
	public List<QvtOperationalModuleEnv> getModuleEnvironments() {
		if (moduleEnvs.size() == 1 && moduleEnvs.get(0) instanceof QvtOperationalFileEnv) {
			return ((QvtOperationalFileEnv) moduleEnvs.get(0)).getInnerEnvironments();
		}
		else {
			return moduleEnvs;
		}
	}
	
	public List<QvtMessage> getErrors() {
		List<QvtMessage> errors = new ArrayList<QvtMessage>();			
		for (QvtMessage nextMessage : fAllProblems) {
			if(nextMessage.getSeverity() == QvtMessage.SEVERITY_ERROR) {
				errors.add(nextMessage);
			}
		}			
		return errors;
	}
	
	public List<QvtMessage> getWarnings() {
		List<QvtMessage> warnings = new ArrayList<QvtMessage>();			
		for (QvtMessage nextMessage : fAllProblems) {
			if(nextMessage.getSeverity() == QvtMessage.SEVERITY_WARNING) {
				warnings.add(nextMessage);
			}
		}			
		return warnings;
	}
	
	
	public List<CompiledUnit> getCompiledImports() { 
		return fImports != null ? fImports : Collections.<CompiledUnit>emptyList();
	}
	
	public List<Module> getModules() {
		List<Module> modules = new ArrayList<Module>(moduleEnvs.size());
		for (QvtOperationalModuleEnv next : moduleEnvs) {
			if (next instanceof QvtOperationalFileEnv) {
				for (QvtOperationalModuleEnv inner : ((QvtOperationalFileEnv)next).getInnerEnvironments()) {
					if(inner.getModuleContextType() != null) {
						modules.add(inner.getModuleContextType());
					}
				}
			}
			if(next.getModuleContextType() != null) {
				modules.add(next.getModuleContextType());
			}
		}
		
		return modules;
	}
	
	public URI getURI() {
		return fUri;
	}
		
	public UnitCS getUnitCST() {
		return fUnitCST;
	}
		
	public List<QvtMessage> getProblems() {
		return fAllProblems;
	}		

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof CompiledUnit) {
			CompiledUnit another = (CompiledUnit) obj;
			return fUri.equals(another.fUri);
		}
		
		return super.equals(obj);
	}
	
	@Override
	public int hashCode() {	
		return fUri.hashCode();
	}
	
	@Override
	public String toString() {		
		return fUri.toString();
	}
}

Back to the top