1 cananian 1.1.2.1 // CallGraph.java, created Thu Aug 24 17:06:06 2000 by cananian
 2 cananian 1.1.2.1 // Copyright (C) 2000 C. Scott Ananian <cananian@alumni.princeton.edu>
 3 cananian 1.1.2.1 // Licensed under the terms of the GNU GPL; see COPYING for details.
 4 cananian 1.1.2.1 package harpoon.Analysis;
 5 cananian 1.1.2.1 
 6 salcianu 1.3     import harpoon.ClassFile.HMethod;
 7 salcianu 1.3     
 8 salcianu 1.3     import harpoon.Util.Graphs.Navigator;
 9 salcianu 1.4     import harpoon.Util.Graphs.ForwardNavigator;
10 salcianu 1.3     import harpoon.Util.Graphs.DiGraph;
11 salcianu 1.3     
12 cananian 1.1.2.1 import java.util.Set;
13 cananian 1.1.2.1 
14 cananian 1.1.2.1 /**
15 cananian 1.1.2.1  * <code>CallGraph</code> is a general IR-independant interface that
16 cananian 1.1.2.1  * for a call graph.  IR-specific subclasses (see
17 cananian 1.1.2.1  * <code>harpoon.Analysis.Quads.CallGraph</code>) can provide
18 cananian 1.1.2.1  * call-site information.
19 cananian 1.1.2.1  * 
20 cananian 1.1.2.1  * @author  C. Scott Ananian <cananian@alumni.princeton.edu>
21 salcianu 1.6      * @version $Id: CallGraph.java,v 1.6 2004/03/04 22:31:44 salcianu Exp $
22 cananian 1.1.2.1  */
23 salcianu 1.3     public abstract class CallGraph extends DiGraph {
24 cananian 1.1.2.1     /** Returns an array containing all possible methods called by
25 cananian 1.1.2.1         method <code>m</code>. If <code>hm</code> doesn't call any 
26 cananian 1.1.2.1         method, return an array of length <code>0</code>. */
27 salcianu 1.3         public abstract HMethod[] calls(final HMethod hm);
28 salcianu 1.3     
29 salcianu 1.3         /** Returns the set of all the methods that can be called in the 
30 salcianu 1.3             execution of the program. */
31 cananian 1.5         public abstract Set<HMethod> callableMethods();
32 salcianu 1.3     
33 salcianu 1.3         /** @return set of all <code>run()</code> methods that may
34 salcianu 1.3             be the bodies of a thread started by the program (optional operation).
35 salcianu 1.3             @throws UnsupportedOperationException if <code>getRunMethods</code>
36 salcianu 1.3             is not implemented by <code>this</code>. */
37 salcianu 1.3         public Set getRunMethods() {
38 salcianu 1.3             throw new UnsupportedOperationException();
39 salcianu 1.3         }
40 salcianu 1.3     
41 salcianu 1.3     
42 salcianu 1.6         public Set<HMethod> getRoots() {
43 salcianu 1.3             // we simply return all the nodes (i.e., methods)
44 salcianu 1.3             return callableMethods();
45 salcianu 1.3         }
46 salcianu 1.3     
47 salcianu 1.4     
48 salcianu 1.3         /** Returns a bi-directional top-down graph navigator through
49 salcianu 1.4             <code>this</code> callgraph.  Result is internally cached. */
50 salcianu 1.6         public Navigator<HMethod> getNavigator() {
51 salcianu 1.4             if(navigator == null) {
52 salcianu 1.4                 final AllCallers ac = new AllCallers(this);
53 cananian 1.5                 navigator = new Navigator<HMethod>() {
54 cananian 1.5                     public HMethod[] next(HMethod node) {
55 cananian 1.5                         return calls(node);
56 salcianu 1.4                     }  
57 cananian 1.5                     public HMethod[] prev(HMethod node) {
58 cananian 1.5                         return ac.directCallers(node);
59 salcianu 1.4                     }
60 salcianu 1.4                 };
61 salcianu 1.4             }
62 salcianu 1.4             return navigator;
63 salcianu 1.4         }
64 salcianu 1.4         /** cached bi-directional navigator */
65 cananian 1.5         protected Navigator<HMethod> navigator = null;
66 salcianu 1.4     
67 salcianu 1.4     
68 salcianu 1.4         /** Returns a forward-only top-down graph navigator through
69 salcianu 1.4             <code>this</code> callgraph. */
70 salcianu 1.6         public ForwardNavigator<HMethod> getForwardNavigator() {
71 cananian 1.5             return new ForwardNavigator<HMethod>() {
72 cananian 1.5                 public HMethod[] next(HMethod node) {
73 cananian 1.5                     return calls(node);
74 salcianu 1.3                 }
75 salcianu 1.3             };
76 salcianu 1.3         }
77 cananian 1.2     }