]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ELiteral.java
(refs #7375) Replaced collectFreeVariables method by a visitor
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / ELiteral.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.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.errors.Locations;
14 import org.simantics.scl.compiler.internal.codegen.references.IVal;
15 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
16 import org.simantics.scl.compiler.internal.interpreted.IConstant;
17 import org.simantics.scl.compiler.internal.interpreted.IExpression;
18 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
19 import org.simantics.scl.compiler.types.exceptions.MatchException;
20 import org.simantics.scl.compiler.types.util.TypeUnparsingContext;
21
22 import gnu.trove.map.hash.TObjectIntHashMap;
23 import gnu.trove.set.hash.TIntHashSet;
24
25 public class ELiteral extends Expression {
26     Constant value;
27     
28     public ELiteral(Constant value) {
29         this.value = value;
30         setType(value.getType());
31     }
32
33     public ELiteral(long loc, Constant value) {
34         super(loc);
35         this.value = value;
36     }
37
38     public Constant getValue() {
39         return value;
40     }
41
42     @Override
43     public void collectVars(TObjectIntHashMap<Variable> allVars,
44             TIntHashSet vars) {     
45     }
46     
47     @Override
48     public Set<Variable> getFreeVariables() {
49         return Collections.emptySet();
50     }
51
52     public void toString(StringBuilder b, TypeUnparsingContext tuc) {
53         b.append(value);
54     }
55
56     @Override
57     protected void updateType() throws MatchException {
58         setType(value.getType());           
59     }
60
61     @Override
62     public IVal toVal(CompilationContext context, CodeWriter w) {
63         return value;
64     }
65
66     @Override
67     public Expression simplify(SimplificationContext context) {
68         return this;
69     }
70
71     @Override
72     public Expression resolve(TranslationContext context) {
73         return this;
74     }
75     
76     @Override
77     public Expression resolveAsPattern(TranslationContext context) {
78         return this;
79     }
80     
81     @Override
82     public void getParameters(TranslationContext translationContext,
83             ArrayList<Expression> parameters) {
84     }
85
86     @Override
87     public Expression replace(ReplaceContext context) {
88         return new ELiteral(value);
89     }
90     
91     @Override
92     public IExpression toIExpression(ExpressionInterpretationContext target) {
93         return new IConstant(value.realizeValue(target.localClassBuilder));
94     }
95     
96     @Override
97     public Expression inferType(TypingContext context) {
98         return this;
99     }
100     
101     @Override
102     public void setLocationDeep(long loc) {
103         if(location == Locations.NO_LOCATION)
104             location = loc;
105     }
106     
107     @Override
108     public void accept(ExpressionVisitor visitor) {
109         visitor.visit(this);
110     }
111     
112     @Override
113     public boolean isPattern(int arity) {
114         return value.constructorTag() >= 0 && value.getArity() == arity;
115     }
116     
117     @Override
118     public Expression accept(ExpressionTransformer transformer) {
119         return transformer.transform(this);
120     }
121
122     @Override
123     public boolean equalsExpression(Expression expression) {
124         if(expression.getClass() != getClass())
125             return false;
126         ELiteral other = (ELiteral)expression;
127         return value.equals(other.value);
128     }
129
130 }