KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > expr > ResolveNames


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

4 package gnu.expr;
5
6 /** This resolves references to lexical Declarations.
7  * So far it is only used for XQuery, which overrides it. */

8
9 public class ResolveNames extends ExpWalker
10 {
11   protected NameLookup lookup;
12
13   public ResolveNames ()
14   {
15   }
16
17   public ResolveNames (Compilation comp)
18   {
19     setContext(comp);
20     lookup = comp.lexical;
21   }
22
23   public void resolveModule(ModuleExp exp)
24   {
25     Compilation save_comp = Compilation.getCurrent();
26     try
27       {
28         if (comp != null)
29           Compilation.setCurrent(comp);
30         push(exp);
31         exp.walkChildren(this);
32       }
33     finally
34       {
35         Compilation.setCurrent(save_comp);
36         // Note we don't do lookup.pop(exp). This is so top-level
37
// declarations remain for future uses of the same Lexer.
38
}
39   }
40
41   protected void push (ScopeExp exp)
42   {
43     lookup.push(exp);
44   }
45
46   protected Expression walkScopeExp (ScopeExp exp)
47   {
48     walkDeclarationTypes(exp);
49     push(exp);
50     exp.walkChildren(this);
51     lookup.pop(exp);
52     return exp;
53   }
54
55   protected Expression walkLetExp (LetExp exp)
56   {
57     walkDeclarationTypes(exp);
58     exp.walkInitializers(this);
59     push(exp);
60     exp.body = (Expression) walk(exp.body);
61     lookup.pop(exp);
62     return exp;
63   }
64
65   public Declaration lookup (Expression exp, Object JavaDoc symbol, boolean function)
66   {
67     return lookup.lookup(symbol, function);
68   }
69
70   protected Expression walkReferenceExp (ReferenceExp exp)
71   {
72     Declaration decl = exp.getBinding();
73     if (decl == null)
74       {
75     decl = lookup(exp, exp.getSymbol(), exp.isProcedureName());
76     if (decl != null)
77       exp.setBinding(decl);
78       }
79     return exp;
80  }
81
82   protected Expression walkSetExp (SetExp exp)
83   {
84     if (exp.binding == null)
85       exp.binding = lookup(exp, exp.getSymbol(), exp.isFuncDef());
86     return super.walkSetExp(exp);
87   }
88 }
89
Popular Tags