]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/util/TypeListener.java
migrated to svn revision 33108
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / types / util / TypeListener.java
1 package org.simantics.scl.compiler.types.util;
2
3 import java.util.ArrayList;
4
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;
13
14 public abstract class TypeListener {
15     private ArrayList<TMetaVarListener> metaVarListeners = new ArrayList<TMetaVarListener>(2);
16     public abstract void notifyAboutChange();
17     
18     private static class SubListener extends TMetaVarListener {
19         private final TypeListener parent;
20         public SubListener(TypeListener parent) {
21             this.parent = parent;
22         }        @Override
23         public void notifyAboutChange() {
24             for(TMetaVarListener otherListeners : parent.metaVarListeners)
25                 otherListeners.remove();
26             parent.notifyAboutChange();
27         }
28     };
29     
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)
37                 return;
38             
39             SubListener subListener = new SubListener(this);
40             metaVarListeners.add(subListener);
41             metaVar.addListener(subListener);
42         }
43         else if(type instanceof TApply) {
44             TApply apply = (TApply)type;
45             listenSkeleton(apply.function);
46             listenSkeleton(apply.parameter);
47         }
48         else if(type instanceof TFun) {
49             TFun fun = (TFun)type;
50             listenSkeleton(fun.domain);
51             listenSkeleton(fun.range);
52         }
53         else if(type instanceof TForAll) {
54             TForAll forAll = (TForAll)type;
55             listenSkeleton(forAll.type);
56         }
57         else if(type instanceof TPred) {
58             TPred pred = (TPred)type;
59             for(Type parameter : pred.parameters)
60                 listenSkeleton(parameter);
61         }
62     }
63 }