1 package org.simantics.scl.compiler.elaboration.chr;
3 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
4 import org.simantics.scl.compiler.elaboration.chr.relations.CHRConstraint;
5 import org.simantics.scl.compiler.elaboration.chr.relations.ExternalCHRRelation;
6 import org.simantics.scl.compiler.elaboration.chr.relations.SpecialCHRRelation;
7 import org.simantics.scl.compiler.elaboration.chr.relations.UnresolvedCHRRelation;
8 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
9 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
10 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
11 import org.simantics.scl.compiler.elaboration.expressions.Expression;
12 import org.simantics.scl.compiler.elaboration.expressions.Variable;
13 import org.simantics.scl.compiler.elaboration.expressions.VariableProcedure;
14 import org.simantics.scl.compiler.elaboration.expressions.printing.ExpressionToStringVisitor;
15 import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
16 import org.simantics.scl.compiler.errors.Locations;
17 import org.simantics.scl.compiler.internal.parsing.Symbol;
18 import org.simantics.scl.compiler.types.TVar;
19 import org.simantics.scl.compiler.types.Type;
20 import org.simantics.scl.compiler.types.Types;
21 import org.simantics.scl.compiler.types.kinds.Kinds;
23 import gnu.trove.map.hash.TObjectIntHashMap;
24 import gnu.trove.set.hash.THashSet;
25 import gnu.trove.set.hash.TIntHashSet;
27 public class CHRLiteral extends Symbol {
29 public CHRRelation relation;
30 public Type[] typeParameters;
31 public Expression[] parameters;
32 public boolean killAfterMatch;
33 public boolean negated;
34 public boolean passive = true;
36 public CHRLiteral(long location, CHRRelation relation, Expression[] parameters, boolean remove, boolean negated) {
37 this.location = location;
38 this.relation = relation;
39 this.parameters = parameters;
40 this.killAfterMatch = remove;
41 this.negated = negated;
44 public void resolve(TranslationContext context) {
45 if(relation instanceof UnresolvedCHRRelation) {
46 UnresolvedCHRRelation unresolved = (UnresolvedCHRRelation)relation;
47 CHRConstraint constraint = context.resolveCHRConstraint(unresolved.name);
48 if(constraint != null) {
49 relation = constraint;
53 SCLRelation sclRelation = context.resolveRelation(unresolved.location, unresolved.name);
54 if(sclRelation != null)
55 relation = new ExternalCHRRelation(sclRelation);
57 Type[] parameterTypes = new Type[parameters.length];
58 for(int i=0;i<parameterTypes.length;++i)
59 parameterTypes[i] = Types.metaVar(Kinds.STAR);
60 constraint = new CHRConstraint(location, unresolved.name, parameterTypes);
61 constraint.implicitlyDeclared = true;
62 context.newCHRConstraint(constraint.name, constraint);
63 relation = constraint;
65 //context.getErrorLog().log(unresolved.location, "Couldn't resolve constraint " + unresolved.name + ".");
69 for(int i=0;i<parameters.length;++i)
70 parameters[i] = parameters[i].resolve(context);
73 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
74 for(Expression parameter : parameters)
75 parameter.collectRefs(allRefs, refs);
78 public void checkType(TypingContext context) {
79 if(relation == SpecialCHRRelation.EXECUTE) {
80 if(parameters.length != 1)
81 throw new InternalCompilerError("Wrong number of parameters for EXECUTE constraint.");
82 parameters[0] = parameters[0].checkIgnoredType(context);
85 TVar[] typeVariables = relation.getTypeVariables();
86 typeParameters = typeVariables.length == 0 ? Type.EMPTY_ARRAY : new Type[typeVariables.length];
87 for(int i=0;i<typeVariables.length;++i)
88 typeParameters[i] = Types.metaVar(typeVariables[i].getKind());
89 Type[] parameterTypes = Types.replace(relation.getParameterTypes(), typeVariables, typeParameters);
90 if(parameterTypes.length != parameters.length)
91 context.getErrorLog().log(location, "Constraint is applied with wrong number of parameters");
93 for(int i=0;i<parameters.length;++i)
94 parameters[i] = parameters[i].checkType(context, parameterTypes[i]);
98 public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
99 for(Expression parameter : parameters)
100 parameter.collectVars(allVars, vars);
103 public void forVariables(VariableProcedure procedure) {
104 for(Expression parameter : parameters)
105 parameter.forVariables(procedure);
108 public void collectFreeVariables(THashSet<Variable> vars) {
109 for(Expression parameter : parameters)
110 parameter.collectFreeVariables(vars);
113 public void setLocationDeep(long loc) {
114 if(location == Locations.NO_LOCATION) {
116 for(Expression parameter : parameters)
117 parameter.setLocationDeep(loc);
121 public void simplify(SimplificationContext context) {
122 for(int i=0;i<parameters.length;++i)
123 parameters[i] = parameters[i].simplify(context);
126 public String toString() {
127 StringBuilder b = new StringBuilder();
128 ExpressionToStringVisitor visitor = new ExpressionToStringVisitor(b);
133 public void collectQueryEffects(THashSet<Type> effects) {
136 public void collectEnforceEffects(THashSet<Type> effects) {