1 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 ; 35 36 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 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 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 164 Expr assign(Expr value) 165 throws ESException 166 { 167 int valueType = value.getType(); 168 Expr typeExpr = value.getTypeExpr(); 169 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 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 224 { 225 printImpl(); 226 } 227 228 void printBooleanImpl() throws IOException 229 { 230 printImpl(); 231 } 232 233 void printInt32Impl() throws IOException 234 { 235 printImpl(); 236 } 237 238 void printStringImpl() throws IOException 239 { 240 printImpl(); 241 } 242 243 void printImpl() throws IOException 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 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 267 { 268 cl.setLine(getFilename(), getLine()); 269 270 cl.print(getId()); 271 } 272 273 public String toString() 274 { 275 return "[IdExpr " + getId() + "]"; 276 } 277 } 278 | Popular Tags |