KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > bytecode > Scope


1 // Copyright (c) 1997, 2004 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.bytecode;
5
6 public class Scope
7 {
8   /** The enclosing scope. */
9   Scope parent;
10   Scope nextSibling;
11   Scope firstChild, lastChild;
12
13   /** If true, don't call freeLocal on our variables (yet). */
14   boolean preserved;
15
16   Label start;
17   Label end;
18   Variable vars;
19   Variable last_var;
20
21   public Scope()
22   {
23   }
24
25   public Scope (Label start, Label end)
26   {
27     this.start = start;
28     this.end = end;
29   }
30
31   // Variable lookup (String name);
32
public final Variable firstVar () { return vars; }
33
34   public VarEnumerator allVars () { return new VarEnumerator (this); }
35
36   /** Link this scope as the next child of its parent scope. */
37   public void linkChild (Scope parent)
38   {
39     this.parent = parent;
40     if (parent == null)
41       return;
42     if (parent.lastChild == null)
43       parent.firstChild = this;
44     else
45       parent.lastChild.nextSibling = this;
46     parent.lastChild = this;
47   }
48
49   public Variable addVariable (CodeAttr code, Type type, String JavaDoc name)
50   {
51     Variable var = new Variable(name, type);
52     addVariable (code, var);
53     return var;
54    }
55
56   public void addVariable (Variable var)
57   {
58     if (last_var == null)
59       vars = var;
60     else
61       last_var.next = var;
62     last_var = var;
63     var.scope = this;
64   }
65
66   /* Add a new Variable, linking it in after a given Variable, */
67   public void addVariableAfter (Variable prev, Variable var)
68   {
69     if (prev == null)
70       { // Put first
71
var.next = vars;
72     vars = var;
73       }
74     else
75       {
76     var.next = prev.next;
77     prev.next = var;
78       }
79     if (last_var == prev)
80       last_var = var;
81     if (var.next == var)
82       throw new Error JavaDoc("cycle");
83     var.scope = this;
84   }
85
86   public void addVariable (CodeAttr code, Variable var)
87   {
88     addVariable (var);
89     if (var.isSimple() && code != null)
90       var.allocateLocal(code);
91   }
92
93   /**
94    * Return a variable the scope, by numerical index.
95    * @param index the number of the variable
96    */

97   public Variable getVariable(int index) {
98     Variable var = vars;
99     while (--index >= 0)
100       var = var.next;
101     return var;
102   }
103
104   static boolean equals (byte[] name1, byte[] name2) {
105     if (name1.length != name2.length)
106       return false;
107     if (name1 == name2)
108       return true;
109     for (int i = name1.length; --i >= 0; )
110       if (name1[i] != name2[i])
111     return false;
112     return true;
113   }
114
115   public void setStartPC(CodeAttr code)
116   {
117     start = code.getLabel();
118   }
119
120   /**
121    * Search by name for a Variable in this Scope (only).
122    * @param name name to search for
123    * @return the Variable, or null if not found (in this scope).
124    */

125   public Variable lookup (String JavaDoc name) {
126     for (Variable var = vars; var != null; var = var.next) {
127       if (name.equals(var.name))
128     return var;
129     }
130     return null;
131   }
132
133   /** Make local variable slots of this scope availabel for re-use.
134    * However, if the 'preserved' flag is set, defer doing so until
135    * we exit a non-preserved Scope. */

136   void freeLocals (CodeAttr code)
137   {
138     if (preserved)
139       return;
140     for (Variable var = vars; var != null; var = var.next)
141       {
142     if (var.isSimple () && ! var.dead ())
143       var.freeLocal(code);
144       }
145     for (Scope child = firstChild; child != null; child = child.nextSibling)
146       {
147     if (child.preserved)
148       {
149         child.preserved = false;
150         child.freeLocals(code);
151       }
152       }
153   }
154 };
155
Popular Tags