KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > es > parser > IdExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.es.parser;
30
31 import com.caucho.es.ESException;
32 import com.caucho.es.ESId;
33
34 import java.io.IOException JavaDoc;
35
36 /**
37  * Expr is an intermediate form representing an expression.
38  */

39 class IdExpr extends Expr {
40   private static ESId ARGUMENTS = ESId.intern("arguments");
41   private Variable var;
42
43   IdExpr(Block block, Variable var)
44   {
45     super(block);
46     
47     this.var = var;
48     if (var.getId() == ARGUMENTS) {
49       function.setArguments();
50       function.setUseAllVariables();
51     }
52
53     if (var.getTypeExpr() != null) {
54       type = var.getType();
55       javaType = var.getTypeExpr().getJavaClass();
56     }
57
58     if (! var.isLocal() && ! function.isGlobalScope() && ! var.isJavaGlobal())
59       function.setNeedsScope();
60   }
61
62   void setType(int type)
63   {
64     var.setType(type);
65   }
66   
67   boolean isLocal()
68   {
69     return var.isLocal() && function.allowLocals();
70   }
71   
72   boolean isJavaLocal()
73   {
74     return var.isJavaLocal() && function.allowLocals();
75   }
76
77   /**
78    * Returns true if the variable is a global represented as a Java
79    * field.
80    */

81   boolean isJavaGlobal()
82   {
83     return var.isJavaGlobal();
84   }
85
86   void setUsed()
87   {
88     var.setUsed();
89   }
90
91   int getType()
92   {
93     if (! isLocal() && ! isJavaGlobal())
94       return TYPE_ES;
95     else
96       return var.getType();
97   }
98
99   Expr getTypeExpr()
100   {
101     if (! isLocal())
102       return null;
103     else
104       return var.getTypeExpr();
105   }
106
107   boolean isSimple()
108   {
109     return isJavaLocal();
110   }
111
112   boolean isGlobalScope()
113   {
114     return function.isGlobalScope() && ! var.isScope();
115   }
116
117   /**
118    * Returns the underlying variable.
119    */

120   Variable getVar()
121   {
122     return var;
123   }
124
125   boolean isUsed()
126   {
127     return var.isUsed() || function.useAllVariables();
128   }
129   
130   void setLocal()
131   {
132     var.setLocal();
133   }
134   
135   ESId getId()
136   {
137     return var.getId();
138   }
139   
140   Expr delete()
141   {
142     return new DeleteExpr(block, this);
143   }
144
145   Expr postfix(int op)
146   {
147     if (op == '+')
148       return new PostfixExpr(block, PostfixExpr.POSTINC, this);
149     else
150       return new PostfixExpr(block, PostfixExpr.POSTDEC, this);
151   }
152
153   Expr prefix(int op)
154   {
155     if (op == '+')
156       return new PostfixExpr(block, PostfixExpr.PREINC, this);
157     else
158       return new PostfixExpr(block, PostfixExpr.PREDEC, this);
159   }
160
161   /**
162    * Assigns the identifier to a value.
163    */

164   Expr assign(Expr value)
165     throws ESException
166   {
167     int valueType = value.getType();
168     Expr typeExpr = value.getTypeExpr();
169     // XXX: needs to differ from getType to make type inference work
170
int type = var.type;
171
172     if (isLocal() || isJavaGlobal()) {
173       if (valueType == TYPE_UNKNOWN)
174         valueType = TYPE_ES;
175
176       if (typeExpr != null)
177         var.setType(TYPE_JAVA, typeExpr);
178       else if (type == TYPE_UNKNOWN)
179         var.setType(valueType);
180       else if (type == valueType) {
181       }
182       else if ((type == TYPE_INTEGER || type == TYPE_NUMBER) &&
183                (valueType == TYPE_INTEGER || valueType == TYPE_NUMBER))
184         var.setType(TYPE_NUMBER);
185       else
186         var.setType(TYPE_ES, null);
187
188       type = var.getType();
189       Expr newTypeExpr = var.getTypeExpr();
190       if (newTypeExpr == null)
191         javaType = null;
192       else
193         javaType = newTypeExpr.getJavaClass();
194     }
195     
196     return new AssignExpr(block, this, value);
197   }
198
199   /**
200    * Creates a call expression from this id.
201    */

202   CallExpr startCall()
203     throws ESException
204   {
205     var.setUsed();
206     
207     return new CallExpr(block, this, null, false);
208   }
209
210   CallExpr startNew()
211     throws ESException
212   {
213     var.setUsed();
214
215     return new CallExpr(block, this, null, true);
216   }
217
218   void exprStatement(Function fun) throws ESException
219   {
220     doVoid().exprStatement(fun);
221   }
222
223   void printNumImpl() throws IOException JavaDoc
224   {
225     printImpl();
226   }
227
228   void printBooleanImpl() throws IOException JavaDoc
229   {
230     printImpl();
231   }
232
233   void printInt32Impl() throws IOException JavaDoc
234   {
235     printImpl();
236   }
237
238   void printStringImpl() throws IOException JavaDoc
239   {
240     printImpl();
241   }
242
243   void printImpl() throws IOException JavaDoc
244   {
245     cl.setLine(getFilename(), getLine());
246     
247     if (isJavaLocal()) {
248       cl.print(getId());
249     }
250     else if (isJavaGlobal()) {
251       cl.print(getId());
252     }
253     else if (isGlobalScope()) {
254       // Need to use getGlobalVariable so an undefined variable will
255
// throw an exception
256
cl.print("_env.getGlobalVariable(");
257       printLiteral(getId());
258       cl.print(")");
259     } else {
260       cl.print("_env.getScopeProperty(");
261       printLiteral(getId());
262       cl.print(")");
263     }
264   }
265
266   void printJavaImpl() throws IOException JavaDoc
267   {
268     cl.setLine(getFilename(), getLine());
269     
270     cl.print(getId());
271   }
272
273   public String JavaDoc toString()
274   {
275     return "[IdExpr " + getId() + "]";
276   }
277 }
278
Popular Tags