]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EExternalConstant.java
1e8341e66cbd4a7f408c8cd05ef6fe4d70bbd30e
[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         public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
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             throw new InternalCompilerError("EExternalConstants must have explicitly defined type.");
53         }
54         
55         @Override
56         public IVal toVal(CompilationContext context, CodeWriter w) {
57                 ModuleWriter mw = w.getModuleWriter();
58                 return mw.getExternalConstant(value, getType());
59     }
60
61     @Override
62     public void collectFreeVariables(THashSet<Variable> vars) {
63     }
64
65
66     @Override
67     public Expression simplify(SimplificationContext context) {
68         return this;
69     }
70
71     @Override
72     public Expression resolve(TranslationContext context) {
73         return this;
74     }
75     
76     @Override
77     public Expression resolveAsPattern(TranslationContext context) {
78         return this;
79     }
80     
81     @Override
82     public void getParameters(TranslationContext translationContext,
83             ArrayList<Expression> parameters) {
84     }
85
86     @Override
87     public Expression replace(ReplaceContext context) {
88         return new EExternalConstant(value, getType().replace(context.tvarMap));
89     }
90     
91     @Override
92     public void removeFreeVariables(THashSet<Variable> vars) {     
93     }
94     
95     @Override
96     public Expression inferType(TypingContext context) {
97         return this;
98     }
99
100     @Override
101     public void collectEffects(THashSet<Type> effects) {
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 IExpression toIExpression(ExpressionInterpretationContext context) {
117         return new IConstant(value);
118     }
119
120     @Override
121     public void forVariables(VariableProcedure procedure) {
122     }
123     
124     @Override
125     public Expression accept(ExpressionTransformer transformer) {
126         return transformer.transform(this);
127     }
128
129 }