]> gerrit.simantics Code Review - simantics/platform.git/blob
0d3e2eeaf3600e32aa1ca5206293f40d4182006d
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3 import java.util.List;
4
5 import org.simantics.scl.compiler.common.names.Names;
6 import org.simantics.scl.compiler.constants.Constant;
7 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
8 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
9 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
10 import org.simantics.scl.compiler.elaboration.expressions.EAmbiguous.Alternative;
11 import org.simantics.scl.compiler.elaboration.expressions.accessor.FieldAccessor;
12 import org.simantics.scl.compiler.elaboration.expressions.accessor.IdAccessor;
13 import org.simantics.scl.compiler.errors.Locations;
14 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;
15 import org.simantics.scl.compiler.internal.header.ModuleHeader;
16 import org.simantics.scl.compiler.types.Type;
17 import org.simantics.scl.compiler.types.Types;
18 import org.simantics.scl.compiler.types.exceptions.MatchException;
19
20 import gnu.trove.map.hash.TObjectIntHashMap;
21 import gnu.trove.set.hash.THashSet;
22 import gnu.trove.set.hash.TIntHashSet;
23
24 public class EFieldAccess extends SimplifiableExpression {
25
26     private static final Type VARIABLE = Types.con("Simantics/Variables", "Variable");
27     
28     Expression parent;
29     FieldAccessor accessor;
30     boolean lastAccessor = true;
31
32     public EFieldAccess(Expression parent, FieldAccessor accessor) {
33         this.parent = parent;
34         this.accessor = accessor;
35         if(parent instanceof EFieldAccess)
36                 ((EFieldAccess)parent).lastAccessor = false;
37     }
38
39     @Override
40     public void collectRefs(TObjectIntHashMap<Object> allRefs,
41             TIntHashSet refs) {
42         parent.collectRefs(allRefs, refs);
43         accessor.collectRefs(allRefs, refs);
44     }
45
46     @Override
47     public void collectVars(TObjectIntHashMap<Variable> allVars,
48             TIntHashSet vars) {
49         parent.collectVars(allVars, vars);
50         accessor.collectVars(allVars, vars);
51     }
52
53     private boolean returnsValue() {
54         return accessor.accessSeparator == '#' && !accessor.isVariableId();
55     }
56
57     @Override
58     protected void updateType() throws MatchException {
59         // Type is already updated in checkBasicType
60     }
61     
62     private Expression resolveAccessor(TypingContext context, Type requiredType) {
63         if(!(accessor instanceof IdAccessor))
64             return null;
65         IdAccessor idAccessor = (IdAccessor)accessor;
66         if(idAccessor.accessSeparator != '.')
67             return null;
68         List<Constant> accessors = context.getEnvironment().getFieldAccessors(idAccessor.fieldName);
69         if(accessors == null) {
70             context.getErrorLog().log("Couldn't resolve accessor ." + idAccessor.fieldName + ".");
71             return new EError(location);
72         }
73         Expression accessorExpression;
74         if(accessors.size() == 1)
75             accessorExpression = new ELiteral(accessors.get(0));
76         else {
77             Alternative[] alternatives = new Alternative[accessors.size()];
78             for(int i=0;i<alternatives.length;++i) {
79                 final int index = i;
80                 alternatives[i] = new Alternative() {
81                     @Override
82                     public Expression realize() {
83                         return new ELiteral(accessors.get(index));
84                     }
85                     @Override
86                     public Type getType() {
87                         return accessors.get(index).getType();
88                     }
89                 };
90             }
91             accessorExpression = new EAmbiguous(alternatives);
92             accessorExpression.location = location;
93         }
94         return new EApply(location, accessorExpression, parent).checkType(context, requiredType);
95     }
96     
97     @Override
98     public Expression checkBasicType(TypingContext context, Type requiredType) {
99         ModuleHeader header = context.getCompilationContext().header;
100         if(header != null && header.fields) {
101             Expression expression = resolveAccessor(context, requiredType);
102             if(expression != null)
103                 return expression;
104         }
105         
106         // Default case
107         if(returnsValue())
108             setType(requiredType);
109         else {
110             setType(VARIABLE);
111             context.subsume(this, requiredType);
112         }
113         parent = parent.checkType(context, VARIABLE);
114         accessor.checkType(context);
115         context.declareEffect(getLocation(), Types.READ_GRAPH);
116         return this;
117     }
118
119     @Override
120     public void collectFreeVariables(THashSet<Variable> vars) {
121         parent.collectFreeVariables(vars);
122         accessor.collectFreeVariables(vars);
123     }
124
125     @Override
126     public Expression simplify(SimplificationContext context) {
127         // Simplify subexpressions
128         parent = parent.simplify(context);
129         accessor.simplify(context);
130         
131         if(accessor.accessSeparator == '.')
132                 return new EApply(
133                                 getLocation(),
134                                 Types.READ_GRAPH,
135                                 context.getConstant(Names.Simantics_Variables_child_),
136                                 parent,
137                                 accessor.asExpression()
138                                 );
139         else if(!lastAccessor)
140                 return new EApply(
141                                 getLocation(),
142                                 Types.READ_GRAPH,
143                                 context.getConstant(Names.Simantics_Variables_property),
144                                 parent,
145                                 accessor.asExpression()
146                                 );
147         else if(accessor.isVariableId())
148                 return parent;
149         else
150                 return new EApply(
151                                 getLocation(),
152                                 Types.READ_GRAPH,
153                                 context.getConstant(Names.Simantics_Variables_untypedPropertyValue, getType()),
154                                 parent,
155                                 accessor.asExpression()
156                                 );
157     }
158
159     @Override
160     public Expression resolve(TranslationContext context) {
161         parent = parent.resolve(context);
162         accessor.resolve(context);
163         return this;
164     }
165
166     @Override
167     public Expression decorate(ExpressionDecorator decorator) {
168         return decorator.decorate(this);
169     }
170
171     @Override
172     public void collectEffects(THashSet<Type> effects) {
173         // FIXME
174         effects.add(Types.READ_GRAPH);
175     }
176     
177     @Override
178     public void setLocationDeep(long loc) {
179         if(location == Locations.NO_LOCATION) {
180             location = loc;
181             parent.setLocationDeep(loc);
182             accessor.setLocationDeep(loc);
183         }
184     }
185     
186     @Override
187     public void accept(ExpressionVisitor visitor) {
188         visitor.visit(this);
189     }
190
191     @Override
192     public void forVariables(VariableProcedure procedure) {
193         parent.forVariables(procedure);
194         accessor.forVariables(procedure);
195     }
196     
197     @Override
198     public Expression accept(ExpressionTransformer transformer) {
199         return transformer.transform(this);
200     }
201
202 }