1 package org.simantics.scl.compiler.types.util;
3 import java.util.ArrayList;
5 import org.simantics.scl.compiler.types.Skeletons;
6 import org.simantics.scl.compiler.types.TApply;
7 import org.simantics.scl.compiler.types.TForAll;
8 import org.simantics.scl.compiler.types.TFun;
9 import org.simantics.scl.compiler.types.TMetaVar;
10 import org.simantics.scl.compiler.types.TMetaVar.TMetaVarListener;
11 import org.simantics.scl.compiler.types.TPred;
12 import org.simantics.scl.compiler.types.Type;
14 public abstract class TypeListener {
15 private ArrayList<TMetaVarListener> metaVarListeners = new ArrayList<TMetaVarListener>(2);
16 public abstract void notifyAboutChange();
18 private static class SubListener extends TMetaVarListener {
19 private final TypeListener parent;
20 public SubListener(TypeListener parent) {
23 public void notifyAboutChange() {
24 for(TMetaVarListener otherListeners : parent.metaVarListeners)
25 otherListeners.remove();
26 parent.notifyAboutChange();
30 public void listenSkeleton(Type type) {
31 type = Skeletons.canonicalSkeleton(type);
32 if(type instanceof TMetaVar) {
33 TMetaVar metaVar = (TMetaVar)type;
34 TMetaVarListener latestListener = metaVar.getLatestListener();
35 if(latestListener instanceof SubListener &&
36 ((SubListener)latestListener).parent == this)
39 SubListener subListener = new SubListener(this);
40 metaVarListeners.add(subListener);
41 metaVar.addListener(subListener);
43 else if(type instanceof TApply) {
44 TApply apply = (TApply)type;
45 listenSkeleton(apply.function);
46 listenSkeleton(apply.parameter);
48 else if(type instanceof TFun) {
49 TFun fun = (TFun)type;
50 listenSkeleton(fun.domain);
51 listenSkeleton(fun.range);
53 else if(type instanceof TForAll) {
54 TForAll forAll = (TForAll)type;
55 listenSkeleton(forAll.type);
57 else if(type instanceof TPred) {
58 TPred pred = (TPred)type;
59 for(Type parameter : pred.parameters)
60 listenSkeleton(parameter);