Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7f99e6446cb0f7d4f71b507b63b861ea5e5c5302 (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
/*******************************************************************************
 * 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.xpand2.ast;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.internal.xpand2.model.XpandDefinition;
import org.eclipse.internal.xpand2.model.XpandResource;
import org.eclipse.internal.xtend.expression.ast.DeclaredParameter;
import org.eclipse.internal.xtend.expression.ast.Identifier;
import org.eclipse.internal.xtend.expression.ast.SyntaxElement;
import org.eclipse.xpand2.XpandExecutionContext;
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 abstract class AbstractDefinition extends SyntaxElement implements XpandDefinition {

	private Template owner = null;

	private DeclaredParameter[] params;

	private Identifier name;

	private Identifier type;

	private Statement[] body;

	private String _stringRepresentation = null;

	protected boolean wildParams = false;

	public AbstractDefinition(final Identifier name, final Identifier type, final DeclaredParameter[] params,
			final Statement[] body) {
		this.name = name;
		this.type = type;
		this.params = params;
		this.body = body;
	}

	public XpandResource getOwner() {
		return owner;
	}

	public void setOwner(final Template owner) {
		this.owner = owner;
	}

	public DeclaredParameter[] getParams() {
		return params;
	}

	public List<DeclaredParameter> getParamsAsList() {
		return Arrays.asList(params);
	}

	public Identifier getType() {
		return type;
	}

	public String getTargetType() {
		return type.getValue();
	}

	public Identifier getDefName() {
		return name;
	}

	public String getName() {
		return name.getValue();
	}

	public String getQualifiedName() {
		if (getFileName() != null) {
			String prefix = getFileName().replaceAll("/", "::");
			prefix = prefix.substring(0, prefix.length() - 4);
			return prefix + "::" + getName();
		}
		return getName();
	}

	public String getParamString(boolean typesOnly) {
		if (params == null || params.length == 0)
			return wildParams ? "(*)" : "";
		final StringBuffer buff = new StringBuffer("(");
		for (int i = 0; i < params.length; i++) {
			final DeclaredParameter p = params[i];
			buff.append(p.getType().getValue());
			if (!typesOnly) {
				buff.append(" ").append(p.getName().getValue());
			}
			if (i + 1 < params.length) {
				buff.append(",");
			}
		}
		if (wildParams) {
			buff.append(",*");
		}
		return buff.append(")").toString();
	}

	public Statement[] getBody() {
		return body;
	}

	public List<Statement> getBodyAsList() {
		return Arrays.asList(body);
	}

	public void analyze(XpandExecutionContext ctx, final Set<AnalysationIssue> issues) {
		try {
			if (ctx.getCallback() != null) {
				ctx.getCallback().pre(this, ctx);
			}
			final Type thisType = ctx.getTypeForName(getType().getValue());
			if (thisType == null) {
				issues.add(new AnalysationIssue(AnalysationIssue.TYPE_NOT_FOUND, "Couldn't find "
						+ getType().getValue(), getType()));
			}
			ctx = (XpandExecutionContext) ctx.cloneWithVariable(new Variable(ExecutionContext.IMPLICIT_VARIABLE,
					thisType));
			for (int i = 0; i < params.length; i++) {
				final DeclaredParameter param = params[i];
				Type paramType = ctx.getTypeForName(param.getType().getValue());
				if (paramType == null) {
					issues.add(new AnalysationIssue(AnalysationIssue.TYPE_NOT_FOUND, "Couldn't find "
							+ param.getType().getValue(), param.getType()));
					paramType = ctx.getObjectType();
				}
				final String name = param.getName().getValue();
				ctx = (XpandExecutionContext) ctx.cloneWithVariable(new Variable(name, paramType));
			}
			for (int i = 0; i < getBody().length; i++) {
				Statement stmt = getBody()[i];
				try {
					stmt.analyze(ctx, issues);
				}
				catch (RuntimeException ex) {
					Map<String, Object> info = new HashMap<String, Object>();
					info.put("body", stmt);
					ctx.handleRuntimeException(ex, this, info);
				}
			}
		}
		finally {
			if (ctx.getCallback() != null) {
				ctx.getCallback().post(null);
			}
		}
	}

	public void evaluate(XpandExecutionContext ctx) {
		try {
			if (ctx.getCallback() != null) {
				ctx.getCallback().pre(this, ctx);
			}
			ctx = (XpandExecutionContext) ctx.cloneWithResource(getOwner());
			for (int i = 0; i < getBody().length; i++) {
				Statement stmt = getBody()[i];
				try {
					stmt.evaluate(ctx);
				}
				catch (RuntimeException ex) {
					Map<String, Object> info = new HashMap<String, Object>();
					info.put("body", stmt);
					ctx.handleRuntimeException(ex, this, info);
				}
			}
		}
		finally {
			if (ctx.getCallback() != null) {
				ctx.getCallback().post(null);
			}
		}
	}

	@Override
	public String toString() {
		if (_stringRepresentation == null) {
			_stringRepresentation = name.getValue() + getParamString(false) + " : " + getType().getValue();
		}

		return _stringRepresentation;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((toString() == null) ? 0 : toString().hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;

		if (obj == null || toString() == null)
			return false;

		if (getClass() != obj.getClass())
			return false;

		AbstractDefinition other = (AbstractDefinition) obj;
		return toString().equals(other.toString());
	}
}

Back to the top