]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRLiteral.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / chr / CHRLiteral.java
1 package org.simantics.scl.compiler.elaboration.chr;
2
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;
22
23 import gnu.trove.map.hash.TObjectIntHashMap;
24 import gnu.trove.set.hash.THashSet;
25 import gnu.trove.set.hash.TIntHashSet;
26
27 public class CHRLiteral extends Symbol {
28     
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;
35     
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;
42     }
43
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;
50                 passive = false;
51             }
52             else {
53                 SCLRelation sclRelation = context.resolveRelation(unresolved.location, unresolved.name);
54                 if(sclRelation != null)
55                     relation = new ExternalCHRRelation(sclRelation);
56                 else {
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;
64                     passive = false;
65                     //context.getErrorLog().log(unresolved.location, "Couldn't resolve constraint " + unresolved.name + ".");
66                 }
67             }
68         }
69         for(int i=0;i<parameters.length;++i)
70             parameters[i] = parameters[i].resolve(context);
71     }
72
73     public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
74         for(Expression parameter : parameters)
75             parameter.collectRefs(allRefs, refs);
76     }
77
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);
83         }
84         else {
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");
92             else
93                 for(int i=0;i<parameters.length;++i)
94                     parameters[i] = parameters[i].checkType(context, parameterTypes[i]);
95         }
96     }
97
98     public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
99         for(Expression parameter : parameters)
100             parameter.collectVars(allVars, vars);
101     }
102
103     public void forVariables(VariableProcedure procedure) {
104         for(Expression parameter : parameters)
105             parameter.forVariables(procedure);
106     }
107
108     public void collectFreeVariables(THashSet<Variable> vars) {
109         for(Expression parameter : parameters)
110             parameter.collectFreeVariables(vars);
111     }
112
113     public void setLocationDeep(long loc) {
114         if(location == Locations.NO_LOCATION) {
115             this.location = loc;
116             for(Expression parameter : parameters)
117                 parameter.setLocationDeep(loc);
118         }
119     }
120     
121     public void simplify(SimplificationContext context) {
122         for(int i=0;i<parameters.length;++i)
123             parameters[i] = parameters[i].simplify(context);
124     }
125     
126     public String toString() {
127         StringBuilder b = new StringBuilder();
128         ExpressionToStringVisitor visitor = new ExpressionToStringVisitor(b);
129         visitor.visit(this);
130         return b.toString();
131     }
132
133     public void collectQueryEffects(THashSet<Type> effects) {
134     }
135
136     public void collectEnforceEffects(THashSet<Type> effects) {
137     }
138 }