]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ELiteral.java
(refs #7278, refs #7279) Small fixes to InternalCompilerExceptions
[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.elaboration.utils.ExpressionDecorator;
15 import org.simantics.scl.compiler.internal.interpreted.IConstant;
16 import org.simantics.scl.compiler.internal.interpreted.IExpression;
17 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
18 import org.simantics.scl.compiler.types.Type;
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.THashSet;
24 import gnu.trove.set.hash.TIntHashSet;
25
26 public class ELiteral extends Expression {
27     Constant value;
28     
29     public ELiteral(Constant value) {
30         this.value = value;
31         setType(value.getType());
32     }
33
34     public ELiteral(long loc, Constant value) {
35         super(loc);
36         this.value = value;
37     }
38
39     public Constant getValue() {
40         return value;
41     }
42
43         public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
44     }
45
46         @Override
47         public void collectVars(TObjectIntHashMap<Variable> allVars,
48                 TIntHashSet vars) {         
49         }
50         
51         public void toString(StringBuilder b, TypeUnparsingContext tuc) {
52         b.append(value);
53     }
54         
55         @Override
56         protected void updateType() throws MatchException {
57             setType(value.getType());       
58         }
59
60         @Override
61         public IVal toVal(CompilationContext context, CodeWriter w) {
62         return value;
63     }
64
65     @Override
66     public void collectFreeVariables(THashSet<Variable> vars) {
67     }
68
69
70     @Override
71     public Expression simplify(SimplificationContext context) {
72         return this;
73     }
74
75     @Override
76     public Expression resolve(TranslationContext context) {
77         return this;
78     }
79     
80     @Override
81     public Expression resolveAsPattern(TranslationContext context) {
82         return this;
83     }
84     
85     @Override
86     public void getParameters(TranslationContext translationContext,
87             ArrayList<Expression> parameters) {
88     }
89
90     @Override
91     public Expression replace(ReplaceContext context) {
92         return new ELiteral(value);
93     }
94     
95     @Override
96     public void removeFreeVariables(THashSet<Variable> vars) {     
97     }
98     
99     @Override
100     public IExpression toIExpression(ExpressionInterpretationContext target) {
101         return new IConstant(value.realizeValue(target.localClassBuilder));
102     }
103     
104     @Override
105     public Expression inferType(TypingContext context) {
106         return this;
107     }
108
109     @Override
110     public Expression decorate(ExpressionDecorator decorator) {
111         return decorator.decorate(this);
112     }
113
114     @Override
115     public void collectEffects(THashSet<Type> effects) {
116     }
117     
118     @Override
119     public void setLocationDeep(long loc) {
120         if(location == Locations.NO_LOCATION)
121             location = loc;
122     }
123     
124     @Override
125     public void accept(ExpressionVisitor visitor) {
126         visitor.visit(this);
127     }
128
129     @Override
130     public void forVariables(VariableProcedure procedure) {
131     }
132     
133     @Override
134     public boolean isPattern(int arity) {
135         return value.constructorTag() >= 0 && value.getArity() == arity;
136     }
137     
138     @Override
139     public Expression accept(ExpressionTransformer transformer) {
140         return transformer.transform(this);
141     }
142
143     @Override
144     public boolean equalsExpression(Expression expression) {
145         if(expression.getClass() != getClass())
146             return false;
147         ELiteral other = (ELiteral)expression;
148         return value.equals(other.value);
149     }
150
151 }