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