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