Skip to main content
summaryrefslogtreecommitdiffstats
blob: 080c297ff3c5d6f266af8e7e7bc940d733150363 (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
/*******************************************************************************
 * Copyright (c) 2005, 2007 committers of openArchitectureWare 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:
 *     committers of openArchitectureWare - initial API and implementation
 *******************************************************************************/

package org.eclipse.internal.xtend.xtend.ast;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.Stack;

import org.eclipse.internal.xtend.expression.ast.DeclaredParameter;
import org.eclipse.internal.xtend.expression.ast.Expression;
import org.eclipse.internal.xtend.expression.ast.Identifier;
import org.eclipse.xtend.expression.AnalysationIssue;
import org.eclipse.xtend.expression.ExecutionContext;
import org.eclipse.xtend.expression.Variable;
import org.eclipse.xtend.typesystem.Type;

public class ExpressionExtensionStatement extends AbstractExtension {

	private Expression expression;

	public ExpressionExtensionStatement(final Identifier name,
			final Identifier returnType,
			final List<DeclaredParameter> formalParameters,
			final Expression expression, final boolean cached,
			final boolean isPrivate) {
		super(name, returnType, formalParameters, cached, isPrivate);
		this.expression = expression;
	}

	public Expression getExpression() {
		return expression;
	}

	private final Stack<List<Type>> analyzations = new Stack<List<Type>>();

	@Override
	public Object evaluateInternal(final Object[] parameters,
			final ExecutionContext ctx) {
		return evaluateInternal2(parameters, ctx);
	}

	protected Object evaluateInternal2(final Object[] parameters,
			ExecutionContext ctx) {
		ctx = ctx.cloneWithoutVariables();
		ctx = ctx.cloneWithResource(file);
		final List<String> paramNames = getParameterNames();
		for (int i = 0, x = paramNames.size(); i < x; i++) {
			final String name = paramNames.get(i);
			ctx = ctx.cloneWithVariable(new Variable(name, parameters[i]));
		}
		return expression.evaluate(ctx);
	}

	@Override
	public void analyzeInternal(final ExecutionContext ctx,
			final Set<AnalysationIssue> issues) {
		if (expression != null)
			expression.analyze(ctx, issues);
	}

	@Override
	protected Type internalGetReturnType(final Type[] parameters,
			final ExecutionContext ctx, final Set<AnalysationIssue> issues) {
		if (getReturnTypeIdentifier() != null)
			return ctx.getTypeForName(getReturnTypeIdentifier().getValue());
		if (parameters == null
				|| parameters.length != getParameterNames().size())
			return null;
		final List<Type> params = Arrays.asList(parameters);
		if (!analyzations.contains(params)) {
			analyzations.push(params);
			try {
				return analyzeInternal(parameters, ctx, issues);
			} finally {
				analyzations.pop();
			}
		} else {
			if (returnType == null) {
				issues
						.add(new AnalysationIssue(
								AnalysationIssue.INTERNAL_ERROR,
								"Recursive extensions need to have a return type specified!",
								this));
				return null;
			}
			return ctx.getTypeForName(returnType.getValue());
		}
	}

	protected Type analyzeInternal(final Type[] parameters,
			ExecutionContext ctx, final Set<AnalysationIssue> issues) {
		ctx = ctx.cloneWithoutVariables();
		ctx = ctx.cloneWithResource(file);
		final List<String> paramNames = getParameterNames();
		for (int i = 0, x = paramNames.size(); i < x; i++) {
			final String name = paramNames.get(i);
			final Type t = parameters[i];
			ctx = ctx.cloneWithVariable(new Variable(name, t));
		}
		return expression.analyze(ctx, issues);
	}
}

Back to the top