1 package org.simantics.scl.compiler.internal.types.effects;
3 import java.util.ArrayList;
4 import java.util.Collection;
6 import org.simantics.scl.compiler.types.TCon;
7 import org.simantics.scl.compiler.types.TMetaVar;
8 import org.simantics.scl.compiler.types.TUnion;
9 import org.simantics.scl.compiler.types.Type;
10 import org.simantics.scl.compiler.types.Types;
12 import gnu.trove.map.hash.TObjectIntHashMap;
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> EFFECT_CONS = new ArrayList<TCon>();
21 private static TObjectIntHashMap<TCon> EFFECT_CODES = new TObjectIntHashMap<TCon>();
23 private static void add(TCon con, int code) {
24 EFFECT_CODES.put(con, code);
29 add(Types.WRITE_GRAPH, 0b11);
30 add(Types.READ_GRAPH, 0b01);
33 private ArrayList<Type> localCons = new ArrayList<Type>();
34 private TObjectIntHashMap<Type> localCodes = new TObjectIntHashMap<Type>();
35 private int freshId = FIRST_FREE_EFFECT_ID;
37 public int toId(Type type, Collection<TMetaVar> metaVars) {
38 type = Types.canonical(type);
39 if(type instanceof TUnion) {
41 for(Type e : ((TUnion)type).effects)
42 id |= toId(e, metaVars);
45 else if(EFFECT_CODES.contains(type)) {
46 return EFFECT_CODES.get(type);
48 else if(type instanceof TMetaVar) {
49 metaVars.add((TMetaVar)type);
52 else if(localCodes.contains(type)) {
53 return localCodes.get(type);
58 localCodes.put(type, id);
64 public Type toType(int id) {
66 return Types.NO_EFFECTS;
67 ArrayList<Type> components = new ArrayList<Type>();
68 for(TCon con : EFFECT_CONS) {
69 int conId = EFFECT_CODES.get(con);
70 if((id&conId) == conId) {
75 for(Type con : localCons) {
76 int conId = localCodes.get(con);
77 if((id&conId) == conId) {
82 if(components.size() == 1)
83 return components.get(0);
85 return Types.union(components);