KickJava   Java API By Example, From Geeks To Geeks.

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


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.ESId;
32
33 /**
34  * Expr is an intermediate form representing an expression.
35  */

36 class Variable {
37   private Function function;
38   private ESId id;
39   private boolean isLocal;
40   private boolean isScope;
41   // Is the variable initialized before it's used?
42
private boolean isInitialized;
43   // Is the variable ever used?
44
private boolean isUsed;
45   // Is the variable used by a closure?
46
private boolean isClosureVar;
47   // True if the variable is a global represented as a java field.
48
private boolean isJavaGlobal;
49   int type;
50   
51   Expr fullType;
52   // True if the type is declared.
53
private boolean isDeclared;
54
55   Variable(Block block, ESId id, Expr type, boolean isLocal)
56   {
57     this.function = block.function;
58     this.id = id;
59     this.isLocal = isLocal;
60     this.isScope = block.getDepth() > 0 || ! function.isGlobalScope();
61     this.type = Expr.TYPE_UNKNOWN;
62     this.fullType = (TypeExpr) type;
63     
64     if (fullType != null) {
65       this.type = fullType.getType();
66       isDeclared = true;
67     }
68   }
69
70   /**
71    * Returns the variable's name.
72    */

73   ESId getId()
74   {
75     return id;
76   }
77
78   /**
79    * Sets the JavaScript type.
80    */

81   void setType(int type)
82   {
83     if (! isUsed)
84       this.isInitialized = true;
85
86     if (fullType != null) {
87       // XXX: check type
88
// throw new RuntimeException("mismatch type");
89
}
90     else
91       this.type = type;
92   }
93
94   /**
95    * Declare the type of this variable.
96    */

97   void declare(int javaScriptType, Expr typeExpr)
98   {
99     if (isDeclared && ! typeExpr.equals(this.fullType))
100       throw new IllegalStateException JavaDoc("can't declare " + this + " twice");
101     
102     setType(javaScriptType, typeExpr);
103
104     isDeclared = true;
105   }
106
107   /**
108    * Sets the type of this variable.
109    */

110   void setType(int newType, Expr expr)
111   {
112     if (! isUsed)
113       this.isInitialized = true;
114
115     if (isDeclared)
116       return;
117
118     if (newType != Expr.TYPE_UNKNOWN && newType != Expr.TYPE_JAVA) {
119       this.type = Expr.TYPE_ES;
120       fullType = null;
121       return;
122     }
123
124     if (fullType == null) {
125     }
126     else if (! (fullType instanceof JavaTypeExpr) ||
127              ! (expr instanceof JavaTypeExpr)) {
128       this.type = Expr.TYPE_ES;
129       fullType = null;
130       return;
131     }
132
133     JavaTypeExpr typeExpr = (JavaTypeExpr) expr;
134     
135
136     if (fullType != null &&
137         ! ((JavaTypeExpr) fullType).getJavaClass().equals(typeExpr.getJavaClass())) {
138       this.type = Expr.TYPE_ES;
139       fullType = null;
140       return;
141     }
142     
143     Class JavaDoc javaClass = typeExpr.getJavaClass();
144     if (javaClass.equals(byte.class) ||
145         javaClass.equals(short.class) ||
146         javaClass.equals(int.class)) {
147       this.type = Expr.TYPE_INTEGER;
148     }
149     else if (javaClass.equals(float.class) ||
150              javaClass.equals(double.class)) {
151       this.type = Expr.TYPE_NUMBER;
152     }
153     else if (javaClass.equals(boolean.class)) {
154       this.type = Expr.TYPE_BOOLEAN;
155     }
156     else if (javaClass.isPrimitive()) {
157       this.type = Expr.TYPE_ES;
158       this.fullType = null;
159       return;
160     }
161     else {
162       this.type = newType;
163       this.fullType = typeExpr;
164     }
165   }
166
167   boolean isLocal()
168   {
169     return isLocal;
170   }
171
172   void killLocal()
173   {
174     isLocal = false;
175   }
176
177   /**
178    * Returns true if this is a variable that can be used just like a
179    * java variable.
180    */

181   boolean isJavaLocal()
182   {
183     return isLocal && ! isClosureVar;
184   }
185
186   /**
187    * True if the variable is a global represented by a global field.
188    */

189   boolean isJavaGlobal()
190   {
191     return isJavaGlobal;
192   }
193
194   /**
195    * Set the variable as a global.
196    */

197   void setJavaGlobal(boolean isGlobal)
198   {
199     isJavaGlobal = isGlobal;
200     isLocal = false;
201     if (type == Expr.TYPE_UNKNOWN)
202       type = Expr.TYPE_ES;
203   }
204
205   /**
206    * Returns the JavaScript type of the variable. If the type hasn't
207    * been inferred yet, the type must be ESBase.
208    */

209   int getType()
210   {
211     if (type == Expr.TYPE_UNKNOWN) {
212       type = Expr.TYPE_ES;
213       isInitialized = false;
214     }
215     
216     return type;
217   }
218
219   Expr getTypeExpr()
220   {
221     return fullType;
222   }
223
224   boolean hasInit()
225   {
226     return type != Expr.TYPE_UNKNOWN && isLocal && isInitialized;
227   }
228
229   boolean isScope()
230   {
231     return isScope;
232   }
233
234   void setUsed()
235   {
236     isUsed = true;
237     if (type == Expr.TYPE_UNKNOWN)
238       type = Expr.TYPE_ES;
239     isInitialized = false;
240   }
241
242   void setUsedByClosure()
243   {
244     isClosureVar = true;
245     setUsed();
246   }
247
248   boolean isUsed()
249   {
250     return isUsed;
251   }
252   
253   void setLocal()
254   {
255     isLocal = true;
256   }
257
258   public String JavaDoc toString()
259   {
260     return "[Variable " + id + " " + fullType + "]";
261   }
262 }
263
Popular Tags