01: // Copyright (c) 2003 Per M.A. Bothner.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.expr;
05:
06: /** This resolves references to lexical Declarations.
07: * So far it is only used for XQuery, which overrides it. */
08:
09: public class ResolveNames extends ExpWalker {
10: protected NameLookup lookup;
11:
12: public ResolveNames() {
13: }
14:
15: public ResolveNames(Compilation comp) {
16: setContext(comp);
17: lookup = comp.lexical;
18: }
19:
20: public void resolveModule(ModuleExp exp) {
21: Compilation save_comp = Compilation.getCurrent();
22: try {
23: if (comp != null)
24: Compilation.setCurrent(comp);
25: push(exp);
26: exp.walkChildren(this );
27: } finally {
28: Compilation.setCurrent(save_comp);
29: // Note we don't do lookup.pop(exp). This is so top-level
30: // declarations remain for future uses of the same Lexer.
31: }
32: }
33:
34: protected void push(ScopeExp exp) {
35: lookup.push(exp);
36: }
37:
38: protected Expression walkScopeExp(ScopeExp exp) {
39: walkDeclarationTypes(exp);
40: push(exp);
41: exp.walkChildren(this );
42: lookup.pop(exp);
43: return exp;
44: }
45:
46: protected Expression walkLetExp(LetExp exp) {
47: walkDeclarationTypes(exp);
48: exp.walkInitializers(this );
49: push(exp);
50: exp.body = (Expression) walk(exp.body);
51: lookup.pop(exp);
52: return exp;
53: }
54:
55: public Declaration lookup(Expression exp, Object symbol,
56: boolean function) {
57: return lookup.lookup(symbol, function);
58: }
59:
60: protected Expression walkReferenceExp(ReferenceExp exp) {
61: Declaration decl = exp.getBinding();
62: if (decl == null) {
63: decl = lookup(exp, exp.getSymbol(), exp.isProcedureName());
64: if (decl != null)
65: exp.setBinding(decl);
66: }
67: return exp;
68: }
69:
70: protected Expression walkSetExp(SetExp exp) {
71: if (exp.binding == null)
72: exp.binding = lookup(exp, exp.getSymbol(), exp.isFuncDef());
73: return super.walkSetExp(exp);
74: }
75: }
|