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