]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EExternalConstant.java
(refs #7375) Replace collectRefs by CollectRefsVisitor
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / EExternalConstant.java
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3 import java.util.ArrayList;
4
5 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
6 import org.simantics.scl.compiler.compilation.CompilationContext;
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.codegen.writer.ModuleWriter;
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 EExternalConstant extends Expression {
27     Object value;
28     
29     public EExternalConstant(Object value, Type type) {
30         this.value = value;
31         setType(type);
32     }
33     
34     public Object getValue() {
35         return value;
36     }
37
38     @Override
39     public void collectVars(TObjectIntHashMap<Variable> allVars,
40             TIntHashSet vars) {     
41     }
42
43     public void toString(StringBuilder b, TypeUnparsingContext tuc) {
44         b.append(value);
45     }
46
47     @Override
48     protected void updateType() throws MatchException {
49         throw new InternalCompilerError("EExternalConstants must have explicitly defined type.");
50     }
51
52     @Override
53     public IVal toVal(CompilationContext context, CodeWriter w) {
54         ModuleWriter mw = w.getModuleWriter();
55         return mw.getExternalConstant(value, getType());
56     }
57
58     @Override
59     public void collectFreeVariables(THashSet<Variable> vars) {
60     }
61
62
63     @Override
64     public Expression simplify(SimplificationContext context) {
65         return this;
66     }
67
68     @Override
69     public Expression resolve(TranslationContext context) {
70         return this;
71     }
72     
73     @Override
74     public Expression resolveAsPattern(TranslationContext context) {
75         return this;
76     }
77     
78     @Override
79     public void getParameters(TranslationContext translationContext,
80             ArrayList<Expression> parameters) {
81     }
82
83     @Override
84     public Expression replace(ReplaceContext context) {
85         return new EExternalConstant(value, getType().replace(context.tvarMap));
86     }
87     
88     @Override
89     public void removeFreeVariables(THashSet<Variable> vars) {     
90     }
91     
92     @Override
93     public Expression inferType(TypingContext context) {
94         return this;
95     }
96
97     @Override
98     public void collectEffects(THashSet<Type> effects) {
99     }
100     
101     @Override
102     public void setLocationDeep(long loc) {
103         if(location == Locations.NO_LOCATION)
104             location = loc;
105     }
106     
107     @Override
108     public void accept(ExpressionVisitor visitor) {
109         visitor.visit(this);
110     }
111     
112     @Override
113     public IExpression toIExpression(ExpressionInterpretationContext context) {
114         return new IConstant(value);
115     }
116     
117     @Override
118     public Expression accept(ExpressionTransformer transformer) {
119         return transformer.transform(this);
120     }
121
122 }