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