]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EConstant.java
(refs #7375) Replaced forVariables by a visitor
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / EConstant.java
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3 import java.util.ArrayList;
4
5 import org.simantics.scl.compiler.common.names.Name;
6 import org.simantics.scl.compiler.common.precedence.Precedence;
7 import org.simantics.scl.compiler.compilation.CompilationContext;
8 import org.simantics.scl.compiler.constants.Constant;
9 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
10 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
11 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
12 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
13 import org.simantics.scl.compiler.elaboration.errors.NotPatternException;
14 import org.simantics.scl.compiler.elaboration.expressions.lhstype.LhsType;
15 import org.simantics.scl.compiler.elaboration.expressions.lhstype.PatternMatchingLhs;
16 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
17 import org.simantics.scl.compiler.errors.Locations;
18 import org.simantics.scl.compiler.internal.codegen.references.IVal;
19 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
20 import org.simantics.scl.compiler.internal.interpreted.IConstant;
21 import org.simantics.scl.compiler.internal.interpreted.IExpression;
22 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
23 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;
24 import org.simantics.scl.compiler.top.ValueNotFound;
25 import org.simantics.scl.compiler.types.TForAll;
26 import org.simantics.scl.compiler.types.TMetaVar;
27 import org.simantics.scl.compiler.types.Type;
28 import org.simantics.scl.compiler.types.Types;
29 import org.simantics.scl.compiler.types.exceptions.MatchException;
30 import org.simantics.scl.compiler.types.util.MultiFunction;
31 import org.simantics.scl.compiler.types.util.TypeUnparsingContext;
32
33 import gnu.trove.map.hash.TObjectIntHashMap;
34 import gnu.trove.set.hash.THashSet;
35 import gnu.trove.set.hash.TIntHashSet;
36
37 public class EConstant extends Expression {
38     SCLValue value;
39     Type[] typeParameters;
40     
41     public EConstant(SCLValue value, Type ... typeParameters) {
42         if(SCLCompilerConfiguration.DEBUG)
43             if(value == null)
44                 throw new NullPointerException();
45         this.value = value;
46         this.typeParameters = typeParameters;
47     }
48
49     public EConstant(SCLValue value) {
50         if(SCLCompilerConfiguration.DEBUG)
51             if(value == null)
52                 throw new NullPointerException();
53         this.value = value;
54         this.typeParameters = Type.EMPTY_ARRAY;
55     }
56
57     public EConstant(long loc, SCLValue value) {
58         super(loc);
59         if(SCLCompilerConfiguration.DEBUG)
60             if(value == null)
61                 throw new NullPointerException();
62         this.value = value;
63         this.typeParameters = Type.EMPTY_ARRAY;
64     }
65     
66     public EConstant(long loc, SCLValue value, Type ... typeParameters) {
67         super(loc);
68         if(SCLCompilerConfiguration.DEBUG)
69             if(value == null)
70                 throw new NullPointerException();
71         this.value = value;
72         this.typeParameters = typeParameters;
73     }
74
75     public void addTypeParameters(Type ... newTypeParameters) {
76         typeParameters = Types.concat(typeParameters, newTypeParameters);
77     }
78     
79     public Expression applyType(Type type) {
80         typeParameters = Types.concat(typeParameters, new Type[] {type});
81         if(getType() != null)
82             setType(Types.instantiate(getType(), type));
83         return this;
84     }
85
86         public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
87         int id = allRefs.get(value);
88         if(id >= 0)
89             refs.add(id);
90     }
91         
92         @Override
93         public void collectVars(TObjectIntHashMap<Variable> allVars,
94                 TIntHashSet vars) {   
95         }
96
97         public void toString(StringBuilder b, TypeUnparsingContext tuc) {
98             Name name = value.getName();
99             if(name.module.equals("Builtin") || name.module.equals("Prelude"))
100                 b.append(name.name);
101             else
102                 b.append(name);
103         /*for(Type type : typeParameters) {
104             b.append(" <");
105             b.append(type.toString(tuc));
106             b.append(">");
107         }*/
108     }
109
110     @Override
111     protected void updateType() throws MatchException {
112         setType(Types.instantiate(value.getType(), typeParameters));
113     }
114     
115         @Override
116         public IVal toVal(CompilationContext context, CodeWriter w) {
117             IVal val = value.getValue();                
118             if(typeParameters.length > 0) {
119                 val = val.createSpecialization(typeParameters);
120             }
121             return val;
122     }
123
124     @Override
125     public void collectFreeVariables(THashSet<Variable> vars) {
126     }
127
128     @Override
129     public Expression simplify(SimplificationContext context) {
130         if(value.getInlineInSimplification()) {
131             if(typeParameters.length > 0) {
132                 context.getErrorLog().log(location, 
133                         "Inlining with type parameters not currently supported in simplification.");
134                 return this;
135             }
136             else
137                 return value.getExpression().copy().simplify(context);
138         }
139         return this;
140     }
141
142     @Override
143     public Expression resolve(TranslationContext context) {
144         return this;
145     }
146     
147     @Override
148     public void getParameters(TranslationContext translationContext,
149             ArrayList<Expression> parameters) {
150     }
151     
152     public SCLValue getValue() {
153         return value;
154     }
155     
156     @Override
157     public Expression resolveAsPattern(TranslationContext context) {
158         return this;
159     }
160        
161     @Override
162     public void removeFreeVariables(THashSet<Variable> vars) {     
163     }
164
165     @Override
166     public Expression replace(ReplaceContext context) {
167         Type[] newTypeParameters;
168         if(typeParameters.length == 0)
169             newTypeParameters = Type.EMPTY_ARRAY;
170         else {
171             newTypeParameters = new Type[typeParameters.length];
172             for(int i=0;i<newTypeParameters.length;++i)
173                 newTypeParameters[i] = typeParameters[i].replace(context.tvarMap);
174         }
175         return new EConstant(value, newTypeParameters);
176     }
177     
178     public Type[] getTypeParameters() {
179         return typeParameters;
180     }
181
182     @Override
183     public LhsType getLhsType() throws NotPatternException {
184         return new PatternMatchingLhs();
185     }
186     
187     @Override
188     public IExpression toIExpression(ExpressionInterpretationContext target) {
189         Name name = value.getName();
190         try {
191             return new IConstant(target.runtimeEnvironment.getRuntimeModule(name.module).getValue(name.name));
192         } catch (ValueNotFound e) {
193             throw new UnsupportedOperationException();
194         }
195     }
196     
197     @Override
198     public Expression inferType(TypingContext context) {
199         if(context.recursiveValues != null &&
200                 context.recursiveValues.contains(value)) {
201             // Handles the case where the constant is one of the recursive definitions we are currently checking
202             // This kind of value is not yet generalized, i.e. it is not necessary to instantiate it.
203             EPlaceholder placeholder = new EPlaceholder(location, this);
204             placeholder.setType(value.getType());
205             
206             context.recursiveReferences.add(placeholder);
207             return placeholder;
208         }
209         else if(context.isInPattern()) {
210             /* This is little hackish code that handles the following kind of constructors:
211              *   data Thunk a = Thunk s (a -> s)
212              * in
213              *   match thunk with Thunk s f -> f s
214              * We cannot assign s with an unbound metaVar because its type depends on 
215              * how it has been constructed. Therefore we parametrize the function with
216              * existential variable.
217              */
218             Type resultType = value.getType();
219             if(resultType instanceof TForAll) {
220                 ArrayList<TMetaVar> vars = new ArrayList<TMetaVar>(); 
221                 resultType = Types.instantiate(resultType, vars);
222                 MultiFunction mfun = Types.matchFunction(resultType);
223                 resultType = mfun.returnType;
224                 
225                 for(TMetaVar var : vars) {
226                     if(resultType.contains(var))
227                         break;
228                     addTypeParameters(Types.var(var.getKind()));                    
229                 }
230             }
231             return this;
232         }
233         else
234             return applyPUnit(context);
235     }
236     
237     @Override
238     public boolean isEffectful() {
239         return false;
240     }
241
242     @Override
243     public void collectEffects(THashSet<Type> effects) {
244     }
245     
246     @Override
247     public void setLocationDeep(long loc) {
248         if(location == Locations.NO_LOCATION)
249             location = loc;
250     }
251     
252     @Override
253     public void accept(ExpressionVisitor visitor) {
254         visitor.visit(this);
255     }
256     
257     @Override
258     public Precedence getPrecedence() {
259         return value.getPrecedence();
260     }
261     
262     @Override
263     public boolean isPattern(int arity) {
264         IVal val = value.getValue();
265         if(!(val instanceof Constant))
266             return false;
267         Constant constant = (Constant)val;
268         return constant.constructorTag() >= 0 && constant.getArity() == arity;
269     }
270     
271     @Override
272     public Expression accept(ExpressionTransformer transformer) {
273         return transformer.transform(this);
274     }
275
276     @Override
277     public boolean equalsExpression(Expression expression) {
278         if(expression.getClass() != getClass())
279             return false;
280         EConstant other = (EConstant)expression;
281         return value == other.value && Types.equals(typeParameters, other.typeParameters);
282     }
283 }