]> 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 forVariables 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
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         public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
43     }
44
45         @Override
46         public void collectVars(TObjectIntHashMap<Variable> allVars,
47                 TIntHashSet vars) {         
48         }
49         
50         public void toString(StringBuilder b, TypeUnparsingContext tuc) {
51         b.append(value);
52     }
53         
54         @Override
55         protected void updateType() throws MatchException {
56             setType(value.getType());       
57         }
58
59         @Override
60         public IVal toVal(CompilationContext context, CodeWriter w) {
61         return value;
62     }
63
64     @Override
65     public void collectFreeVariables(THashSet<Variable> vars) {
66     }
67
68
69     @Override
70     public Expression simplify(SimplificationContext context) {
71         return this;
72     }
73
74     @Override
75     public Expression resolve(TranslationContext context) {
76         return this;
77     }
78     
79     @Override
80     public Expression resolveAsPattern(TranslationContext context) {
81         return this;
82     }
83     
84     @Override
85     public void getParameters(TranslationContext translationContext,
86             ArrayList<Expression> parameters) {
87     }
88
89     @Override
90     public Expression replace(ReplaceContext context) {
91         return new ELiteral(value);
92     }
93     
94     @Override
95     public void removeFreeVariables(THashSet<Variable> vars) {     
96     }
97     
98     @Override
99     public IExpression toIExpression(ExpressionInterpretationContext target) {
100         return new IConstant(value.realizeValue(target.localClassBuilder));
101     }
102     
103     @Override
104     public Expression inferType(TypingContext context) {
105         return this;
106     }
107
108     @Override
109     public void collectEffects(THashSet<Type> effects) {
110     }
111     
112     @Override
113     public void setLocationDeep(long loc) {
114         if(location == Locations.NO_LOCATION)
115             location = loc;
116     }
117     
118     @Override
119     public void accept(ExpressionVisitor visitor) {
120         visitor.visit(this);
121     }
122     
123     @Override
124     public boolean isPattern(int arity) {
125         return value.constructorTag() >= 0 && value.getArity() == arity;
126     }
127     
128     @Override
129     public Expression accept(ExpressionTransformer transformer) {
130         return transformer.transform(this);
131     }
132
133     @Override
134     public boolean equalsExpression(Expression expression) {
135         if(expression.getClass() != getClass())
136             return false;
137         ELiteral other = (ELiteral)expression;
138         return value.equals(other.value);
139     }
140
141 }