]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/utils/CopyContext.java
(refs #7250) Merging master, minor CHR bugfixes
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / codegen / utils / CopyContext.java
1 package org.simantics.scl.compiler.internal.codegen.utils;
2
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;
13
14 import gnu.trove.map.hash.THashMap;
15
16 public class CopyContext {
17
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>(); 
21     
22     public void put(Val src, Val tgt) {
23         Val ret = valMap.put(src, tgt);
24         if(SCLCompilerConfiguration.DEBUG) {
25             if(ret != null)
26                 throw new InternalCompilerError();
27         }
28     }
29
30     public void put(Cont src, Cont tgt) {
31         Cont ret = contMap.put(src, tgt);
32         if(SCLCompilerConfiguration.DEBUG) {
33             if(ret != null)
34                 throw new InternalCompilerError();
35         }
36     }
37     
38     public TVar[] copyParameters(TVar[] vars) {
39         TVar[] result = new TVar[vars.length];
40         for(int i=0;i<vars.length;++i) {
41             TVar var = vars[i];
42             TVar newVar = Types.var(var.getKind());
43             result[i] = newVar;
44             tvarMap.put(var, newVar);
45         }
46         return result;
47     }
48
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]);
53         return tgt;
54     }
55     
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]);
60         return tgt;
61     }
62     
63     public ValRef copy(ValRef src) {
64         return copy(src.getBinding())
65                 .createOccurrence(Types.replace(src.getTypeParameters(), tvarMap));
66     }
67
68     @SuppressWarnings("unchecked")
69     public <T extends Val> T copy(T src) {
70         Val tgt = valMap.get(src);
71         if(tgt != null)
72             return (T)tgt;
73         tgt = src.copy(tvarMap);
74         valMap.put(src, tgt);
75         return (T)tgt;
76     }
77     
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]);
82         return tgt;
83     }
84     
85     public ContRef copy(ContRef src) {
86         return copy(src.getBinding()).createOccurrence();
87     }
88
89     @SuppressWarnings("unchecked")
90     public <T extends Cont> T copy(T src) {
91         Cont tgt = contMap.get(src);
92         if(tgt != null)
93             return (T)tgt;
94         tgt = src.copy(this);
95         contMap.put(src, tgt);
96         return (T)tgt;
97     }
98
99     public Type copyType(Type type) {
100         return type.replace(tvarMap); 
101     }
102
103 }