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