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 collectVars(TObjectIntHashMap<Variable> allVars,
41 parent.collectVars(allVars, vars);
42 accessor.collectVars(allVars, vars);
45 private boolean returnsValue() {
46 return accessor.accessSeparator == '#' && !accessor.isVariableId();
50 protected void updateType() throws MatchException {
51 // Type is already updated in checkBasicType
54 private Expression resolveAccessor(TypingContext context, Type requiredType) {
55 if(!(accessor instanceof IdAccessor))
57 IdAccessor idAccessor = (IdAccessor)accessor;
58 if(idAccessor.accessSeparator != '.')
60 List<Constant> accessors = context.getEnvironment().getFieldAccessors(idAccessor.fieldName);
61 if(accessors == null) {
62 context.getErrorLog().log(idAccessor.location, "Couldn't resolve accessor ." + idAccessor.fieldName + ".");
63 return new EError(location);
65 Expression accessorExpression;
66 if(accessors.size() == 1)
67 accessorExpression = new ELiteral(accessors.get(0));
69 Alternative[] alternatives = new Alternative[accessors.size()];
70 for(int i=0;i<alternatives.length;++i) {
72 alternatives[i] = new Alternative() {
74 public Expression realize() {
75 return new ELiteral(accessors.get(index));
78 public Type getType() {
79 return accessors.get(index).getType();
82 public String toString() {
83 return accessors.get(index).toString();
87 accessorExpression = new EAmbiguous(alternatives);
88 accessorExpression.location = location;
90 return new EApply(location, accessorExpression, parent).checkType(context, requiredType);
94 public Expression checkBasicType(TypingContext context, Type requiredType) {
95 ModuleHeader header = context.getCompilationContext().header;
96 if(header != null && header.fields) {
97 Expression expression = resolveAccessor(context, requiredType);
98 if(expression != null)
104 setType(requiredType);
107 context.subsume(this, requiredType);
109 parent = parent.checkType(context, VARIABLE);
110 accessor.checkType(context);
111 context.declareEffect(getLocation(), Types.READ_GRAPH);
116 public void collectFreeVariables(THashSet<Variable> vars) {
117 parent.collectFreeVariables(vars);
118 accessor.collectFreeVariables(vars);
122 public Expression simplify(SimplificationContext context) {
123 // Simplify subexpressions
124 parent = parent.simplify(context);
125 accessor.simplify(context);
127 if(accessor.accessSeparator == '.')
131 context.getConstant(Names.Simantics_Variables_child_),
133 accessor.asExpression()
135 else if(!lastAccessor)
139 context.getConstant(Names.Simantics_Variables_property),
141 accessor.asExpression()
143 else if(accessor.isVariableId())
149 context.getConstant(Names.Simantics_Variables_untypedPropertyValue, getType()),
151 accessor.asExpression()
156 public Expression resolve(TranslationContext context) {
157 parent = parent.resolve(context);
158 accessor.resolve(context);
163 public void setLocationDeep(long loc) {
164 if(location == Locations.NO_LOCATION) {
166 parent.setLocationDeep(loc);
167 accessor.setLocationDeep(loc);
172 public void accept(ExpressionVisitor visitor) {
177 public Expression accept(ExpressionTransformer transformer) {
178 return transformer.transform(this);