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.TIntHashSet;
22 public class EFieldAccess extends SimplifiableExpression {
24 private static final Type VARIABLE = Types.con("Simantics/Variables", "Variable");
26 public Expression parent;
27 public FieldAccessor accessor;
28 boolean lastAccessor = true;
30 public EFieldAccess(Expression parent, FieldAccessor accessor) {
32 this.accessor = accessor;
33 if(parent instanceof EFieldAccess)
34 ((EFieldAccess)parent).lastAccessor = false;
38 public void collectVars(TObjectIntHashMap<Variable> allVars,
40 parent.collectVars(allVars, vars);
41 accessor.collectVars(allVars, vars);
44 private boolean returnsValue() {
45 return accessor.accessSeparator == '#' && !accessor.isVariableId();
49 protected void updateType() throws MatchException {
50 // Type is already updated in checkBasicType
53 private Expression resolveAccessor(TypingContext context, Type requiredType) {
54 if(!(accessor instanceof IdAccessor))
56 IdAccessor idAccessor = (IdAccessor)accessor;
57 if(idAccessor.accessSeparator != '.')
59 List<Constant> accessors = context.getEnvironment().getFieldAccessors(idAccessor.fieldName);
60 if(accessors == null) {
61 context.getErrorLog().log(idAccessor.location, "Couldn't resolve accessor ." + idAccessor.fieldName + ".");
62 return new EError(location);
64 Expression accessorExpression;
65 if(accessors.size() == 1)
66 accessorExpression = new ELiteral(accessors.get(0));
68 Alternative[] alternatives = new Alternative[accessors.size()];
69 for(int i=0;i<alternatives.length;++i) {
71 alternatives[i] = new Alternative() {
73 public Expression realize() {
74 return new ELiteral(accessors.get(index));
77 public Type getType() {
78 return accessors.get(index).getType();
81 public String toString() {
82 return accessors.get(index).toString();
86 accessorExpression = new EAmbiguous(alternatives);
87 accessorExpression.location = location;
89 return new EApply(location, accessorExpression, parent).checkType(context, requiredType);
93 public Expression checkBasicType(TypingContext context, Type requiredType) {
94 ModuleHeader header = context.getCompilationContext().header;
95 if(header != null && header.fields) {
96 Expression expression = resolveAccessor(context, requiredType);
97 if(expression != null)
103 setType(requiredType);
106 context.subsume(this, requiredType);
108 parent = parent.checkType(context, VARIABLE);
109 accessor.checkType(context);
110 context.declareEffect(getLocation(), Types.READ_GRAPH);
115 public Expression simplify(SimplificationContext context) {
116 // Simplify subexpressions
117 parent = parent.simplify(context);
118 accessor.simplify(context);
120 if(accessor.accessSeparator == '.')
124 context.getConstant(Names.Simantics_Variables_child_),
126 accessor.asExpression()
128 else if(!lastAccessor)
132 context.getConstant(Names.Simantics_Variables_property),
134 accessor.asExpression()
136 else if(accessor.isVariableId())
142 context.getConstant(Names.Simantics_Variables_untypedPropertyValue, getType()),
144 accessor.asExpression()
149 public Expression resolve(TranslationContext context) {
150 parent = parent.resolve(context);
151 accessor.resolve(context);
156 public void setLocationDeep(long loc) {
157 if(location == Locations.NO_LOCATION) {
159 parent.setLocationDeep(loc);
160 accessor.setLocationDeep(loc);
165 public void accept(ExpressionVisitor visitor) {
170 public Expression accept(ExpressionTransformer transformer) {
171 return transformer.transform(this);