1 package org.simantics.scl.compiler.internal.deriving;
3 import java.util.ArrayList;
5 import org.simantics.scl.compiler.common.datatypes.Constructor;
6 import org.simantics.scl.compiler.common.names.Name;
7 import org.simantics.scl.compiler.constants.ByteConstant;
8 import org.simantics.scl.compiler.constants.IntegerConstant;
9 import org.simantics.scl.compiler.elaboration.errors.NotPatternException;
10 import org.simantics.scl.compiler.elaboration.expressions.Case;
11 import org.simantics.scl.compiler.elaboration.expressions.EApply;
12 import org.simantics.scl.compiler.elaboration.expressions.EBlock;
13 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
14 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
15 import org.simantics.scl.compiler.elaboration.expressions.EMatch;
16 import org.simantics.scl.compiler.elaboration.expressions.EPreLet;
17 import org.simantics.scl.compiler.elaboration.expressions.EVar;
18 import org.simantics.scl.compiler.elaboration.expressions.Expression;
19 import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement;
20 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
21 import org.simantics.scl.compiler.elaboration.modules.TypeConstructor;
22 import org.simantics.scl.compiler.environment.AmbiguousNameException;
23 import org.simantics.scl.compiler.environment.Environment;
24 import org.simantics.scl.compiler.environment.Environments;
25 import org.simantics.scl.compiler.errors.ErrorLog;
26 import org.simantics.scl.compiler.internal.parsing.declarations.DDerivingInstanceAst;
27 import org.simantics.scl.compiler.internal.parsing.declarations.DInstanceAst;
28 import org.simantics.scl.compiler.internal.parsing.declarations.DValueAst;
29 import org.simantics.scl.compiler.internal.parsing.expressions.Expressions;
30 import org.simantics.scl.compiler.internal.parsing.translation.ProcessedDInstanceAst;
31 import org.simantics.scl.compiler.internal.parsing.translation.ValueRepository;
32 import org.simantics.scl.compiler.internal.parsing.types.TVarAst;
33 import org.simantics.scl.compiler.types.TCon;
35 class IODeriver implements InstanceDeriver {
37 private static final Name WRITE = Name.create("Serialization", "write");
38 private static final Name READ = Name.create("Serialization", "read");
39 private static final Name IO_SIZE = Name.create("Serialization", "ioSize");
44 Environment environment,
45 ArrayList<ProcessedDInstanceAst> instancesAst,
46 DDerivingInstanceAst der) {
48 if(der.types.length != 1) {
49 errorLog.log(der.location, "Invalid number of parameters to " + der.name);
52 TVarAst headType = DerivingUtils.getHeadType(der.types[0]);
53 if(headType == null) {
54 errorLog.log(der.types[0].location, "Cannot derive IO instance for the type " + headType + ".");
59 con = Environments.getTypeConstructorName(environment, headType.name);
60 } catch (AmbiguousNameException e1) {
61 errorLog.log(headType.location, e1.getMessage());
65 errorLog.log(headType.location, "Couldn't resolve " + headType.name);
68 TypeConstructor tcon = environment.getTypeConstructor(con);
70 errorLog.log(headType.location, "Didn't find type constructor for " + headType.name);
74 errorLog.log(headType.location, "Cannot derive instance for open data types.");
78 DInstanceAst instanceAst = new DInstanceAst(der.location, der.context, der.name, der.types);
79 ValueRepository valueDefs = new ValueRepository();
81 SCLValue write = environment.getValue(WRITE);
82 SCLValue read = environment.getValue(READ);
83 SCLValue ioSize = environment.getValue(IO_SIZE);
86 for(int id=0;id<tcon.constructors.length;++id) {
87 Constructor constructor = tcon.constructors[id];
88 int l = constructor.parameterTypes.length;
89 String[] par = new String[l];
90 for(int i=0;i<l;++i) {
93 Expression lhs = new EApply(
96 new EApply(new EVar(constructor.name.name), Expressions.vars(par))
98 ArrayList<Expression> statements = new ArrayList<Expression>();
100 new EApply(new EConstant(write), new EVar("s"),
101 new ELiteral(new ByteConstant((byte)id))));
103 statements.add(new EApply(new EConstant(write), new EVar("s"), new EVar(par[i])));
105 Expression rhs = EBlock.create(statements);
107 DValueAst valueAst = new DValueAst(lhs, rhs);
108 valueAst.setLocationDeep(der.location);
109 valueDefs.add(valueAst);
110 /*valueDefs.addAnnotation("write", new DAnnotationAst(new EVar("@private"),
111 Collections.<Expression>emptyList()));*/
112 } catch (NotPatternException e) {
113 errorLog.log(e.getExpression().location, "Not a pattern.");
119 Expression lhs = new EApply(
123 Case[] cases = new Case[tcon.constructors.length];
124 for(int id=0;id<tcon.constructors.length;++id) {
125 Constructor constructor = tcon.constructors[id];
126 int l = constructor.parameterTypes.length;
127 String[] par = new String[l];
128 for(int i=0;i<l;++i) {
131 ArrayList<LetStatement> assignments = new ArrayList<LetStatement>(l);
133 assignments.add(new LetStatement(new EVar(par[i]),
134 new EApply(new EConstant(read), new EVar("s"))));
135 Expression in = new EApply(
136 new EVar(constructor.name.name),
137 Expressions.vars(par));
138 cases[id] = new Case(new ELiteral(new ByteConstant((byte)id)),
139 new EPreLet(assignments, in));
142 new EMatch(new EApply(new EConstant(read), new EVar("s")),
145 DValueAst valueAst = new DValueAst(lhs, rhs);
146 valueAst.setLocationDeep(der.location);
147 valueDefs.add(valueAst);
148 /*valueDefs.addAnnotation("read=", new DAnnotationAst(new EVar("@private"),
149 Collections.<Expression>emptyList()));*/
150 } catch (NotPatternException e) {
151 errorLog.log(e.getExpression().location, "Not a pattern.");
156 for(int id=0;id<tcon.constructors.length;++id) {
157 Constructor constructor = tcon.constructors[id];
158 int l = constructor.parameterTypes.length;
159 String[] par = new String[l];
160 for(int i=0;i<l;++i) {
163 Expression lhs = new EApply(
165 new EApply(new EVar(constructor.name.name), Expressions.vars(par))
167 Expression rhs = new ELiteral(new IntegerConstant(1));
169 rhs = new EApply(new EVar("+"),
171 new EApply(new EConstant(ioSize), new EVar(par[i]))
174 DValueAst valueAst = new DValueAst(lhs, rhs);
175 valueAst.setLocationDeep(der.location);
176 valueDefs.add(valueAst);
177 /*valueDefs.addAnnotation("ioSize", new DAnnotationAst(new EVar("@private"),
178 Collections.<Expression>emptyList()));*/
179 } catch (NotPatternException e) {
180 errorLog.log(e.getExpression().location, "Not a pattern.");
184 instancesAst.add(new ProcessedDInstanceAst(instanceAst, valueDefs));