1 28 29 package com.caucho.es.parser; 30 31 import com.caucho.es.ESId; 32 33 36 class Variable { 37 private Function function; 38 private ESId id; 39 private boolean isLocal; 40 private boolean isScope; 41 private boolean isInitialized; 43 private boolean isUsed; 45 private boolean isClosureVar; 47 private boolean isJavaGlobal; 49 int type; 50 51 Expr fullType; 52 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 73 ESId getId() 74 { 75 return id; 76 } 77 78 81 void setType(int type) 82 { 83 if (! isUsed) 84 this.isInitialized = true; 85 86 if (fullType != null) { 87 } 90 else 91 this.type = type; 92 } 93 94 97 void declare(int javaScriptType, Expr typeExpr) 98 { 99 if (isDeclared && ! typeExpr.equals(this.fullType)) 100 throw new IllegalStateException ("can't declare " + this + " twice"); 101 102 setType(javaScriptType, typeExpr); 103 104 isDeclared = true; 105 } 106 107 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 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 181 boolean isJavaLocal() 182 { 183 return isLocal && ! isClosureVar; 184 } 185 186 189 boolean isJavaGlobal() 190 { 191 return isJavaGlobal; 192 } 193 194 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 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 toString() 259 { 260 return "[Variable " + id + " " + fullType + "]"; 261 } 262 } 263 | Popular Tags |