KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > expr > BindingInitializer


1 package gnu.expr;
2 import gnu.bytecode.*;
3 import gnu.mapping.*;
4
5 public class BindingInitializer extends Initializer
6 {
7   Declaration decl;
8   Expression value;
9
10   /** Create a BindingInitializer and link it into the correct
11    * intializer chain. */

12   public static void create (Declaration decl, Expression value,
13                              Compilation comp)
14   {
15     BindingInitializer init = new BindingInitializer(decl, value);
16     if (decl.field != null && decl.field.getStaticFlag())
17       {
18         init.next = comp.clinitChain;
19         comp.clinitChain = init;
20       }
21     else
22       {
23         init.next = comp.mainLambda.initChain; // FIXME why mainLambda?
24
comp.mainLambda.initChain = init;
25       }
26   }
27
28   public BindingInitializer(Declaration decl, Expression value)
29   {
30     this.decl = decl;
31     this.value = value;
32     this.field = decl.field;
33   }
34
35   public void emit(Compilation comp)
36   {
37     CodeAttr code = comp.getCode();
38
39     if (value instanceof QuoteExp)
40       {
41     Object JavaDoc val = ((QuoteExp) value).getValue();
42     if (val != null && ! (val instanceof String JavaDoc))
43       {
44         Literal lit = comp.litTable.findLiteral(val);
45         if (lit.field == this.field)
46           return;
47       }
48       }
49
50     int line = decl.getLineNumber();
51     if (line > 0)
52       code.putLineNumber(decl.getFileName(), line);
53
54     if (field != null && ! field.getStaticFlag())
55       code.emitPushThis();
56
57     if (value == null)
58       {
59     boolean func = comp.getLanguage().hasSeparateFunctionNamespace();
60     Object JavaDoc property
61       = func && decl.isProcedureDecl() ? EnvironmentKey.FUNCTION : null;
62
63     Object JavaDoc name = decl.getSymbol();
64
65     if (decl.isAlias())
66           {
67             comp.compileConstant(name, Target.pushObject);
68             code.emitInvokeStatic(makeLocationMethod(name));
69           }
70     else
71       {
72         ClassType t = ClassType.make("gnu.mapping.ThreadLocation");
73         if (decl.getFlag(Declaration.IS_UNKNOWN
74                  |Declaration.IS_DYNAMIC|Declaration.IS_FLUID))
75           {
76                 if (name instanceof String JavaDoc)
77                   name = Namespace.EmptyNamespace.getSymbol((String JavaDoc) name);
78                 comp.compileConstant(name, Target.pushObject);
79         if (property == null)
80           code.emitPushNull();
81         else
82           comp.compileConstant(property, Target.pushObject);
83         code.emitInvokeStatic(t.getDeclaredMethod("getInstance", 2));
84           }
85         else
86           {
87                 Type[] atypes = new Type[1];
88                 if (name instanceof Symbol)
89                   atypes[0] = Compilation.typeSymbol;
90                 else
91                   atypes[0] = Type.tostring_type;
92                 comp.compileConstant(name, Target.pushObject);
93         code.emitInvokeStatic(t.getDeclaredMethod("makePrivate",
94                                                           atypes));
95           }
96       }
97       }
98     else
99       {
100         Type type = field == null ? decl.getType() : field.getType();
101     value.compileWithPosition(comp, StackTarget.getInstance(type));
102       }
103
104     // Optimization of Declaration.compileStore, to avoid swap.
105
if (field == null)
106       {
107     Variable var = decl.getVariable();
108     if (var == null)
109       var = decl.allocateVariable(code);
110     code.emitStore(var);
111       }
112     else if (field.getStaticFlag())
113       code.emitPutStatic(field);
114     else
115       code.emitPutField(field);
116   }
117
118   public static Method makeLocationMethod (Object JavaDoc name)
119   {
120     Type[] atypes = new Type[1];
121     if (name instanceof Symbol)
122       atypes[0] = Compilation.typeSymbol;
123     else
124       atypes[0] = Type.string_type;
125     return Compilation.typeLocation.getDeclaredMethod("make", atypes);
126   }
127 }
128
Popular Tags