]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/utils/CopyContext.java
migrated to svn revision 33108
[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;\r
2 \r
3 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;\r
4 import org.simantics.scl.compiler.internal.codegen.continuations.Cont;\r
5 import org.simantics.scl.compiler.internal.codegen.continuations.ContRef;\r
6 import org.simantics.scl.compiler.internal.codegen.references.BoundVar;\r
7 import org.simantics.scl.compiler.internal.codegen.references.Val;\r
8 import org.simantics.scl.compiler.internal.codegen.references.ValRef;\r
9 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;\r
10 import org.simantics.scl.compiler.types.TVar;\r
11 import org.simantics.scl.compiler.types.Type;\r
12 import org.simantics.scl.compiler.types.Types;\r
13 \r
14 import gnu.trove.map.hash.THashMap;\r
15 \r
16 public class CopyContext {\r
17 \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
21     \r
22     public void put(Val src, Val tgt) {\r
23         Val ret = valMap.put(src, tgt);\r
24         if(SCLCompilerConfiguration.DEBUG) {\r
25             if(ret != null)\r
26                 throw new InternalCompilerError();\r
27         }\r
28     }\r
29 \r
30     public void put(Cont src, Cont tgt) {\r
31         Cont ret = contMap.put(src, tgt);\r
32         if(SCLCompilerConfiguration.DEBUG) {\r
33             if(ret != null)\r
34                 throw new InternalCompilerError();\r
35         }\r
36     }\r
37     \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
41             TVar var = vars[i];\r
42             TVar newVar = Types.var(var.getKind());\r
43             result[i] = newVar;\r
44             tvarMap.put(var, newVar);\r
45         }\r
46         return result;\r
47     }\r
48 \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
53         return tgt;\r
54     }\r
55     \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
60         return tgt;\r
61     }\r
62     \r
63     public ValRef copy(ValRef src) {\r
64         return copy(src.getBinding())\r
65                 .createOccurrence(Types.replace(src.getTypeParameters(), tvarMap));\r
66     }\r
67 \r
68     @SuppressWarnings("unchecked")\r
69     public <T extends Val> T copy(T src) {\r
70         Val tgt = valMap.get(src);\r
71         if(tgt != null)\r
72             return (T)tgt;\r
73         tgt = src.copy(tvarMap);\r
74         valMap.put(src, tgt);\r
75         return (T)tgt;\r
76     }\r
77     \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
82         return tgt;\r
83     }\r
84     \r
85     public ContRef copy(ContRef src) {\r
86         return copy(src.getBinding()).createOccurrence();\r
87     }\r
88 \r
89     @SuppressWarnings("unchecked")\r
90     public <T extends Cont> T copy(T src) {\r
91         Cont tgt = contMap.get(src);\r
92         if(tgt != null)\r
93             return (T)tgt;\r
94         tgt = src.copy(this);\r
95         contMap.put(src, tgt);\r
96         return (T)tgt;\r
97     }\r
98 \r
99     public Type copyType(Type type) {\r
100         return type.replace(tvarMap); \r
101     }\r
102 \r
103 }\r