1 29 30 package com.caucho.quercus.expr; 31 32 import com.caucho.quercus.Location; 33 import com.caucho.quercus.env.Value; 34 import com.caucho.quercus.parser.QuercusParser; 35 import com.caucho.quercus.program.*; 36 import com.caucho.util.L10N; 37 import com.caucho.vfs.Path; 38 39 import java.util.ArrayList ; 40 import java.util.logging.Level ; 41 import java.util.logging.Logger ; 42 43 46 public class ExprFactory { 47 private static final L10N L = new L10N(ExprFactory.class); 48 private static final Logger log 49 = Logger.getLogger(ExprFactory.class.getName()); 50 51 public ExprFactory() 52 { 53 } 54 55 public static ExprFactory create() 56 { 57 try { 58 Class cl = Class.forName("com.caucho.quercus.expr.ProExprFactory"); 59 60 return (ExprFactory) cl.newInstance(); 61 } catch (Exception e) { 62 log.log(Level.FINEST, e.toString(), e); 63 64 return new ExprFactory(); 65 } 66 } 67 68 71 public Expr createNull() 72 { 73 return NullLiteralExpr.NULL; 74 } 75 76 79 public Expr createString(String lexeme) 80 { 81 return new StringLiteralExpr(lexeme); 82 } 83 84 87 public Expr createBinary(byte []bytes) 88 { 89 return new BinaryLiteralExpr(bytes); 90 } 91 92 95 public Expr createLong(long value) 96 { 97 return new LongLiteralExpr(value); 98 } 99 100 103 public Expr createLiteral(Value literal) 104 { 105 return new LiteralExpr(literal); 106 } 107 108 111 public VarExpr createVar(VarInfo var) 112 { 113 return new VarExpr(var); 114 } 115 116 119 public VarVarExpr createVarVar(Expr var) 120 { 121 return new VarVarExpr(var); 122 } 123 124 127 public ConstExpr createConst(String name) 128 { 129 return new ConstExpr(name); 130 } 131 132 135 public ClassConstExpr createClassConst(String className, String name) 136 { 137 return new ClassConstExpr(className, name); 138 } 139 140 143 public ThisExpr createThis(InterpretedClassDef cl) 144 { 145 return new ThisExpr(cl); 146 } 147 148 151 public ArrayGetExpr createArrayGet(Expr base, Expr index) 152 { 153 return new ArrayGetExpr(base, index); 154 } 155 156 159 public ArrayTailExpr createArrayTail(Expr base) 160 { 161 return new ArrayTailExpr(base); 162 } 163 164 167 public Expr createFieldGet(Expr base, String name) 168 { 169 return new FieldGetExpr(base, name); 170 } 171 172 175 public Expr createFieldVarGet(Expr base, Expr name) 176 { 177 return new FieldVarGetExpr(base, name); 178 } 179 180 183 public Expr createStaticFieldGet(String className, String name) 184 { 185 return new StaticFieldGetExpr(className, name); 186 } 187 188 191 public Expr createUnsetVar(AbstractVarExpr var) 192 { 193 return new UnsetVarExpr(var); 194 } 195 196 199 public CharAtExpr createCharAt(Expr base, Expr index) 200 { 201 return new CharAtExpr(base, index); 202 } 203 204 207 public PostIncrementExpr createPostIncrement(Expr expr, int incr) 208 { 209 return new PostIncrementExpr(expr, incr); 210 } 211 212 215 public PreIncrementExpr createPreIncrement(Expr expr, int incr) 216 { 217 return new PreIncrementExpr(expr, incr); 218 } 219 220 223 public Expr createMinus(Expr expr) 224 { 225 return new MinusExpr(expr); 226 } 227 228 231 public Expr createPlus(Expr expr) 232 { 233 return new PlusExpr(expr); 234 } 235 236 239 public Expr createNot(Expr expr) 240 { 241 return new NotExpr(expr); 242 } 243 244 247 public Expr createBitNot(Expr expr) 248 { 249 return new BitNotExpr(expr); 250 } 251 252 255 public Expr createClone(Expr expr) 256 { 257 return new CloneExpr(expr); 258 } 259 260 263 public Expr createCopy(Expr expr) 264 { 265 return new CopyExpr(expr); 266 } 267 268 271 public Expr createSuppress(Expr expr) 272 { 273 return new SuppressErrorExpr(expr); 274 } 275 276 279 public Expr createToBoolean(Expr expr) 280 { 281 return new ToBooleanExpr(expr); 282 } 283 284 287 public Expr createToLong(Expr expr) 288 { 289 return new ToLongExpr(expr); 290 } 291 292 295 public Expr createToDouble(Expr expr) 296 { 297 return new ToDoubleExpr(expr); 298 } 299 300 303 public Expr createToString(Expr expr) 304 { 305 return new ToStringExpr(expr); 306 } 307 308 311 public Expr createToUnicode(Expr expr) 312 { 313 return new ToUnicodeExpr(expr); 314 } 315 316 319 public Expr createToBinary(Expr expr) 320 { 321 return new ToBinaryExpr(expr); 322 } 323 324 327 public Expr createToObject(Expr expr) 328 { 329 return new ToObjectExpr(expr); 330 } 331 332 335 public Expr createToArray(Expr expr) 336 { 337 return new ToArrayExpr(expr); 338 } 339 340 343 public Expr createDie(Expr expr) 344 { 345 return new DieExpr(expr); 346 } 347 348 351 public Expr createExit(Expr expr) 352 { 353 return new ExitExpr(expr); 354 } 355 356 359 public Expr createRequired() 360 { 361 return new RequiredExpr(); 362 } 363 364 367 public Expr createDefault() 368 { 369 return new DefaultExpr(); 370 } 371 372 375 public Expr createAdd(Expr left, Expr right) 376 { 377 return new AddExpr(left, right); 378 } 379 380 383 public Expr createSub(Expr left, Expr right) 384 { 385 return new SubExpr(left, right); 386 } 387 388 391 public Expr createMul(Expr left, Expr right) 392 { 393 return new MulExpr(left, right); 394 } 395 396 399 public Expr createDiv(Expr left, Expr right) 400 { 401 return new DivExpr(left, right); 402 } 403 404 407 public Expr createMod(Expr left, Expr right) 408 { 409 return new ModExpr(left, right); 410 } 411 412 415 public Expr createLeftShift(Expr left, Expr right) 416 { 417 return new LeftShiftExpr(left, right); 418 } 419 420 423 public Expr createRightShift(Expr left, Expr right) 424 { 425 return new RightShiftExpr(left, right); 426 } 427 428 431 public Expr createBitAnd(Expr left, Expr right) 432 { 433 return new BitAndExpr(left, right); 434 } 435 436 439 public Expr createBitOr(Expr left, Expr right) 440 { 441 return new BitOrExpr(left, right); 442 } 443 444 447 public Expr createBitXor(Expr left, Expr right) 448 { 449 return new BitXorExpr(left, right); 450 } 451 452 455 public final Expr createAppend(Expr left, Expr right) 456 { 457 AppendExpr leftAppend; 458 459 471 472 if (left instanceof AppendExpr) 473 leftAppend = (AppendExpr) left; 474 else 475 leftAppend = createAppendImpl(left, null); 476 477 AppendExpr next; 478 479 490 491 if (right instanceof AppendExpr) 492 next = (AppendExpr) right; 493 else 494 next = createAppendImpl(right, null); 495 496 AppendExpr result = append(leftAppend, next); 497 498 if (result.getNext() != null) 499 return result; 500 else 501 return result.getValue(); 502 } 503 504 508 private AppendExpr append(AppendExpr left, AppendExpr tail) 509 { 510 if (left == null) 511 return tail; 512 513 tail = append(left.getNext(), tail); 514 515 if (true 516 && left.getValue() instanceof StringLiteralExpr 517 && tail.getValue() instanceof StringLiteralExpr) { 518 StringLiteralExpr leftString = (StringLiteralExpr) left.getValue(); 519 StringLiteralExpr rightString = (StringLiteralExpr) tail.getValue(); 520 521 Expr value = createString(leftString.evalConstant().toString() 522 + rightString.evalConstant().toString()); 523 524 return createAppendImpl(value, tail.getNext()); 525 } 526 else { 527 left.setNext(tail); 528 529 return left; 530 } 531 } 532 533 protected AppendExpr createAppendImpl(Expr left, AppendExpr right) 534 { 535 return new AppendExpr(left, right); 536 } 537 538 541 public Expr createLt(Expr left, Expr right) 542 { 543 return new LtExpr(left, right); 544 } 545 546 549 public Expr createLeq(Expr left, Expr right) 550 { 551 return new LeqExpr(left, right); 552 } 553 554 557 public Expr createGt(Expr left, Expr right) 558 { 559 return new GtExpr(left, right); 560 } 561 562 565 public Expr createGeq(Expr left, Expr right) 566 { 567 return new GeqExpr(left, right); 568 } 569 570 573 public Expr createEq(Expr left, Expr right) 574 { 575 return new EqExpr(left, right); 576 } 577 578 581 public Expr createNeq(Expr left, Expr right) 582 { 583 return new NeqExpr(left, right); 584 } 585 586 589 public Expr createEquals(Expr left, Expr right) 590 { 591 return new EqualsExpr(left, right); 592 } 593 594 597 public Expr createAssign(AbstractVarExpr left, Expr right) 598 { 599 return new AssignExpr(left, right); 600 } 601 602 605 public Expr createAssignRef(AbstractVarExpr left, Expr right) 606 { 607 return new AssignRefExpr(left, right); 608 } 609 610 613 public RefExpr createRef(Expr base) 614 { 615 return new RefExpr(base); 616 } 617 618 621 public Expr createAnd(Expr left, Expr right) 622 { 623 return new AndExpr(left, right); 624 } 625 626 629 public Expr createOr(Expr left, Expr right) 630 { 631 return new OrExpr(left, right); 632 } 633 634 637 public Expr createXor(Expr left, Expr right) 638 { 639 return new XorExpr(left, right); 640 } 641 642 645 public Expr createComma(Expr left, Expr right) 646 { 647 return new CommaExpr(left, right); 648 } 649 650 653 public Expr createInstanceOf(Expr expr, String name) 654 { 655 return new InstanceOfExpr(expr, name); 656 } 657 658 661 public Expr createInstanceOfVar(Expr expr, Expr name) 662 { 663 return new InstanceOfVarExpr(expr, name); 664 } 665 666 669 public Expr createEach(Expr expr) 670 { 671 return new EachExpr(expr); 672 } 673 674 677 public final Expr createList(QuercusParser parser, 678 ListHeadExpr head, Expr value) 679 { 680 boolean isSuppress = value instanceof SuppressErrorExpr; 681 682 if (isSuppress) { 683 SuppressErrorExpr suppressExpr = (SuppressErrorExpr) value; 684 685 value = suppressExpr.getExpr(); 686 } 687 688 Expr expr; 689 690 if (value instanceof EachExpr) { 691 expr = createListEach(head.getVarList(), (EachExpr) value); 692 } 693 else 694 expr = createList(head, value); 695 696 if (isSuppress) 697 return createSuppress(expr); 698 else 699 return expr; 700 } 701 702 705 public ListHeadExpr createListHead(ArrayList <Expr> keys) 706 { 707 return new ListHeadExpr(keys); 708 } 709 710 713 public Expr createList(ListHeadExpr head, Expr value) 714 { 715 return new ListExpr(head, value); 716 } 717 718 721 public Expr createListEach(Expr []varList, EachExpr value) 722 { 723 return new ListEachExpr(varList, value); 724 } 725 726 729 public Expr createConditional(Expr test, Expr left, Expr right) 730 { 731 return new ConditionalExpr(test, left, right); 732 } 733 734 737 public Expr createArrayFun(ArrayList <Expr> keys, ArrayList <Expr> values) 738 { 739 return new ArrayFunExpr(keys, values); 740 } 741 742 745 public FunctionExpr createFunction(Location loc, 746 String name, 747 ArrayList <Expr> args) 748 { 749 return new FunctionExpr(loc, name, args); 750 } 751 752 755 public VarFunctionExpr createVarFunction(Location loc, 756 Expr name, 757 ArrayList <Expr> args) 758 { 759 return new VarFunctionExpr(loc, name, args); 760 } 761 762 765 public Expr createClassMethod(Location loc, 766 String className, 767 String name, 768 ArrayList <Expr> args) 769 { 770 return new ClassMethodExpr(loc, className, name, args); 771 } 772 773 776 public Expr createStaticMethod(Location loc, 777 String className, 778 String name, 779 ArrayList <Expr> args) 780 { 781 return new StaticMethodExpr(loc, className, name, args); 782 } 783 784 787 public Expr createMethodCall(Location loc, 788 Expr objExpr, 789 String name, 790 ArrayList <Expr> args) 791 { 792 return new MethodCallExpr(loc, objExpr, name, args); 793 } 794 795 798 public Expr createVarMethodCall(Location loc, 799 Expr objExpr, 800 Expr name, 801 ArrayList <Expr> args) 802 { 803 return new VarMethodCallExpr(loc, objExpr, name, args); 804 } 805 806 809 public NewExpr createNew(Location loc, 810 String name, 811 ArrayList <Expr> args) 812 { 813 return new NewExpr(loc, name, args); 814 } 815 816 819 public VarNewExpr createVarNew(Location loc, 820 Expr name, 821 ArrayList <Expr> args) 822 { 823 return new VarNewExpr(loc, name, args); 824 } 825 826 829 public Expr createInclude(Location loc, 830 Path source, 831 Expr expr) 832 { 833 return new IncludeExpr(loc, source, expr, false); 834 } 835 836 839 public Expr createRequire(Location loc, 840 Path source, 841 Expr expr) 842 { 843 return new IncludeExpr(loc, source, expr, true); 844 } 845 846 849 public Expr createIncludeOnce(Location loc, 850 Path source, 851 Expr expr) 852 { 853 return new IncludeOnceExpr(loc, source, expr, false); 854 } 855 856 859 public Expr createRequireOnce(Location loc, 860 Path source, 861 Expr expr) 862 { 863 return new IncludeOnceExpr(loc, source, expr, true); 864 } 865 866 869 public Statement createNullStatement() 870 { 871 return NullStatement.NULL; 872 } 873 874 877 public Statement createEcho(Location loc, Expr expr) 878 { 879 return new EchoStatement(loc, expr); 880 } 881 882 885 public Statement createExpr(Location loc, Expr expr) 886 { 887 return new ExprStatement(loc, expr); 888 } 889 890 public final Statement createBlock(Location loc, 891 ArrayList <Statement> statementList) 892 { 893 if (statementList.size() == 1) 894 return statementList.get(0); 895 896 Statement []statements = new Statement[statementList.size()]; 897 898 statementList.toArray(statements); 899 900 return createBlockImpl(loc, statements); 901 } 902 903 public final Statement createBlock(Location loc, Statement []statementList) 904 { 905 if (statementList.length == 1) 906 return statementList[0]; 907 908 Statement []statements = new Statement[statementList.length]; 909 910 System.arraycopy(statementList, 0, statements, 0, statementList.length); 911 912 return createBlockImpl(loc, statements); 913 } 914 915 918 public final BlockStatement createBlockImpl(Location loc, 919 ArrayList <Statement> statementList) 920 { 921 Statement []statements = new Statement[statementList.size()]; 922 923 statementList.toArray(statements); 924 925 return createBlockImpl(loc, statements); 926 } 927 928 931 public BlockStatement createBlockImpl(Location loc, Statement []statements) 932 { 933 return new BlockStatement(loc, statements); 934 } 935 936 939 public Statement createText(Location loc, String text) 940 { 941 return new TextStatement(loc, text); 942 } 943 944 947 public Statement createIf(Location loc, 948 Expr test, 949 Statement trueBlock, 950 Statement falseBlock) 951 { 952 return new IfStatement(loc, test, trueBlock, falseBlock); 953 } 954 955 958 public Statement createSwitch(Location loc, 959 Expr value, 960 ArrayList <Expr[]> caseList, 961 ArrayList <BlockStatement> blockList, 962 Statement defaultBlock) 963 { 964 return new SwitchStatement(loc, value, caseList, blockList, defaultBlock); 965 } 966 967 970 public Statement createFor(Location loc, 971 Expr init, 972 Expr test, 973 Expr incr, 974 Statement block) 975 { 976 return new ForStatement(loc, init, test, incr, block); 977 } 978 979 982 public Statement createForeach(Location loc, 983 Expr objExpr, 984 AbstractVarExpr key, 985 AbstractVarExpr value, 986 boolean isRef, 987 Statement block) 988 { 989 return new ForeachStatement(loc, objExpr, key, value, isRef, block); 990 } 991 992 995 public Statement createWhile(Location loc, 996 Expr test, 997 Statement block) 998 { 999 return new WhileStatement(loc, test, block); 1000 } 1001 1002 1005 public Statement createDo(Location loc, 1006 Expr test, 1007 Statement block) 1008 { 1009 return new DoStatement(loc, test, block); 1010 } 1011 1012 1015 public BreakStatement createBreak() 1016 { 1017 return BreakStatement.BREAK; 1018 } 1019 1020 1023 public ContinueStatement createContinue() 1024 { 1025 return ContinueStatement.CONTINUE; 1026 } 1027 1028 1031 public Statement createGlobal(Location loc, 1032 VarExpr var) 1033 { 1034 return new GlobalStatement(loc, var); 1035 } 1036 1037 1040 public Statement createStatic(Location loc, 1041 VarExpr var, 1042 Expr value) 1043 { 1044 return new StaticStatement(loc, var, value); 1045 } 1046 1047 1050 public Statement createThrow(Location loc, 1051 Expr value) 1052 { 1053 return new ThrowStatement(loc, value); 1054 } 1055 1056 1059 public TryStatement createTry(Location loc, 1060 Statement block) 1061 { 1062 return new TryStatement(loc, block); 1063 } 1064 1065 1068 public Statement createReturn(Location loc, 1069 Expr value) 1070 { 1071 return new ReturnStatement(loc, value); 1072 } 1073 1074 1077 public Statement createReturnRef(Location loc, 1078 Expr value) 1079 { 1080 return new ReturnRefStatement(loc, value); 1081 } 1082 1083 1086 public Statement createFunctionDef(Location loc, 1087 Function fun) 1088 { 1089 return new FunctionDefStatement(loc, fun); 1090 } 1091 1092 1095 public Statement createClassDef(Location loc, 1096 InterpretedClassDef cl) 1097 { 1098 return new ClassDefStatement(loc, cl); 1099 } 1100 1101 1104 public Function createFunction(Location loc, 1105 String name, 1106 FunctionInfo info, 1107 ArrayList <Arg> argList, 1108 ArrayList <Statement> statementList) 1109 { 1110 return new Function(this, loc, name, info, argList, statementList); 1111 } 1112 1113 1116 public Function createObjectMethod(Location loc, 1117 InterpretedClassDef cl, 1118 String name, 1119 FunctionInfo info, 1120 ArrayList <Arg> argList, 1121 ArrayList <Statement> statementList) 1122 { 1123 return new ObjectMethod(this, loc, cl, name, info, argList, statementList); 1124 } 1125 1126 1129 public Function createMethodDeclaration(Location loc, 1130 InterpretedClassDef cl, 1131 String name, 1132 FunctionInfo info, 1133 ArrayList <Arg> argList) 1134 { 1135 return new MethodDeclaration(this, loc, cl, name, info, argList); 1136 } 1137 1138 public InterpretedClassDef createClassDef(String name, 1139 String parentName, 1140 String []ifaceList) 1141 { 1142 return new InterpretedClassDef(name, parentName, ifaceList); 1143 } 1144} 1145 1146 | Popular Tags |