1 package org.simantics.scl.compiler.elaboration.expressions;
3 import java.util.ArrayList;
5 import org.simantics.scl.compiler.common.names.Name;
6 import org.simantics.scl.compiler.common.names.Names;
7 import org.simantics.scl.compiler.compilation.CompilationContext;
8 import org.simantics.scl.compiler.constants.NoRepConstant;
9 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
10 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
11 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
12 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
13 import org.simantics.scl.compiler.elaboration.errors.NotPatternException;
14 import org.simantics.scl.compiler.elaboration.expressions.lhstype.LhsType;
15 import org.simantics.scl.compiler.elaboration.expressions.lhstype.PatternMatchingLhs;
16 import org.simantics.scl.compiler.elaboration.java.ListConstructor;
17 import org.simantics.scl.compiler.elaboration.macros.MacroRule;
18 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
19 import org.simantics.scl.compiler.errors.Locations;
20 import org.simantics.scl.compiler.internal.codegen.references.IVal;
21 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
22 import org.simantics.scl.compiler.internal.interpreted.IApply;
23 import org.simantics.scl.compiler.internal.interpreted.IExpression;
24 import org.simantics.scl.compiler.internal.interpreted.IListLiteral;
25 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
26 import org.simantics.scl.compiler.types.Skeletons;
27 import org.simantics.scl.compiler.types.TFun;
28 import org.simantics.scl.compiler.types.Type;
29 import org.simantics.scl.compiler.types.Types;
30 import org.simantics.scl.compiler.types.exceptions.MatchException;
31 import org.simantics.scl.compiler.types.exceptions.UnificationException;
32 import org.simantics.scl.compiler.types.kinds.Kinds;
33 import org.simantics.scl.compiler.types.util.MultiFunction;
35 import gnu.trove.map.hash.TObjectIntHashMap;
36 import gnu.trove.set.hash.TIntHashSet;
38 public class EApply extends Expression {
39 public Expression function;
40 public Expression[] parameters;
41 public Type effect = Types.NO_EFFECTS;
43 public EApply(Expression function, Expression ... parameters) {
44 this.function = function;
45 this.parameters = parameters;
48 public EApply(Expression function, Expression parameter) {
49 this(function, new Expression[] {parameter});
52 public EApply(long loc, Expression function, Expression ... parameters) {
54 this.function = function;
55 this.parameters = parameters;
58 public EApply(long loc, Type effect, Expression function, Expression ... parameters) {
61 this.function = function;
62 this.parameters = parameters;
65 public void set(Expression function, Expression[] parameters) {
66 this.function = function;
67 this.parameters = parameters;
70 public Expression getFunction() {
74 public Expression[] getParameters() {
78 public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
79 function.collectVars(allVars, vars);
80 for(Expression parameter : parameters)
81 parameter.collectVars(allVars, vars);
85 protected void updateType() throws MatchException {
86 MultiFunction mfun = Types.matchFunction(function.getType(), parameters.length);
87 /*for(int i=0;i<parameters.length;++i)
88 if(!Types.equals(parameters[i].getType(), mfun.parameterTypes[i]))
89 throw new MatchException();*/
91 setType(mfun.returnType);
95 public IVal toVal(CompilationContext context, CodeWriter w) {
96 IVal functionVal = function.toVal(context, w);
97 IVal[] parameterVals = new IVal[parameters.length];
98 for(int i=0;i<parameters.length;++i)
99 parameterVals[i] = parameters[i].toVal(context, w);
100 Type type = getType();
101 effect = Types.simplifyFinalEffect(effect);
102 return w.applyWithEffect(location, effect, type, functionVal, parameterVals);
105 private void combineApplications() {
106 if(function instanceof EApply) {
107 EApply apply = (EApply)function;
108 if(Types.canonical(apply.effect) == Types.NO_EFFECTS) {
109 function = apply.function;
110 parameters = Expression.concat(apply.parameters, parameters);
116 public Expression simplify(SimplificationContext context) {
117 function = function.simplify(context);
118 for(int i=0;i<parameters.length;++i)
119 parameters[i] = parameters[i].simplify(context);
120 combineApplications();
122 // Try to apply macro rule
123 if(function instanceof EConstant) {
124 EConstant constant = (EConstant)function;
125 MacroRule rule = constant.value.getMacroRule();
127 Expression simplified = rule.apply(context, constant.typeParameters, this);
128 if(simplified != null)
129 // There may be more to simplify after macro application
130 // However this may cause performance problems (O(n^2) algorithm in pathologic cases)
131 return simplified.simplify(context);
139 public EVar getPatternHead() throws NotPatternException {
140 return function.getPatternHead();
144 public LhsType getLhsType() throws NotPatternException {
145 LhsType lhsType = function.getLhsType();
146 if(lhsType instanceof PatternMatchingLhs)
147 for(Expression parameter : parameters)
148 parameter.collectVariableNames((PatternMatchingLhs)lhsType);
153 public Expression resolve(TranslationContext context) {
154 function = function.resolve(context);
155 for(int i=0;i<parameters.length;++i)
156 parameters[i] = parameters[i].resolve(context);
157 //combineApplications();
162 public Expression resolveAsPattern(TranslationContext context) {
163 function = function.resolveAsPattern(context);
164 for(int i=0;i<parameters.length;++i)
165 parameters[i] = parameters[i].resolveAsPattern(context);
166 combineApplications();
167 if(!(function instanceof EConstant || function instanceof EError)) {
168 context.getErrorLog().log(location, "Only constants can be applied in patterns.");
175 public void getParameters(TranslationContext context,
176 ArrayList<Expression> parameters) {
177 function.getParameters(context, parameters);
178 for(Expression parameter : this.parameters)
179 parameters.add(parameter);
183 public Expression replace(ReplaceContext context) {
186 effect.replace(context.tvarMap),
187 function.replace(context),
188 replace(context, parameters));
192 public void setLocationDeep(long loc) {
193 if(location == Locations.NO_LOCATION) {
195 function.setLocationDeep(loc);
196 for(Expression parameter : parameters)
197 parameter.setLocationDeep(loc);
202 public int getFunctionDefinitionPatternArity() throws NotPatternException {
203 return function.getFunctionDefinitionPatternArity() + parameters.length;
207 public IExpression toIExpression(ExpressionInterpretationContext target) {
208 IExpression[] parametersI = toIExpressions(target, parameters);
210 Expression function = this.function;
211 while(function instanceof EApplyType)
212 function = ((EApplyType)function).expression;
215 if(function instanceof EConstant) {
216 SCLValue functionValue = ((EConstant)function).value;
217 Name name = functionValue.getName();
218 if(name.module.equals("Builtin")) {
219 IVal val = functionValue.getValue();
220 if(val instanceof ListConstructor) {
221 if(((ListConstructor)val).arity == parametersI.length)
222 return new IListLiteral(parametersI);
226 //System.out.println("--> " + function + " " + function.getClass().getSimpleName());
229 return new IApply(function.toIExpression(target), parametersI);
232 private void inferType(TypingContext context, boolean ignoreResult) {
233 function = function.inferType(context);
234 function = context.instantiate(function);
237 mfun = Types.unifyFunction(function.getType(), parameters.length);
238 } catch (UnificationException e) {
239 int arity = Types.getArity(function.getType());
241 context.getErrorLog().log(location, "Application of non-function.");
243 context.getErrorLog().log(location, "Function of arity " + arity +
244 " is applied with " + parameters.length + " parameters.");
245 setType(Types.metaVar(Kinds.STAR));
246 for(int i=0;i<parameters.length;++i)
247 parameters[i] = parameters[i].inferType(context);
250 if((ignoreResult && Skeletons.canonicalSkeleton(mfun.returnType) instanceof TFun &&
251 Types.canonical(mfun.effect) == Types.NO_EFFECTS) ||
252 (context.isInPattern() && Skeletons.canonicalSkeleton(mfun.returnType) instanceof TFun)) {
253 context.getErrorLog().log(location, "The function is applied with too few parameters.");
256 // Check parameter types
257 for(int i=0;i<parameters.length;++i)
258 parameters[i] = parameters[i].checkType(context, mfun.parameterTypes[i]);
260 effect = mfun.effect;
262 context.declareEffect(location, mfun.effect);
263 setType(mfun.returnType);
267 public Expression inferType(TypingContext context) {
268 if(parameters.length == 2 && function instanceof EConstant && ((EConstant)function).value.getName() == Names.Prelude_dollar)
269 return new EApply(location, parameters[0], parameters[1]).inferType(context);
270 inferType(context, false);
275 public Expression checkIgnoredType(TypingContext context) {
276 if(parameters.length == 2 && function instanceof EConstant && ((EConstant)function).value.getName() == Names.Prelude_dollar)
277 return new EApply(location, parameters[0], parameters[1]).inferType(context);
278 inferType(context, true);
279 if(Types.canonical(getType()) != Types.UNIT)
280 return new ESimpleLet(location, null, this, new ELiteral(NoRepConstant.PUNIT));
284 public Type getLocalEffect() {
288 public Expression toANormalForm(Expression root) {
289 Expression expression = root;
290 for(int i=parameters.length-1;i>=0;--i) {
291 Expression parameter = parameters[i];
292 if(parameter.isEffectful()) {
293 Variable var = new Variable("aNormalTemp" + i, parameter.getType());
294 expression = new ESimpleLet(var, parameter, expression);
295 parameters[i] = new EVariable(var);
298 if(function.isEffectful()) {
299 Variable var = new Variable("aNormalTempF", function.getType());
300 expression = new ESimpleLet(var, function, expression);
301 function = new EVariable(var);
307 public boolean isEffectful() {
308 if(effect != Types.NO_EFFECTS)
310 for(Expression parameter : parameters)
311 if(parameter.isEffectful())
313 if(function.isEffectful())
319 public boolean isFunctionPattern() {
320 return !isConstructorApplication();
324 public boolean isConstructorApplication() {
325 return function.isConstructorApplication();
329 public void accept(ExpressionVisitor visitor) {
334 public boolean isFunctionDefinitionLhs() {
336 EVar patternHead = function.getPatternHead();
337 return !Character.isUpperCase(patternHead.name.charAt(0));
338 } catch(NotPatternException e) {
344 public boolean isPattern(int arity) {
345 if(!function.isPattern(arity+parameters.length))
347 for(Expression parameter : parameters)
348 if(!parameter.isPattern(0))
354 public Expression accept(ExpressionTransformer transformer) {
355 return transformer.transform(this);
359 public boolean equalsExpression(Expression expression) {
360 if(expression.getClass() != getClass())
362 EApply other = (EApply)expression;
363 if(parameters.length != other.parameters.length)
365 if(!function.equalsExpression(other.function))
367 for(int i=0;i<parameters.length;++i)
368 if(!parameters[i].equalsExpression(other.parameters[i]))