KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > expr > ThisExp


1 package gnu.expr;
2 import gnu.bytecode.*;
3 import gnu.mapping.*;
4
5 /** Evaluates to the "this" implicit variable.
6  * This is currently neither robust nor general. FIXME!
7  */

8
9 public class ThisExp extends ReferenceExp
10 {
11   /** Non-interned name for implicit 'this' variable. */
12   public static final String JavaDoc THIS_NAME = new String JavaDoc("$this$");
13
14   /** When evaluating, return the context.
15    * This is used for the "context" of a Macro.
16    */

17   static int EVAL_TO_CONTEXT = NEXT_AVAIL_FLAG;
18
19   /** The class which this refers to. */
20   ScopeExp context;
21
22   /** If this is being used to pass the context instance to a Macro. */
23   public final boolean isForContext ()
24   {
25     return (flags & EVAL_TO_CONTEXT) != 0;
26   }
27
28   public void apply (CallContext ctx)
29     throws Throwable JavaDoc
30   {
31     if (isForContext())
32       ctx.writeValue(context);
33     else
34       super.apply(ctx);
35   }
36
37   public ScopeExp getContextScope () { return context; }
38
39   public ThisExp ()
40   {
41     super(THIS_NAME);
42   }
43
44   public ThisExp(ScopeExp context)
45   {
46     super(THIS_NAME);
47     this.context = context;
48   }
49
50   public ThisExp (Declaration binding)
51   {
52     super(THIS_NAME, binding);
53   }
54
55   public ThisExp (ClassType type)
56   {
57     this(new Declaration(THIS_NAME, type));
58   }
59
60   public static ThisExp makeGivingContext (ScopeExp context)
61   {
62     ThisExp exp = new ThisExp(context);
63     exp.flags |= EVAL_TO_CONTEXT;
64     return exp;
65   }
66
67   public void compile (Compilation comp, Target target)
68   {
69     if (target instanceof IgnoreTarget)
70       return;
71     if (isForContext())
72       {
73         // This is an extension used by define_syntax.
74
CodeAttr code = comp.getCode();
75         if (comp.method.getStaticFlag())
76           code.emitGetStatic(comp.moduleInstanceMainField);
77         else
78           code.emitPushThis();
79         target.compileFromStack(comp, getType());
80       }
81     else
82       {
83         super.compile(comp, target);
84       }
85   }
86
87   protected Expression walk (ExpWalker walker)
88   {
89     return walker.walkThisExp(this);
90   }
91
92   public final gnu.bytecode.Type getType()
93   {
94     if (binding != null)
95       return binding.getType();
96     if (context instanceof ClassExp || context instanceof ModuleExp)
97       return context.getType();
98     return Type.pointer_type;
99   }
100 }
101
Popular Tags