Skip to main content
summaryrefslogtreecommitdiffstats
blob: d0eaabbfe306c833561a8d2edffc91d300cc9052 (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
/**
 * <copyright> 
 *
 * Copyright (c) 2002-2007 Kolbware 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: 
 *   Kolbware, Bernd Kolb - Initial API and implementation
 *
 * </copyright>
 *
 */
package org.eclipse.xtend.middleend.internal.xpand3;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.xand3.analyzation.AnalyzeContext;
import org.eclipse.xpand3.SyntaxElement;
import org.eclipse.xpand3.expression.AbstractExpression;
import org.eclipse.xpand3.expression.BooleanLiteral;
import org.eclipse.xpand3.expression.Case;
import org.eclipse.xpand3.expression.Cast;
import org.eclipse.xpand3.expression.ChainExpression;
import org.eclipse.xpand3.expression.CollectionExpression;
import org.eclipse.xpand3.expression.ConstructorCallExpression;
import org.eclipse.xpand3.expression.FeatureCall;
import org.eclipse.xpand3.expression.GlobalVarExpression;
import org.eclipse.xpand3.expression.IfExpression;
import org.eclipse.xpand3.expression.IntegerLiteral;
import org.eclipse.xpand3.expression.LetExpression;
import org.eclipse.xpand3.expression.ListLiteral;
import org.eclipse.xpand3.expression.Literal;
import org.eclipse.xpand3.expression.NullLiteral;
import org.eclipse.xpand3.expression.OperationCall;
import org.eclipse.xpand3.expression.RealLiteral;
import org.eclipse.xpand3.expression.StringLiteral;
import org.eclipse.xpand3.expression.SwitchExpression;
import org.eclipse.xpand3.expression.TypeSelectExpression;
import org.eclipse.xpand3.staticTypesystem.AbstractTypeReference;
import org.eclipse.xtend.backend.common.ExpressionBase;
import org.eclipse.xtend.backend.common.SourcePos;
import org.eclipse.xtend.backend.common.StaticProperty;
import org.eclipse.xtend.backend.expr.CreateUncachedExpression;
import org.eclipse.xtend.backend.expr.HidingLocalVarDefExpression;
import org.eclipse.xtend.backend.expr.InitClosureExpression;
import org.eclipse.xtend.backend.expr.InvocationOnCollectionExpression;
import org.eclipse.xtend.backend.expr.InvocationOnObjectExpression;
import org.eclipse.xtend.backend.expr.InvocationOnWhateverExpression;
import org.eclipse.xtend.backend.expr.ListLiteralExpression;
import org.eclipse.xtend.backend.expr.LiteralExpression;
import org.eclipse.xtend.backend.expr.LocalVarEvalExpression;
import org.eclipse.xtend.backend.expr.NewLocalVarDefExpression;
import org.eclipse.xtend.backend.expr.PropertyOnCollectionExpression;
import org.eclipse.xtend.backend.expr.PropertyOnObjectExpression;
import org.eclipse.xtend.backend.expr.PropertyOnWhateverExpression;
import org.eclipse.xtend.backend.expr.SequenceExpression;
import org.eclipse.xtend.backend.syslib.SysLibNames;
import org.eclipse.xtend.backend.types.builtin.CollectionType;
import org.eclipse.xtend.backend.types.builtin.ObjectType;
import org.eclipse.xtend.backend.util.CollectionHelper;
import org.eclipse.xtend.backend.util.Pair;

/**
 * @author Bernd Kolb
 * 
 */
public class XtendFrontendASTConverter {

	private String extensionName;
	private final BackendTypeConverter typeConverter;
	private AnalyzeContext ctx;

	public XtendFrontendASTConverter(AnalyzeContext ctx, BackendTypeConverter typeConverter, String extensionName) {
		this.ctx = ctx;
		this.typeConverter = typeConverter;
		this.extensionName = extensionName;
	}

	public ExpressionBase convert(SyntaxElement ele) {
		throw new UnsupportedOperationException("Call for abstract class 'SyntaxElement'");
	}

	public ExpressionBase convert(Literal ele) {
		throw new UnsupportedOperationException("Call for abstract class 'Literal'");
	}

	public ExpressionBase convert(BooleanLiteral lit) {
		return new LiteralExpression("true".equals(lit.getLiteralValue().getValue()), getSourcePos(lit));
	}

	public ExpressionBase convert(IntegerLiteral lit) {
		return new LiteralExpression(new Long(lit.getLiteralValue().getValue()), getSourcePos(lit));
	}

	public ExpressionBase convert(RealLiteral lit) {
		return new LiteralExpression(new Double(lit.getLiteralValue().getValue()), getSourcePos(lit));
	}

	public ExpressionBase convert(NullLiteral lit) {
		return new LiteralExpression(null, getSourcePos(lit));
	}

	public ExpressionBase convert(ListLiteral lit) {
		final List<ExpressionBase> inner = new ArrayList<ExpressionBase>();

		for (AbstractExpression e : lit.getElements())
			inner.add(convert(e));

		return new ListLiteralExpression(inner, getSourcePos(lit));
	}

	public ExpressionBase convert(StringLiteral lit) {
		return new LiteralExpression(lit.getLiteralValue().getValue(), getSourcePos(lit));
	}

	public ExpressionBase convert(ChainExpression expr) {
		return new SequenceExpression(getInner(expr), getSourcePos(expr));
	}

	public ExpressionBase convert(Cast expr) {
		return convert(expr.getTarget());
	}

	public ExpressionBase convert(ConstructorCallExpression expr) {
		return new CreateUncachedExpression(typeConverter.convertToBackendType(expr.getType()), getSourcePos(expr));
	}

	public ExpressionBase convert(FeatureCall expr) {
		final SourcePos sourcePos = getSourcePos(expr);

		if (expr.getTarget() == null) {
			// 1. check for a static property
			final StaticProperty staticProp = typeConverter.getEnumLiteral(expr.getName());
			if (staticProp != null)
				return new LiteralExpression(staticProp.get(), sourcePos);

			// 2. check for a local variable
			if (ctx.getLocalVars().containsKey(expr.getName().getValue()))
				return new LocalVarEvalExpression(expr.getName().getValue(), sourcePos);

			// 3. check for a type literal
			try {
				return new LiteralExpression(typeConverter.convertToBackendType(expr.getName()), sourcePos);
			} catch (IllegalArgumentException exc) {
			} // do nothing - this means
			// it is not a type literal

			// 4. check for "this"
			if (ctx.hasThis()) {
				final ExpressionBase thisExpr = new LocalVarEvalExpression(
						org.eclipse.xtend.backend.common.SyntaxConstants.THIS, sourcePos);
				return createPropertyExpression(thisExpr, ctx.getThis(), expr.getName().getValue(), sourcePos);
			}

			throw new IllegalArgumentException("feature call " + expr.toString() + " does not match any feature: "
					+ sourcePos);
		} else {
			// evaluate the target and evaluate the property on the result
			return createPropertyExpression(convert(expr.getTarget()), analyze(expr.getTarget()), expr.getName()
					.getValue(), sourcePos);
		}
	}

	public ExpressionBase convert(CollectionExpression expr) {
		final SourcePos sourcePos = getSourcePos(expr);

		final String functionName = expr.getName().getValue();

		final AnalyzeContext oldCtx = ctx;
		ctx = ctx.cloneWithVariable(expr.getEleName(), null /*
		 * TODO ObjectType
		 */);
		final ExpressionBase bodyExpr = convert(expr.getClosure());
		ctx = oldCtx;

		final InitClosureExpression closureExpr = new InitClosureExpression(
				Arrays.asList(expr.getEleName().getValue()), Arrays.asList(ObjectType.INSTANCE), bodyExpr, sourcePos);

		if (expr.getTarget() == null) {
			if (!ctx.hasThis())
				throw new IllegalStateException(functionName + " with neither a target nor an implicit 'this'");

			final ExpressionBase thisExpr = new LocalVarEvalExpression(
					org.eclipse.xtend.backend.common.SyntaxConstants.THIS, sourcePos);
			return new InvocationOnObjectExpression(functionName, Arrays.asList(thisExpr, closureExpr), true, sourcePos);
		} else
			return new InvocationOnObjectExpression(functionName,
					Arrays.asList(convert(expr.getTarget()), closureExpr), true, sourcePos);
	}

	public ExpressionBase convert(TypeSelectExpression expr) {
		final SourcePos sourcePos = getSourcePos(expr);

		final AbstractExpression t = ctx.getTypeForName(expr.getTypeLiteral());
		final ExpressionBase typeExpr = new LiteralExpression(typeConverter.convertToBackendType(t), sourcePos);

		if (expr.getTarget() == null) {
			if (!ctx.hasThis())
				throw new IllegalStateException("typeSelect with neither a target nor an implicit 'this'");

			final ExpressionBase thisExpr = new LocalVarEvalExpression(
					org.eclipse.xtend.backend.common.SyntaxConstants.THIS, sourcePos);
			return new InvocationOnObjectExpression(SysLibNames.TYPE_SELECT, Arrays.asList(thisExpr, typeExpr), true,
					sourcePos);
		} else
			return new InvocationOnObjectExpression(SysLibNames.TYPE_SELECT, Arrays.asList(convert(expr.getTarget()),
					typeExpr), false, sourcePos);
	}

	public ExpressionBase convert(OperationCall expr) {
		final SourcePos sourcePos = getSourcePos(expr);
		final String functionName = transformFunctionName(expr.getName().getValue());

		final List<ExpressionBase> params = new ArrayList<ExpressionBase>();
		for (AbstractExpression e : expr.getParams())
			params.add(convert(e));

		final List<AbstractTypeReference> paramTypes = new ArrayList<AbstractTypeReference>();
		for (AbstractExpression e : expr.getParams())
			paramTypes.add(analyze(e));

		if (expr.getTarget() == null) {
			if (ctx.hasThis()) {
				// if a function matches directly (i.e. without implicitly
				// passing 'this' as a first parameter), that
				// has precedence in matching
				if (hasMatchingOperationCall(functionName, paramTypes.toArray(new AbstractTypeReference[0])))
					return new InvocationOnObjectExpression(functionName, params, false, sourcePos);
				else {
					final ExpressionBase thisExpression = new LocalVarEvalExpression(
							org.eclipse.xtend.backend.common.SyntaxConstants.THIS, sourcePos);
					final AbstractTypeReference thisType = ctx.getVariable(AnalyzeContext.IMPLICIT_VARIABLE);
					return createInvocationOnTargetExpression(functionName, thisExpression, thisType, params,
							paramTypes, true, sourcePos);
				}
			} else
				return new InvocationOnObjectExpression(functionName, params, false, sourcePos);
		} else
			return createInvocationOnTargetExpression(functionName, convert(expr.getTarget()),
					analyze(expr.getTarget()), params, paramTypes, true, sourcePos);
	}

	public ExpressionBase convert(GlobalVarExpression expr) {
		// TODO: To be impl
		throw new UnsupportedOperationException("Not yet implemented");
	}

	public ExpressionBase convert(IfExpression expr) {
		final ExpressionBase elseExpr = (expr.getElsePart() != null) ? convert(expr.getElsePart())
				: new LiteralExpression(null, getSourcePos(expr));

		return new org.eclipse.xtend.backend.expr.IfExpression(convert(expr.getCondition()),
				convert(expr.getThenPart()), elseExpr, getSourcePos(expr));
	}

	public ExpressionBase convert(LetExpression expr) {
		final ExpressionBase varExpr = convert(expr.getVarExpression());
		final AbstractTypeReference varType = analyze(expr.getVarExpression());

		final AnalyzeContext oldCtx = ctx;
		ctx = ctx.cloneWithVariable(expr.getVarName(), varType);

		try {
			if (oldCtx.getLocalVars().containsKey(expr.getVarName()))
				return new HidingLocalVarDefExpression(expr.getVarName().getValue(), varExpr, convert(expr
						.getTargetExpression()), getSourcePos(expr));
			else
				return new NewLocalVarDefExpression(expr.getVarName().getValue(), varExpr, convert(expr
						.getTargetExpression()), getSourcePos(expr));
		} finally {
			ctx = oldCtx;
		}
	}

	public ExpressionBase convert(SwitchExpression expr) {
		final List<Pair<ExpressionBase, ExpressionBase>> cases = new ArrayList<Pair<ExpressionBase, ExpressionBase>>();
		for (Case c : expr.getCases())
			cases.add(new Pair<ExpressionBase, ExpressionBase>(convert(c.getCondition()), convert(c.getThenPart())));

		return new org.eclipse.xtend.backend.expr.SwitchExpression(convert(expr.getSwitchExpr()), cases, convert(expr
				.getDefaultExpr()), getSourcePos(expr));
	}

	/**
	 * transform built-in operator names from the old to the new special names
	 */
	private String transformFunctionName(String functionName) {
		if ("+".equals(functionName))
			return SysLibNames.OPERATOR_PLUS;
		if ("-".equals(functionName))
			return SysLibNames.OPERATOR_MINUS;
		if ("*".equals(functionName))
			return SysLibNames.OPERATOR_MULT;
		if ("/".equals(functionName))
			return SysLibNames.OPERATOR_DIV;
		if ("%".equals(functionName))
			return SysLibNames.OPERATOR_MOD;

		if ("==".equals(functionName))
			return SysLibNames.OPERATOR_EQUALS;
		if ("!=".equals(functionName))
			return SysLibNames.OPERATOR_NOT_EQUALS;
		if ("<".equals(functionName))
			return SysLibNames.OPERATOR_LESS;
		if ("<=".equals(functionName))
			return SysLibNames.OPERATOR_LESS_OR_EQUALS;
		if (">=".equals(functionName))
			return SysLibNames.OPERATOR_GREATER_OR_EQUALS;
		if (">".equals(functionName))
			return SysLibNames.OPERATOR_GREATER;

		if ("!".equals(functionName))
			return SysLibNames.OPERATOR_NOT;

		if ("subString".equals(functionName))
			return SysLibNames.SUBSTRING;
		if ("replaceAll".equals(functionName))
			return SysLibNames.REPLACE_ALL_REGEX;

		return functionName;
	}

	private AbstractTypeReference analyze(AbstractExpression target) {
		// TODO: To be impl
		throw new UnsupportedOperationException("Not yet implemented");
	}

	private ExpressionBase createInvocationOnTargetExpression(String functionName, ExpressionBase targetExpression,
			AbstractTypeReference targetType, List<ExpressionBase> params, List<AbstractTypeReference> paramTypes,
			boolean isMethodStyle, SourcePos sourcePos) {
		final List<ExpressionBase> paramsWithoutFirst = params;
		final List<ExpressionBase> allParams = new ArrayList<ExpressionBase>();
		allParams.add(targetExpression);
		allParams.addAll(params);

		if (isCollectionType(targetType)) {
			paramTypes.add(0, targetType);
			final AbstractTypeReference[] paramTypeArray = paramTypes.toArray(new AbstractTypeReference[0]);

			if (hasMatchingOperationCall(functionName, paramTypeArray))
				// check if there is a function that directly matches the
				// collection
				return new InvocationOnObjectExpression(functionName, allParams, true, sourcePos);
			else
				// otherwise, do a 'collect' and call the function on all
				// elements of the collection
				return new InvocationOnCollectionExpression(targetExpression, functionName, paramsWithoutFirst,
						sourcePos);
		}

		if (isObjectType(targetType))
			// if the static type is "Object", we do not know if it is a
			// collection, so we do the logic at runtime
			return new InvocationOnWhateverExpression(functionName, allParams, isMethodStyle, sourcePos);

		// otherwise we know that it is not a collection and can avoid repeating
		// this logic at runtime
		return new InvocationOnObjectExpression(functionName, allParams, true, sourcePos);
	}

	private boolean hasMatchingOperationCall(String functionName, AbstractTypeReference[] paramTypes) {
		if (ctx.getExtensionForTypes(functionName, paramTypes) != null)
			return true;

		if (paramTypes.length == 0)
			return false;

		final AbstractTypeReference target = paramTypes[0];
		return ctx.getOperationFor(target, functionName, CollectionHelper.withoutFirst(paramTypes)) != null;
	}

	private ExpressionBase createPropertyExpression(ExpressionBase target, Object type, String varName,
			SourcePos sourcePos) {
		if (isCollectionType(type)) {
			if (CollectionType.INSTANCE.getProperties().keySet().contains(varName))
				return new PropertyOnObjectExpression(target, varName, sourcePos);
			else
				return new PropertyOnCollectionExpression(target, varName, sourcePos);
		}

		if (isObjectType(type))
			return new PropertyOnWhateverExpression(target, varName, sourcePos);

		return new PropertyOnObjectExpression(target, varName, sourcePos);
	}

	private boolean isObjectType(Object t) {
		// TODO : To be impl
		return true;
		// throw new UnsupportedOperationException("Not yet implemented");
	}

	private boolean isCollectionType(Object t) {
		// TODO : To be impl
		return false;
		// throw new UnsupportedOperationException("Not yet implemented");
	}

	/**
	 * extract the inner expressions as a "flat" list - they are stored as a
	 * binary tree in the ChainExpression...
	 */
	private List<ExpressionBase> getInner(ChainExpression expr) {
		final List<ExpressionBase> result = new ArrayList<ExpressionBase>();

		if (expr.getFirst() instanceof ChainExpression)
			result.addAll(getInner((ChainExpression) expr.getFirst()));
		else
			result.add(convert(expr.getFirst()));

		if (expr.getNext() instanceof ChainExpression)
			result.addAll(getInner((ChainExpression) expr.getNext()));
		else
			result.add(convert(expr.getNext()));

		return result;
	}

	private SourcePos getSourcePos(SyntaxElement lit) {
		return new SourcePos(lit.getFileName(), extensionName, lit.getLine());
	}

}

Back to the top