]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EFieldAccess.java
c05b15409bf8976d6a95c386d7a714085c7c5d01
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / EFieldAccess.java
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         }
93         return new EApply(location, accessorExpression, parent).checkType(context, requiredType);
94     }
95     
96     @Override
97     public Expression checkBasicType(TypingContext context, Type requiredType) {
98         ModuleHeader header = context.getCompilationContext().header;
99         if(header != null && header.fields) {
100             Expression expression = resolveAccessor(context, requiredType);
101             if(expression != null)
102                 return expression;
103         }
104         
105         // Default case
106         if(returnsValue())
107             setType(requiredType);
108         else {
109             setType(VARIABLE);
110             context.subsume(this, requiredType);
111         }
112         parent = parent.checkType(context, VARIABLE);
113         accessor.checkType(context);
114         context.declareEffect(getLocation(), Types.READ_GRAPH);
115         return this;
116     }
117
118     @Override
119     public void collectFreeVariables(THashSet<Variable> vars) {
120         parent.collectFreeVariables(vars);
121         accessor.collectFreeVariables(vars);
122     }
123
124     @Override
125     public Expression simplify(SimplificationContext context) {
126         // Simplify subexpressions
127         parent = parent.simplify(context);
128         accessor.simplify(context);
129         
130         if(accessor.accessSeparator == '.')
131                 return new EApply(
132                                 getLocation(),
133                                 Types.READ_GRAPH,
134                                 context.getConstant(Names.Simantics_Variables_child_),
135                                 parent,
136                                 accessor.asExpression()
137                                 );
138         else if(!lastAccessor)
139                 return new EApply(
140                                 getLocation(),
141                                 Types.READ_GRAPH,
142                                 context.getConstant(Names.Simantics_Variables_property),
143                                 parent,
144                                 accessor.asExpression()
145                                 );
146         else if(accessor.isVariableId())
147                 return parent;
148         else
149                 return new EApply(
150                                 getLocation(),
151                                 Types.READ_GRAPH,
152                                 context.getConstant(Names.Simantics_Variables_untypedPropertyValue, getType()),
153                                 parent,
154                                 accessor.asExpression()
155                                 );
156     }
157
158     @Override
159     public Expression resolve(TranslationContext context) {
160         parent = parent.resolve(context);
161         accessor.resolve(context);
162         return this;
163     }
164
165     @Override
166     public Expression decorate(ExpressionDecorator decorator) {
167         return decorator.decorate(this);
168     }
169
170     @Override
171     public void collectEffects(THashSet<Type> effects) {
172         // FIXME
173         effects.add(Types.READ_GRAPH);
174     }
175     
176     @Override
177     public void setLocationDeep(long loc) {
178         if(location == Locations.NO_LOCATION) {
179             location = loc;
180             parent.setLocationDeep(loc);
181             accessor.setLocationDeep(loc);
182         }
183     }
184     
185     @Override
186     public void accept(ExpressionVisitor visitor) {
187         visitor.visit(this);
188     }
189
190     @Override
191     public void forVariables(VariableProcedure procedure) {
192         parent.forVariables(procedure);
193         accessor.forVariables(procedure);
194     }
195     
196     @Override
197     public Expression accept(ExpressionTransformer transformer) {
198         return transformer.transform(this);
199     }
200
201 }