1 package org.simantics.scl.compiler.internal.codegen.utils;
\r
3 import gnu.trove.map.hash.THashMap;
\r
5 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
\r
6 import org.simantics.scl.compiler.internal.codegen.continuations.Cont;
\r
7 import org.simantics.scl.compiler.internal.codegen.continuations.ContRef;
\r
8 import org.simantics.scl.compiler.internal.codegen.references.BoundVar;
\r
9 import org.simantics.scl.compiler.internal.codegen.references.Val;
\r
10 import org.simantics.scl.compiler.internal.codegen.references.ValRef;
\r
11 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;
\r
12 import org.simantics.scl.compiler.types.TVar;
\r
13 import org.simantics.scl.compiler.types.Type;
\r
14 import org.simantics.scl.compiler.types.Types;
\r
16 public class CopyContext {
\r
18 THashMap<Val, Val> valMap = new THashMap<Val, Val>();
\r
19 THashMap<Cont, Cont> contMap = new THashMap<Cont, Cont>();
\r
20 THashMap<TVar, TVar> tvarMap = new THashMap<TVar, TVar>();
\r
22 public void put(Val src, Val tgt) {
\r
23 Val ret = valMap.put(src, tgt);
\r
24 if(SCLCompilerConfiguration.DEBUG) {
\r
26 throw new InternalCompilerError();
\r
30 public void put(Cont src, Cont tgt) {
\r
31 Cont ret = contMap.put(src, tgt);
\r
32 if(SCLCompilerConfiguration.DEBUG) {
\r
34 throw new InternalCompilerError();
\r
38 public TVar[] copyParameters(TVar[] vars) {
\r
39 TVar[] result = new TVar[vars.length];
\r
40 for(int i=0;i<vars.length;++i) {
\r
42 TVar newVar = Types.var(var.getKind());
\r
44 tvarMap.put(var, newVar);
\r
49 public BoundVar[] copy(BoundVar[] src) {
\r
50 BoundVar[] tgt = new BoundVar[src.length];
\r
51 for(int i=0;i<src.length;++i)
\r
52 tgt[i] = copy(src[i]);
\r
56 public ValRef[] copy(ValRef[] src) {
\r
57 ValRef[] tgt = new ValRef[src.length];
\r
58 for(int i=0;i<src.length;++i)
\r
59 tgt[i] = copy(src[i]);
\r
63 public ValRef copy(ValRef src) {
\r
64 return copy(src.getBinding())
\r
65 .createOccurrence(Types.replace(src.getTypeParameters(), tvarMap));
\r
68 @SuppressWarnings("unchecked")
\r
69 public <T extends Val> T copy(T src) {
\r
70 Val tgt = valMap.get(src);
\r
73 tgt = src.copy(tvarMap);
\r
74 valMap.put(src, tgt);
\r
78 public ContRef[] copy(ContRef[] src) {
\r
79 ContRef[] tgt = new ContRef[src.length];
\r
80 for(int i=0;i<src.length;++i)
\r
81 tgt[i] = copy(src[i]);
\r
85 public ContRef copy(ContRef src) {
\r
86 return copy(src.getBinding()).createOccurrence();
\r
89 @SuppressWarnings("unchecked")
\r
90 public <T extends Cont> T copy(T src) {
\r
91 Cont tgt = contMap.get(src);
\r
94 tgt = src.copy(this);
\r
95 contMap.put(src, tgt);
\r
99 public Type copyType(Type type) {
\r
100 return type.replace(tvarMap);
\r