1 package org.simantics.scl.compiler.internal.types.effects;
3 import gnu.trove.map.hash.TObjectIntHashMap;
5 import java.util.ArrayList;
6 import java.util.Collection;
8 import org.simantics.scl.compiler.types.TCon;
9 import org.simantics.scl.compiler.types.TMetaVar;
10 import org.simantics.scl.compiler.types.TUnion;
11 import org.simantics.scl.compiler.types.Type;
12 import org.simantics.scl.compiler.types.Types;
14 public class EffectIdMap {
16 public static final int MIN = 0;
17 public static final int MAX = 0xffffffff;
18 private static final int FIRST_FREE_EFFECT_ID = 16;
20 private static ArrayList<TCon> effectCons = new ArrayList<TCon>();
21 private static TObjectIntHashMap<TCon> effectCodes = new TObjectIntHashMap<TCon>();
23 private static void add(String module, String name, int code) {
24 TCon con = Types.con(module, name);
25 effectCodes.put(con, code);
30 add("Simantics/DB", "WriteGraph", 12);
31 add("Simantics/DB", "ReadGraph", 4);
32 add(Types.BUILTIN, "Proc", 1);
35 private ArrayList<Type> localCons = new ArrayList<Type>();
36 private TObjectIntHashMap<Type> localCodes = new TObjectIntHashMap<Type>();
37 private int freshId = FIRST_FREE_EFFECT_ID;
39 public int toId(Type type, Collection<TMetaVar> metaVars) {
40 type = Types.canonical(type);
41 if(type instanceof TUnion) {
43 for(Type e : ((TUnion)type).effects)
44 id |= toId(e, metaVars);
47 else if(effectCodes.contains(type)) {
48 return effectCodes.get(type);
50 else if(type instanceof TMetaVar) {
51 metaVars.add((TMetaVar)type);
54 else if(localCodes.contains(type)) {
55 return localCodes.get(type);
60 localCodes.put(type, id);
66 public Type toType(int id) {
68 return Types.NO_EFFECTS;
69 ArrayList<Type> components = new ArrayList<Type>();
70 for(TCon con : effectCons) {
71 int conId = effectCodes.get(con);
72 if((id&conId) == conId) {
77 for(Type con : localCons) {
78 int conId = localCodes.get(con);
79 if((id&conId) == conId) {
84 if(components.size() == 1)
85 return components.get(0);
87 return Types.union(components);