1 package org.simantics.scl.compiler.elaboration.expressions;
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.header.ModuleHeader;
15 import org.simantics.scl.compiler.types.Type;
16 import org.simantics.scl.compiler.types.Types;
17 import org.simantics.scl.compiler.types.exceptions.MatchException;
19 import gnu.trove.map.hash.TObjectIntHashMap;
20 import gnu.trove.set.hash.THashSet;
21 import gnu.trove.set.hash.TIntHashSet;
23 public class EFieldAccess extends SimplifiableExpression {
25 private static final Type VARIABLE = Types.con("Simantics/Variables", "Variable");
28 FieldAccessor accessor;
29 boolean lastAccessor = true;
31 public EFieldAccess(Expression parent, FieldAccessor accessor) {
33 this.accessor = accessor;
34 if(parent instanceof EFieldAccess)
35 ((EFieldAccess)parent).lastAccessor = false;
39 public void collectRefs(TObjectIntHashMap<Object> allRefs,
41 parent.collectRefs(allRefs, refs);
42 accessor.collectRefs(allRefs, refs);
46 public void collectVars(TObjectIntHashMap<Variable> allVars,
48 parent.collectVars(allVars, vars);
49 accessor.collectVars(allVars, vars);
52 private boolean returnsValue() {
53 return accessor.accessSeparator == '#' && !accessor.isVariableId();
57 protected void updateType() throws MatchException {
58 // Type is already updated in checkBasicType
61 private Expression resolveAccessor(TypingContext context, Type requiredType) {
62 if(!(accessor instanceof IdAccessor))
64 IdAccessor idAccessor = (IdAccessor)accessor;
65 if(idAccessor.accessSeparator != '.')
67 List<Constant> accessors = context.getEnvironment().getFieldAccessors(idAccessor.fieldName);
68 if(accessors == null) {
69 context.getErrorLog().log(idAccessor.location, "Couldn't resolve accessor ." + idAccessor.fieldName + ".");
70 return new EError(location);
72 Expression accessorExpression;
73 if(accessors.size() == 1)
74 accessorExpression = new ELiteral(accessors.get(0));
76 Alternative[] alternatives = new Alternative[accessors.size()];
77 for(int i=0;i<alternatives.length;++i) {
79 alternatives[i] = new Alternative() {
81 public Expression realize() {
82 return new ELiteral(accessors.get(index));
85 public Type getType() {
86 return accessors.get(index).getType();
89 public String toString() {
90 return accessors.get(index).toString();
94 accessorExpression = new EAmbiguous(alternatives);
95 accessorExpression.location = location;
97 return new EApply(location, accessorExpression, parent).checkType(context, requiredType);
101 public Expression checkBasicType(TypingContext context, Type requiredType) {
102 ModuleHeader header = context.getCompilationContext().header;
103 if(header != null && header.fields) {
104 Expression expression = resolveAccessor(context, requiredType);
105 if(expression != null)
111 setType(requiredType);
114 context.subsume(this, requiredType);
116 parent = parent.checkType(context, VARIABLE);
117 accessor.checkType(context);
118 context.declareEffect(getLocation(), Types.READ_GRAPH);
123 public void collectFreeVariables(THashSet<Variable> vars) {
124 parent.collectFreeVariables(vars);
125 accessor.collectFreeVariables(vars);
129 public Expression simplify(SimplificationContext context) {
130 // Simplify subexpressions
131 parent = parent.simplify(context);
132 accessor.simplify(context);
134 if(accessor.accessSeparator == '.')
138 context.getConstant(Names.Simantics_Variables_child_),
140 accessor.asExpression()
142 else if(!lastAccessor)
146 context.getConstant(Names.Simantics_Variables_property),
148 accessor.asExpression()
150 else if(accessor.isVariableId())
156 context.getConstant(Names.Simantics_Variables_untypedPropertyValue, getType()),
158 accessor.asExpression()
163 public Expression resolve(TranslationContext context) {
164 parent = parent.resolve(context);
165 accessor.resolve(context);
170 public void collectEffects(THashSet<Type> effects) {
172 effects.add(Types.READ_GRAPH);
176 public void setLocationDeep(long loc) {
177 if(location == Locations.NO_LOCATION) {
179 parent.setLocationDeep(loc);
180 accessor.setLocationDeep(loc);
185 public void accept(ExpressionVisitor visitor) {
190 public void forVariables(VariableProcedure procedure) {
191 parent.forVariables(procedure);
192 accessor.forVariables(procedure);
196 public Expression accept(ExpressionTransformer transformer) {
197 return transformer.transform(this);