]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/deriving/EqDeriver.java
Added info on backup location to documentation backup.
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / deriving / EqDeriver.java
1 package org.simantics.scl.compiler.internal.deriving;
2
3 import java.util.ArrayList;
4
5 import org.simantics.scl.compiler.common.datatypes.Constructor;
6 import org.simantics.scl.compiler.elaboration.errors.NotPatternException;
7 import org.simantics.scl.compiler.elaboration.expressions.EApply;
8 import org.simantics.scl.compiler.elaboration.expressions.EVar;
9 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
10 import org.simantics.scl.compiler.elaboration.expressions.Expression;
11 import org.simantics.scl.compiler.elaboration.expressions.Variable;
12 import org.simantics.scl.compiler.elaboration.modules.TypeConstructor;
13 import org.simantics.scl.compiler.environment.AmbiguousNameException;
14 import org.simantics.scl.compiler.environment.Environment;
15 import org.simantics.scl.compiler.environment.Environments;
16 import org.simantics.scl.compiler.errors.ErrorLog;
17 import org.simantics.scl.compiler.internal.parsing.declarations.DDerivingInstanceAst;
18 import org.simantics.scl.compiler.internal.parsing.declarations.DInstanceAst;
19 import org.simantics.scl.compiler.internal.parsing.declarations.DValueAst;
20 import org.simantics.scl.compiler.internal.parsing.expressions.Expressions;
21 import org.simantics.scl.compiler.internal.parsing.translation.ProcessedDInstanceAst;
22 import org.simantics.scl.compiler.internal.parsing.translation.ValueRepository;
23 import org.simantics.scl.compiler.internal.parsing.types.TVarAst;
24 import org.simantics.scl.compiler.types.TCon;
25
26 class EqDeriver implements InstanceDeriver {
27
28     @Override
29     public void derive(
30             ErrorLog errorLog,
31             Environment environment,
32             ArrayList<ProcessedDInstanceAst> instancesAst,
33             DDerivingInstanceAst der) {
34      // Analyze
35         if(der.types.length != 1) {
36             errorLog.log(der.location, "Invalid number of parameters to " + der.name);
37             return;
38         }
39         TVarAst headType = DerivingUtils.getHeadType(der.types[0]);
40         if(headType == null) {
41             errorLog.log(der.types[0].location, "Cannot derive Eq instance for the type " + headType + ".");
42             return;
43         }
44         TCon con;
45         try {
46             con = Environments.getTypeConstructorName(environment, headType.name);
47         } catch (AmbiguousNameException e1) {
48             errorLog.log(headType.location, e1.getMessage());
49             return;
50         }
51         if(con == null) {
52             errorLog.log(headType.location, "Couldn't resolve " + headType.name);
53             return;
54         }
55         TypeConstructor tcon = environment.getTypeConstructor(con);
56         if(tcon == null) {
57             errorLog.log(headType.location, "Didn't find type constructor for " + headType.name);
58             return;
59         }
60         if(tcon.isOpen) {
61             errorLog.log(headType.location, "Cannot derive instance for open data types.");
62             return;
63         }
64         
65         // Generate
66         DInstanceAst instanceAst = new DInstanceAst(der.location, der.context, der.name, der.types);
67         ValueRepository valueDefs = new ValueRepository();
68         for(Constructor constructor : tcon.constructors) {
69             int l = constructor.parameterTypes.length;
70             String[] par1 = new String[l];
71             String[] par2 = new String[l];
72             for(int i=0;i<l;++i) {
73                 par1[i] = "a" + i;
74                 par2[i] = "b" + i;
75             }
76             Expression lhs = new EApply(
77                     new EVar("=="),
78                     new EApply(new EVar(constructor.name.name), Expressions.vars(par1)),
79                     new EApply(new EVar(constructor.name.name), Expressions.vars(par2))
80                     );
81             Expression value = new EVar("True");
82             for(int i=l-1;i>=0;--i)
83                 value = new EApply(
84                         new EVar("&&"),
85                         new EApply(
86                                 new EVar("=="), 
87                                 new EVar(par1[i]), 
88                                 new EVar(par2[i])),
89                         value);
90             try {
91                 DValueAst valueAst = new DValueAst(lhs, value);
92                 valueAst.setLocationDeep(der.location);
93                 valueDefs.add(valueAst);
94             } catch (NotPatternException e) {
95                 errorLog.log(e.getExpression().location, "Not a pattern.");
96             }
97         }
98         {
99             Expression lhs = new EApply(
100                     new EVar("=="),
101                     new EVariable(new Variable("_")),
102                     new EVariable(new Variable("_")));
103             Expression value = new EVar("False");
104             try {
105                 DValueAst valueAst = new DValueAst(lhs, value);
106                 valueAst.setLocationDeep(der.location);
107                 valueDefs.add(valueAst);
108                 /*valueDefs.addAnnotation("==", new DAnnotationAst(new EVar("@private"), 
109                         Collections.<Expression>emptyList()));*/
110             } catch (NotPatternException e) {
111                 errorLog.log(e.getExpression().location, "Not a pattern.");
112             }
113         }
114         instancesAst.add(new ProcessedDInstanceAst(instanceAst, valueDefs));
115     }
116
117 }