]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ELiteral.java
14bd02344e4d52e56b009adc9bd8cacd1b7535f8
[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
5 import org.simantics.scl.compiler.compilation.CompilationContext;
6 import org.simantics.scl.compiler.constants.Constant;
7 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
8 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
9 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
10 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
11 import org.simantics.scl.compiler.errors.Locations;
12 import org.simantics.scl.compiler.internal.codegen.references.IVal;
13 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
14 import org.simantics.scl.compiler.internal.interpreted.IConstant;
15 import org.simantics.scl.compiler.internal.interpreted.IExpression;
16 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
17 import org.simantics.scl.compiler.types.Type;
18 import org.simantics.scl.compiler.types.exceptions.MatchException;
19 import org.simantics.scl.compiler.types.util.TypeUnparsingContext;
20
21 import gnu.trove.map.hash.TObjectIntHashMap;
22 import gnu.trove.set.hash.THashSet;
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     public void toString(StringBuilder b, TypeUnparsingContext tuc) {
48         b.append(value);
49     }
50
51     @Override
52     protected void updateType() throws MatchException {
53         setType(value.getType());           
54     }
55
56     @Override
57     public IVal toVal(CompilationContext context, CodeWriter w) {
58         return value;
59     }
60
61     @Override
62     public void collectFreeVariables(THashSet<Variable> vars) {
63     }
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 void removeFreeVariables(THashSet<Variable> vars) {     
93     }
94     
95     @Override
96     public IExpression toIExpression(ExpressionInterpretationContext target) {
97         return new IConstant(value.realizeValue(target.localClassBuilder));
98     }
99     
100     @Override
101     public Expression inferType(TypingContext context) {
102         return this;
103     }
104
105     @Override
106     public void collectEffects(THashSet<Type> effects) {
107     }
108     
109     @Override
110     public void setLocationDeep(long loc) {
111         if(location == Locations.NO_LOCATION)
112             location = loc;
113     }
114     
115     @Override
116     public void accept(ExpressionVisitor visitor) {
117         visitor.visit(this);
118     }
119     
120     @Override
121     public boolean isPattern(int arity) {
122         return value.constructorTag() >= 0 && value.getArity() == arity;
123     }
124     
125     @Override
126     public Expression accept(ExpressionTransformer transformer) {
127         return transformer.transform(this);
128     }
129
130     @Override
131     public boolean equalsExpression(Expression expression) {
132         if(expression.getClass() != getClass())
133             return false;
134         ELiteral other = (ELiteral)expression;
135         return value.equals(other.value);
136     }
137
138 }