]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRLiteral.java
Resolve some dependency problems with SDK features
[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 Expression[] typeConstraintEvidenceParameters;
33     public boolean killAfterMatch;
34     public boolean negated;
35     public boolean passive = true;
36     
37     public CHRLiteral(long location, CHRRelation relation, Expression[] parameters, boolean remove, boolean negated) {
38         this.location = location;
39         this.relation = relation;
40         this.parameters = parameters;
41         this.killAfterMatch = remove;
42         this.negated = negated;
43     }
44
45     public void resolve(TranslationContext context) {
46         if(relation instanceof UnresolvedCHRRelation) {
47             UnresolvedCHRRelation unresolved = (UnresolvedCHRRelation)relation;
48             CHRConstraint constraint = context.resolveCHRConstraint(unresolved.name);
49             if(constraint != null) {
50                 relation = constraint;
51                 passive = false;
52             }
53             else {
54                 SCLRelation sclRelation = context.resolveRelation(unresolved.location, unresolved.name);
55                 if(sclRelation != null)
56                     relation = new ExternalCHRRelation(sclRelation);
57                 else {
58                     Type[] parameterTypes = new Type[parameters.length];
59                     for(int i=0;i<parameterTypes.length;++i)
60                         parameterTypes[i] = Types.metaVar(Kinds.STAR);
61                     constraint = new CHRConstraint(location, unresolved.name, parameterTypes);
62                     constraint.implicitlyDeclared = true;
63                     context.newCHRConstraint(constraint.name, constraint);
64                     relation = constraint;
65                     passive = false;
66                     //context.getErrorLog().log(unresolved.location, "Couldn't resolve constraint " + unresolved.name + ".");
67                 }
68             }
69         }
70         for(int i=0;i<parameters.length;++i)
71             parameters[i] = parameters[i].resolve(context);
72     }
73
74     public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
75         for(Expression parameter : parameters)
76             parameter.collectRefs(allRefs, refs);
77         if(typeConstraintEvidenceParameters != null)
78             for(Expression parameter : typeConstraintEvidenceParameters)
79                 parameter.collectRefs(allRefs, refs);
80     }
81
82     public void checkType(TypingContext context) {
83         if(relation == SpecialCHRRelation.EXECUTE) {
84             if(parameters.length != 1)
85                 throw new InternalCompilerError("Wrong number of parameters for EXECUTE constraint.");
86             parameters[0] = parameters[0].checkIgnoredType(context);
87             typeConstraintEvidenceParameters = Expression.EMPTY_ARRAY;
88         }
89         else {
90             TVar[] typeVariables = relation.getTypeVariables();
91             typeParameters = typeVariables.length == 0 ? Type.EMPTY_ARRAY : new Type[typeVariables.length];
92             for(int i=0;i<typeVariables.length;++i)
93                 typeParameters[i] = Types.metaVar(typeVariables[i].getKind());
94             Type[] parameterTypes = Types.replace(relation.getParameterTypes(), typeVariables, typeParameters);
95             if(parameterTypes.length != parameters.length)
96                 context.getErrorLog().log(location, "Constraint is applied with wrong number of parameters");
97             else
98                 for(int i=0;i<parameters.length;++i)
99                     parameters[i] = parameters[i].checkType(context, parameterTypes[i]);
100             
101             typeConstraintEvidenceParameters = context.addConstraints(Types.replace(relation.getTypeConstraints(), typeVariables, typeParameters));
102         }
103     }
104
105     public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
106         for(Expression parameter : parameters)
107             parameter.collectVars(allVars, vars);
108         if(typeConstraintEvidenceParameters != null)
109             for(Expression parameter : typeConstraintEvidenceParameters)
110                 parameter.collectVars(allVars, vars);
111     }
112
113     public void forVariables(VariableProcedure procedure) {
114         for(Expression parameter : parameters)
115             parameter.forVariables(procedure);
116         if(typeConstraintEvidenceParameters != null)
117             for(Expression parameter : typeConstraintEvidenceParameters)
118                 parameter.forVariables(procedure);
119     }
120
121     public void collectFreeVariables(THashSet<Variable> vars) {
122         for(Expression parameter : parameters)
123             parameter.collectFreeVariables(vars);
124         if(typeConstraintEvidenceParameters != null)
125             for(Expression parameter : typeConstraintEvidenceParameters)
126                 parameter.collectFreeVariables(vars);
127     }
128
129     public void setLocationDeep(long loc) {
130         if(location == Locations.NO_LOCATION) {
131             this.location = loc;
132             for(Expression parameter : parameters)
133                 parameter.setLocationDeep(loc);
134         }
135     }
136     
137     public void simplify(SimplificationContext context) {
138         for(int i=0;i<parameters.length;++i)
139             parameters[i] = parameters[i].simplify(context);
140         if(typeConstraintEvidenceParameters != null)
141             for(int i=0;i<typeConstraintEvidenceParameters.length;++i)
142                 typeConstraintEvidenceParameters[i] = typeConstraintEvidenceParameters[i].simplify(context);
143     }
144     
145     public String toString() {
146         StringBuilder b = new StringBuilder();
147         ExpressionToStringVisitor visitor = new ExpressionToStringVisitor(b);
148         visitor.visit(this);
149         return b.toString();
150     }
151
152     public void collectQueryEffects(THashSet<Type> effects) {
153     }
154
155     public void collectEnforceEffects(THashSet<Type> effects) {
156     }
157 }