1 package org.jacorb.idl; 2 3 22 23 import java.io.ByteArrayOutputStream ; 24 import java.io.File ; 25 import java.io.PrintWriter ; 26 import java.util.*; 27 28 public class UnionType 29 extends TypeDeclaration 30 implements Scope 31 { 32 33 TypeSpec switch_type_spec; 34 35 SwitchBody switch_body; 36 private boolean written = false; 37 38 private ScopeData scopeData; 39 40 private boolean allCasesCovered = false; 41 private boolean switch_is_enum = false; 42 private boolean switch_is_bool = false; 43 private boolean switch_is_longlong = false; 44 private boolean explicit_default_case = false; 45 private int labels; 46 47 public UnionType(int num) 48 { 49 super(num); 50 pack_name = ""; 51 } 52 53 public Object clone() 54 { 55 UnionType ut = new UnionType(new_num()); 56 ut.switch_type_spec = this.switch_type_spec; 57 ut.switch_body = switch_body; 58 ut.pack_name = this.pack_name; 59 ut.name = this.name; 60 ut.written = this.written; 61 ut.scopeData = this.scopeData; 62 ut.enclosing_symbol = this.enclosing_symbol; 63 ut.token = this.token; 64 return ut; 65 } 66 67 public void setScopeData(ScopeData data) 68 { 69 scopeData = data; 70 } 71 72 public ScopeData getScopeData() 73 { 74 return scopeData; 75 } 76 77 78 public TypeDeclaration declaration() 79 { 80 return this; 81 } 82 83 84 public void setEnclosingSymbol(IdlSymbol s) 85 { 86 if (enclosing_symbol != null && enclosing_symbol != s) 87 throw new RuntimeException ("Compiler Error: trying to reassign container for " + name); 88 enclosing_symbol = s; 89 if (switch_body != null) 90 { 91 switch_body.setEnclosingSymbol(s); 92 } 93 } 94 95 public String typeName() 96 { 97 if (typeName == null) 98 setPrintPhaseNames(); 99 return typeName; 100 } 101 102 public String className() 103 { 104 String fullName = typeName(); 105 if (fullName.indexOf('.') > 0) 106 { 107 return fullName.substring(fullName.lastIndexOf('.') + 1); 108 } 109 else 110 { 111 return fullName; 112 } 113 } 114 115 public String printReadExpression(String Streamname) 116 { 117 return typeName() + "Helper.read(" + Streamname + ")"; 118 } 119 120 public String printWriteStatement(String var_name, String streamname) 121 { 122 return typeName() + "Helper.write(" + streamname + "," + var_name + ");"; 123 } 124 125 public String holderName() 126 { 127 return typeName() + "Holder"; 128 } 129 130 public void set_included(boolean i) 131 { 132 included = i; 133 } 134 135 public void setSwitchType(TypeSpec s) 136 { 137 switch_type_spec = s; 138 } 139 140 public void setSwitchBody(SwitchBody sb) 141 { 142 switch_body = sb; 143 } 144 145 public void setPackage(String s) 146 { 147 s = parser.pack_replace(s); 148 if (pack_name.length() > 0) 149 { 150 pack_name = new String (s + "." + pack_name); 151 } 152 else 153 { 154 pack_name = s; 155 } 156 if (switch_type_spec != null) 157 { 158 switch_type_spec.setPackage (s); 159 } 160 if (switch_body != null) 161 { 162 switch_body.setPackage(s); 163 } 164 } 165 166 public boolean basic() 167 { 168 return false; 169 } 170 171 public void parse() 172 { 173 boolean justAnotherOne = false; 174 175 escapeName(); 176 177 ConstrTypeSpec ctspec = new ConstrTypeSpec(new_num()); 178 try 179 { 180 ScopedName.definePseudoScope(full_name()); 181 ctspec.c_type_spec = this; 182 NameTable.define(full_name(), "type-union"); 183 TypeMap.typedef(full_name(), ctspec); 184 } 185 catch (NameAlreadyDefined nad) 186 { 187 if (parser.get_pending (full_name ()) != null) 188 { 189 if (switch_type_spec == null) 190 { 191 justAnotherOne = true; 192 } 193 195 if (!full_name().equals("org.omg.CORBA.TypeCode") && switch_type_spec != null) 196 { 197 TypeMap.replaceForwardDeclaration(full_name(), ctspec); 198 } 199 } 200 else 201 { 202 parser.error("Union " + full_name() + " already defined", token); 203 } 204 } 205 206 if (switch_type_spec != null) 207 { 208 TypeSpec ts; 210 if (switch_type_spec.type_spec instanceof ScopedName) 211 { 212 ts = ((ScopedName)switch_type_spec.type_spec).resolvedTypeSpec(); 213 214 while(ts instanceof ScopedName || ts instanceof AliasTypeSpec) 215 { 216 if (ts instanceof ScopedName) 217 { 218 ts = ((ScopedName)ts).resolvedTypeSpec(); 219 } 220 else 221 { 222 ts = ((AliasTypeSpec)ts).originalType(); 223 } 224 } 225 addImportedName(switch_type_spec.typeName()); 226 } 227 else 228 { 229 ts = switch_type_spec.type_spec; 230 } 231 232 234 if 235 (!( 236 ((ts instanceof SwitchTypeSpec) && 237 (((SwitchTypeSpec)ts).isSwitchable())) 238 || 239 ((ts instanceof BaseType) && 240 (((BaseType)ts).isSwitchType())) 241 || 242 ((ts instanceof ConstrTypeSpec) && 243 (((ConstrTypeSpec)ts).c_type_spec instanceof EnumType)) 244 )) 245 { 246 parser.error("Illegal Switch Type: " + ts.typeName(), token); 247 } 248 249 switch_type_spec.parse(); 250 switch_body.setTypeSpec(switch_type_spec); 251 switch_body.setUnion(this); 252 253 ScopedName.addRecursionScope(typeName()); 254 switch_body.parse(); 255 ScopedName.removeRecursionScope(typeName()); 256 257 259 for (Enumeration e = switch_body.caseListVector.elements(); e.hasMoreElements();) 260 { 261 Case c = (Case)e.nextElement(); 262 c.element_spec.t = getElementType(c.element_spec); 263 } 264 265 NameTable.parsed_interfaces.put(full_name(), ""); 266 parser.remove_pending(full_name()); 267 } 268 else if (!justAnotherOne) 269 { 270 parser.set_pending(full_name()); 273 } 274 } 275 276 277 281 282 public String getTypeCodeExpression() 283 { 284 return typeName() + "Helper.type()"; 285 } 286 287 public String getTypeCodeExpression(Set knownTypes) 288 { 289 if (knownTypes.contains(this)) 290 { 291 return this.getRecursiveTypeCodeExpression(); 292 } 293 else 294 { 295 return this.getTypeCodeExpression(); 296 } 297 } 298 299 private void printClassComment(String className, PrintWriter ps) 300 { 301 ps.println("/**"); 302 ps.println(" *\tGenerated from IDL definition of union " + 303 "\"" + className + "\""); 304 ps.println(" *\t@author JacORB IDL compiler "); 305 ps.println(" */\n"); 306 } 307 308 private void printUnionClass(String className, PrintWriter pw) 309 { 310 Enumeration e; 311 if (Environment.JAVA14 && pack_name.equals("")) 312 lexer.emit_warn 313 ("No package defined for " + className + " - illegal in JDK1.4", token); 314 if (!pack_name.equals("")) 315 pw.println("package " + pack_name + ";"); 316 317 printImport(pw); 318 319 printClassComment(className, pw); 320 321 pw.println("public" + parser.getFinalString() + " class " + className); 322 pw.println("\timplements org.omg.CORBA.portable.IDLEntity"); 323 pw.println("{"); 324 325 TypeSpec ts = switch_type_spec.typeSpec(); 326 327 while(ts instanceof ScopedName || ts instanceof AliasTypeSpec) 328 { 329 if (ts instanceof ScopedName) 330 ts = ((ScopedName)ts).resolvedTypeSpec(); 331 if (ts instanceof AliasTypeSpec) 332 ts = ((AliasTypeSpec)ts).originalType(); 333 } 334 335 pw.println("\tprivate " + ts.typeName() + " discriminator;"); 336 337 338 339 String defaultStr = ""; 340 341 344 345 int def = 0; 346 java.util.Vector allCaseLabels = new java.util.Vector (); 347 java.util.Vector unusedCaseLabels = new java.util.Vector (); 348 349 e = switch_body.caseListVector.elements(); 350 while (e.hasMoreElements()) 351 { 352 Case c = (Case)e.nextElement(); 353 for (int i = 0; i < c.case_label_list.v.size(); i++) 354 { 355 labels++; Object ce = c.case_label_list.v.elementAt(i); 357 if (ce != null) 358 { 359 if (ce instanceof ConstExpr) 360 { 361 allCaseLabels.addElement(((ConstExpr)ce).value()); 362 } 363 else 364 { 365 allCaseLabels.addElement 367 (ScopedName.unPseudoName (((ScopedName)ce).resolvedName())); 368 } 369 } 370 else 371 { 372 def = 1; 373 explicit_default_case = true; 374 } 375 } 376 } 377 378 379 380 if ((ts instanceof ConstrTypeSpec && 381 ((ConstrTypeSpec)ts).declaration() instanceof EnumType)) 382 { 383 this.switch_is_enum = true; 384 EnumType et = (EnumType)((ConstrTypeSpec)ts).declaration(); 385 386 if (allCaseLabels.size() + def > et.size()) 387 { 388 lexer.emit_warn("Too many case labels in definition of union " + 389 full_name() + ", default cannot apply", token); 390 } 391 if (allCaseLabels.size() + def == et.size()) 392 { 393 allCasesCovered = true; 394 } 395 396 for (int i = 0; i < et.size(); i++) 397 { 398 String qualifiedCaseLabel = 399 ts.typeName() + "." + (String )et.enumlist.v.elementAt(i); 400 if (!(allCaseLabels.contains(qualifiedCaseLabel))) 401 { 402 404 if (defaultStr.length () == 0) 405 { 406 defaultStr = qualifiedCaseLabel; 407 } 408 unusedCaseLabels.addElement (qualifiedCaseLabel); 409 } 410 } 411 } 412 else 413 { 414 if (ts instanceof BaseType) 415 { 416 ts = ((BaseType)ts).typeSpec(); 417 } 418 419 if (ts instanceof BooleanType) 420 { 421 this.switch_is_bool = true; 422 423 425 if (allCaseLabels.size() + def > 2) 426 { 427 parser.error("Case label error: too many default labels.", token); 428 return; 429 } 430 else if (allCaseLabels.size() == 1) 431 { 432 if (((String )allCaseLabels.elementAt(0)).equals("true")) 433 defaultStr = "false"; 434 else 435 defaultStr = "true"; 436 } 437 else 438 { 439 } 441 } 442 else if (ts instanceof CharType) 443 { 444 446 Enumeration enumeration; 447 String charStr; 448 boolean matched = false; 449 short val; 450 451 453 for (short s = 0; s < 256; s++) 454 { 455 matched = false; 456 enumeration = allCaseLabels.elements (); 457 while (enumeration.hasMoreElements ()) 458 { 459 charStr = (String ) enumeration.nextElement (); 460 461 463 charStr = charStr.substring (1, charStr.length () - 1); 464 465 467 if (charStr.charAt (0) == '\\') 468 { 469 charStr = charStr.substring (1); 470 val = Short.parseShort (charStr); 471 } 472 else 473 { 474 val = (short) charStr.charAt (0); 475 } 476 477 if (s == val) 478 { 479 matched = true; 480 break; 481 } 482 else 483 { 484 continue; 485 } 486 } 487 488 490 if (! matched) 491 { 492 defaultStr = "(char)" + s; 493 break; 494 } 495 } 496 } 497 else if (ts instanceof IntType) 498 { 499 int maxint = 65536; if (ts instanceof LongType) 501 maxint = 2147483647; for (int i = 0; i < maxint; i++) 503 { 504 if (!(allCaseLabels.contains(String.valueOf(i)))) 505 { 506 defaultStr = Integer.toString(i); 507 break; 508 } 509 } 510 if (ts instanceof LongLongType) 511 { 512 this.switch_is_longlong = true; 513 } 514 } 515 else 516 { 517 logger.error("Something went wrong in UnionType, " 518 + "could not identify switch type " 519 + switch_type_spec.type_spec); 520 } 521 522 } 523 524 525 526 527 e = switch_body.caseListVector.elements(); 528 while (e.hasMoreElements()) 529 { 530 Case c = (Case)e.nextElement(); 531 int caseLabelNum = c.case_label_list.v.size(); 532 533 String label[] = new String [ caseLabelNum ]; 534 for (int i = 0; i < caseLabelNum; i++) 535 { 536 Object o = c.case_label_list.v.elementAt(i); 537 if (o == null) { 539 label[ i ] = null; 540 } 541 else if (o != null && o instanceof ConstExpr) 542 { 543 label[ i ] = ((ConstExpr)o).value(); 544 } 545 else if (o instanceof ScopedName) 546 { 547 label[ i ] = ((ScopedName)o).typeName(); 548 } 549 } 550 551 pw.println("\tprivate " + c.element_spec.t.typeName() 552 + " " + c.element_spec.d.name() + ";"); 553 } 554 555 558 559 pw.println("\n\tpublic " + className + " ()"); 560 pw.println("\t{"); 561 pw.println("\t}\n"); 562 563 566 567 pw.println("\tpublic " + ts.typeName() + " discriminator ()"); 568 pw.println("\t{"); 569 pw.println("\t\treturn discriminator;"); 570 pw.println("\t}\n"); 571 572 575 576 e = switch_body.caseListVector.elements(); 577 while (e.hasMoreElements ()) 578 { 579 Case c = (Case) e.nextElement(); 580 boolean thisCaseIsDefault = false; 581 582 int caseLabelNum = c.case_label_list.v.size(); 583 584 String label[] = new String [ caseLabelNum ]; 585 586 587 588 for (int i = 0; i < caseLabelNum; i++) 589 { 590 Object o = c.case_label_list.v.elementAt(i); 591 if (o == null) { 593 label[ i ] = null; 594 thisCaseIsDefault = true; 595 } 596 else if (o instanceof ConstExpr) 597 label[ i ] = ((ConstExpr)o).value(); 598 else if (o instanceof ScopedName) 599 label[ i ] = ((ScopedName)o).typeName(); 600 } 601 602 604 pw.println("\tpublic " + c.element_spec.t.typeName() 605 + " " + c.element_spec.d.name() + " ()"); 606 pw.println("\t{"); 607 pw.print("\t\tif (discriminator != "); 608 609 boolean defaultFound = false; 610 for (int i = 0; i < caseLabelNum; i++) 611 { 612 if (label[ i ] == null) 613 { 614 defaultFound = true; 615 pw.print (defaultStr); 616 } 617 else 618 { 619 pw.print (label[ i ]); 620 } 621 622 if (i < caseLabelNum - 1) 623 { 624 pw.print(" && discriminator != "); 625 } 626 } 627 628 630 if (defaultFound) 631 { 632 for (int i = 0; i < unusedCaseLabels.size (); i++) 633 { 634 String lab = (String ) unusedCaseLabels.elementAt (i); 635 if (! lab.equals (defaultStr)) 636 { 637 pw.print (" && discriminator != " + lab); 638 } 639 } 640 } 641 642 pw.println(")\n\t\t\tthrow new org.omg.CORBA.BAD_OPERATION();"); 643 pw.println("\t\treturn " + c.element_spec.d.name() + ";"); 644 pw.println("\t}\n"); 645 646 648 pw.println("\tpublic void " + c.element_spec.d.name() + 649 " (" + c.element_spec.t.typeName() + " _x)"); 650 pw.println("\t{"); 651 652 pw.print("\t\tdiscriminator = "); 653 654 if (label[ 0 ] == null) 655 pw.println(defaultStr + ";"); 656 else 657 pw.println(label[ 0 ] + ";"); 658 pw.println("\t\t" + c.element_spec.d.name() + " = _x;"); 659 pw.println("\t}\n"); 660 661 if (caseLabelNum > 1 || thisCaseIsDefault) 662 { 663 pw.println("\tpublic void " + c.element_spec.d.name() + " (" + 664 ts.typeName() + " _discriminator, " + 665 c.element_spec.t.typeName() + " _x)"); 666 pw.println("\t{"); 667 pw.print("\t\tif (_discriminator != "); 668 669 defaultFound = false; 670 for (int i = 0; i < caseLabelNum; i++) 671 { 672 if (label[ i ] == null) 673 { 674 defaultFound = true; 675 pw.print(defaultStr); 676 } 677 else 678 { 679 pw.print(label[ i ]); 680 } 681 682 if (i < caseLabelNum - 1) 683 { 684 pw.print(" && _discriminator != "); 685 } 686 } 687 688 690 if (defaultFound) 691 { 692 for (int i = 0; i < unusedCaseLabels.size (); i++) 693 { 694 String lab = (String ) unusedCaseLabels.elementAt (i); 695 if (! lab.equals (defaultStr)) 696 { 697 pw.print (" && discriminator != " + lab); 698 } 699 } 700 } 701 702 pw.println(")\n\t\t\tthrow new org.omg.CORBA.BAD_OPERATION();"); 703 pw.println("\t\tdiscriminator = _discriminator;"); 704 pw.println("\t\t" + c.element_spec.d.name() + " = _x;"); 705 pw.println("\t}\n"); 706 } 707 } 708 709 712 713 if (def == 0 && defaultStr.length() > 0) 714 { 715 pw.println("\tpublic void __default ()"); 716 pw.println("\t{"); 717 pw.println("\t\tdiscriminator = " + defaultStr + ";"); 718 pw.println("\t}"); 719 720 pw.println("\tpublic void __default (" + ts.typeName() + " _discriminator)"); 721 pw.println("\t{"); 722 pw.println("\t\tdiscriminator = _discriminator;"); 723 pw.println("\t}"); 724 } 725 726 pw.println("}"); 727 } 728 729 730 public void printHolderClass(String className, PrintWriter ps) 731 { 732 if (Environment.JAVA14 && pack_name.equals("")) 733 lexer.emit_warn 734 ("No package defined for " + className + " - illegal in JDK1.4", token); 735 if (!pack_name.equals("")) 736 ps.println("package " + pack_name + ";"); 737 738 printClassComment(className, ps); 739 740 ps.println("public" + parser.getFinalString() + " class " + className + "Holder"); 741 ps.println("\timplements org.omg.CORBA.portable.Streamable"); 742 ps.println("{"); 743 744 ps.println("\tpublic " + className + " value;\n"); 745 746 ps.println("\tpublic " + className + "Holder ()"); 747 ps.println("\t{"); 748 ps.println("\t}"); 749 750 ps.println("\tpublic " + className + "Holder (final " + className + " initial)"); 751 ps.println("\t{"); 752 ps.println("\t\tvalue = initial;"); 753 ps.println("\t}"); 754 755 ps.println("\tpublic org.omg.CORBA.TypeCode _type ()"); 756 ps.println("\t{"); 757 ps.println("\t\treturn " + className + "Helper.type ();"); 758 ps.println("\t}"); 759 760 ps.println("\tpublic void _read (final org.omg.CORBA.portable.InputStream in)"); 761 ps.println("\t{"); 762 ps.println("\t\tvalue = " + className + "Helper.read (in);"); 763 ps.println("\t}"); 764 765 ps.println("\tpublic void _write (final org.omg.CORBA.portable.OutputStream out)"); 766 ps.println("\t{"); 767 ps.println("\t\t" + className + "Helper.write (out, value);"); 768 ps.println("\t}"); 769 770 ps.println("}"); 771 } 772 773 private void printHelperClass (String className, PrintWriter ps) 774 { 775 if (Environment.JAVA14 && pack_name.equals("")) 776 lexer.emit_warn 777 ("No package defined for " + className + " - illegal in JDK1.4", token); 778 if (!pack_name.equals ("")) 779 { 780 ps.println ("package " + pack_name + ";"); 781 } 782 783 printImport(ps); 784 785 printClassComment(className, ps); 786 787 ps.println("public" + parser.getFinalString() + " class " + className + "Helper"); 788 ps.println("{"); 789 ps.println("\tprivate static org.omg.CORBA.TypeCode _type;"); 790 791 String _type = typeName(); 792 793 TypeSpec.printInsertExtractMethods(ps, typeName()); 794 795 printIdMethod(ps); 796 797 798 799 ps.println("\tpublic static " + className + " read (org.omg.CORBA.portable.InputStream in)"); 800 ps.println("\t{"); 801 ps.println("\t\t" + className + " result = new " + className + " ();"); 802 803 TypeSpec switch_ts_resolved = switch_type_spec; 804 805 if (switch_type_spec.type_spec instanceof ScopedName) 806 { 807 switch_ts_resolved = ((ScopedName)switch_type_spec.type_spec).resolvedTypeSpec(); 808 } 809 810 String indent1 = "\t\t\t"; 811 String indent2 = "\t\t\t\t"; 812 if (switch_is_longlong) 813 { 814 indent1 = "\t\t"; 815 indent2 = "\t\t\t"; 816 } 817 818 String case_str = "case "; 819 String colon_str = ":"; 820 String default_str = "default:"; 821 822 if (switch_is_enum) 823 { 824 ps.println("\t\t" + switch_ts_resolved.toString() + " disc = " + 825 switch_ts_resolved.toString() + ".from_int(in.read_long());"); 826 ps.println("\t\tswitch (disc.value ())"); 827 ps.println("\t\t{"); 828 } 829 else 830 { 831 ps.println ("\t\t" + switch_ts_resolved.toString () + " " 832 + switch_ts_resolved.printReadStatement ("disc", "in")); 833 if (switch_is_bool) 834 { 835 836 837 case_str = "if (disc == "; 838 colon_str = ")"; 839 default_str = "else"; 840 } 841 else if (switch_is_longlong) 842 { 843 844 845 case_str = "if (disc == "; 846 colon_str = ")"; 847 default_str = "else"; 848 } 849 else 850 { 851 ps.println("\t\tswitch (disc)"); 852 ps.println("\t\t{"); 853 } 854 } 855 856 Enumeration e; 857 Case cse; 858 ByteArrayOutputStream bos; 859 PrintWriter caseWriter; 860 boolean was_default; 861 int caseLabelNum; 862 String defaultCases = null; 863 864 e = switch_body.caseListVector.elements (); 865 while (e.hasMoreElements ()) 866 { 867 bos = new ByteArrayOutputStream (); 868 caseWriter = new PrintWriter (bos); 869 was_default = false; 870 cse = (Case) e.nextElement(); 871 caseLabelNum = cse.case_label_list.v.size(); 872 TypeSpec t = cse.element_spec.t; 873 Declarator d = cse.element_spec.d; 874 875 for (int i = 0; i < caseLabelNum; i++) 876 { 877 Object o = cse.case_label_list.v.elementAt (i); 878 879 if (o == null) 880 { 881 883 caseWriter.println (indent1 + default_str); 884 was_default = true; 885 } 886 else if (o instanceof ConstExpr) 887 { 888 caseWriter.println (indent1 + case_str 889 + ((ConstExpr)o).value () + colon_str); 890 } 891 else if (o instanceof ScopedName) 892 { 893 String _t = ((ScopedName)o).typeName (); 894 if (switch_is_enum) 895 { 896 caseWriter.println (indent1 + case_str 897 + _t.substring (0, _t.lastIndexOf ('.') + 1) 898 + "_" + _t.substring (_t.lastIndexOf ('.') + 1) 899 + colon_str); 900 } 901 else 902 { 903 caseWriter.println (indent1 + case_str + _t + colon_str); 904 } 905 } 906 907 if (i == caseLabelNum - 1) 908 { 909 caseWriter.println (indent1 + "{"); 910 911 if (t instanceof ScopedName) 912 { 913 t = ((ScopedName)t).resolvedTypeSpec (); 914 } 915 t = t.typeSpec (); 916 917 String varname = "_var"; 918 caseWriter.println (indent2 + t.typeName () + " " + varname + ";"); 919 caseWriter.println (indent2 + t.printReadStatement (varname, "in")); 920 caseWriter.print (indent2 + "result." + d.name () + " ("); 921 if (caseLabelNum > 1) 922 { 923 caseWriter.print ("disc,"); 924 } 925 caseWriter.println (varname + ");"); 926 927 929 if (o != null && !switch_is_bool && !switch_is_longlong) 930 { 931 caseWriter.println (indent2 + "break;"); 932 } 933 if (switch_is_longlong) 934 { 935 caseWriter.println (indent2 + "return result;"); 936 } 937 caseWriter.println (indent1 + "}"); 938 } 939 } 940 if (switch_is_bool && !was_default) 941 { 942 case_str = "else " + case_str; 943 } 944 945 947 caseWriter.close (); 948 if (bos.size () > 0) 949 { 950 if (was_default) 951 { 952 defaultCases = bos.toString (); 953 } 954 else 955 { 956 ps.print (bos.toString ()); 957 } 958 } 959 } 960 961 if 962 ( 963 !explicit_default_case && !switch_is_bool 964 && !switch_is_longlong && !allCasesCovered 965 ) 966 { 967 ps.println ("\t\t\tdefault: result.__default (disc);"); 968 } 969 if (!explicit_default_case && switch_is_longlong) 970 { 971 ps.println ("\t\tresult.__default (disc);"); 972 ps.println ("\t\treturn result;"); 973 } 974 975 977 if (defaultCases != null) 978 { 979 ps.print (defaultCases); 980 } 981 982 if (!switch_is_bool && !switch_is_longlong) 983 { 984 ps.println ("\t\t}"); } 986 if (!switch_is_longlong) 987 { 988 ps.println ("\t\treturn result;"); 989 } 990 ps.println ("\t}"); 991 992 993 994 ps.println ("\tpublic static void write (org.omg.CORBA.portable.OutputStream out, " + className + " s)"); 995 ps.println ("\t{"); 996 997 999 if (switch_is_enum) 1000 { 1001 ps.println ("\t\tout.write_long (s.discriminator().value ());"); 1002 ps.println ("\t\tswitch (s.discriminator().value ())"); 1003 ps.println ("\t\t{"); 1004 } 1005 else 1006 { 1007 ps.println ("\t\t" + switch_type_spec.typeSpec().printWriteStatement("s.discriminator ()", "out")); 1008 if (switch_is_bool) 1009 { 1010 1011 case_str = "if (s.discriminator () =="; 1012 } 1014 else if (switch_is_longlong) 1015 { 1016 ps.println ("\t\tlong disc = s.discriminator ();"); 1017 } 1018 else 1019 { 1020 ps.println ("\t\tswitch (s.discriminator ())"); 1021 ps.println ("\t\t{"); 1022 } 1023 } 1024 1025 1027 defaultCases = null; 1028 1029 e = switch_body.caseListVector.elements (); 1030 while (e.hasMoreElements ()) 1031 { 1032 was_default = false; 1033 bos = new ByteArrayOutputStream (); 1034 caseWriter = new PrintWriter (bos); 1035 cse = (Case) e.nextElement (); 1036 TypeSpec t = cse.element_spec.t; 1037 Declarator d = cse.element_spec.d; 1038 caseLabelNum = cse.case_label_list.v.size (); 1039 Object o; 1040 1041 for (int i = 0; i < caseLabelNum; i++) 1042 { 1043 o = cse.case_label_list.v.elementAt (i); 1044 1045 if (o == null) 1046 { 1047 1049 caseWriter.println (indent1 + default_str); 1050 was_default = true; 1051 } 1052 else if (o != null && o instanceof ConstExpr) 1053 { 1054 caseWriter.println (indent1 + case_str 1055 + ((ConstExpr)o).value () + colon_str); 1056 } 1057 else if (o instanceof ScopedName) 1058 { 1059 String _t = ((ScopedName)o).typeName (); 1060 if (switch_is_enum) 1061 { 1062 caseWriter.println (indent1 + case_str 1063 + _t.substring (0, _t.lastIndexOf ('.') + 1) 1064 + "_" + _t.substring (_t.lastIndexOf ('.') + 1) 1065 + colon_str); 1066 } 1067 else 1068 { 1069 caseWriter.println (indent1 + case_str + _t + colon_str); 1070 } 1071 } 1072 1073 if (i == caseLabelNum - 1) 1074 { 1075 caseWriter.println (indent1 + "{"); 1076 1077 if (t instanceof ScopedName) 1078 { 1079 t = ((ScopedName)t).resolvedTypeSpec (); 1080 } 1081 t = t.typeSpec (); 1082 1083 caseWriter.println (indent2 + t.printWriteStatement ("s." + d.name () 1084 + " ()", "out")); 1085 1086 1088 if (o != null && !switch_is_bool && !switch_is_longlong) 1089 { 1090 caseWriter.println (indent2 + "break;"); 1091 } 1092 if (switch_is_longlong) 1093 { 1094 caseWriter.println (indent2 + "return;"); 1095 } 1096 caseWriter.println (indent1 + "}"); 1097 } 1098 } 1099 1100 if (switch_is_bool && !was_default) 1101 { 1102 case_str = "else " + case_str; 1103 } 1104 1105 1107 caseWriter.close (); 1108 if (bos.size () > 0) 1109 { 1110 if (was_default) 1111 { 1112 defaultCases = bos.toString (); 1113 } 1114 else 1115 { 1116 ps.print (bos.toString ()); 1117 } 1118 } 1119 } 1120 1121 1123 if (defaultCases != null) 1124 { 1125 ps.print (defaultCases); 1126 } 1127 1128 1129 1130 if (!switch_is_bool && !switch_is_longlong) 1131 { 1132 ps.println ("\t\t}"); 1133 } 1134 1135 ps.println ("\t}"); 1136 1137 1138 1139 ps.println("\tpublic static org.omg.CORBA.TypeCode type ()"); 1140 ps.println("\t{"); 1141 ps.println("\t\tif (_type == null)"); 1142 ps.println("\t\t{"); 1143 ps.println("\t\t\torg.omg.CORBA.UnionMember[] members = new org.omg.CORBA.UnionMember[" + labels + "];"); 1144 ps.println("\t\t\torg.omg.CORBA.Any label_any;"); 1145 1146 TypeSpec label_t = switch_type_spec.typeSpec(); 1147 1148 while(label_t instanceof ScopedName || label_t instanceof AliasTypeSpec) 1149 { 1150 if (label_t instanceof ScopedName) 1151 label_t = ((ScopedName)label_t).resolvedTypeSpec(); 1152 if (label_t instanceof AliasTypeSpec) 1153 label_t = ((AliasTypeSpec)label_t).originalType(); 1154 } 1155 1156 label_t = label_t.typeSpec(); 1157 e = switch_body.caseListVector.elements (); 1158 1159 int mi = -1; 1160 for (int i=0; i<switch_body.caseListVector.size (); i++) 1161 { 1162 mi += ((Case)switch_body.caseListVector.get (i)).case_label_list.v.size (); 1163 } 1164 1165 while (e.hasMoreElements ()) 1166 { 1167 cse = (Case) e.nextElement(); 1168 TypeSpec t = cse.element_spec.t; 1169 1170 while(t instanceof ScopedName || t instanceof AliasTypeSpec) 1171 { 1172 if (t instanceof ScopedName) 1173 t = ((ScopedName)t).resolvedTypeSpec(); 1174 if (t instanceof AliasTypeSpec) 1175 t = ((AliasTypeSpec)t).originalType(); 1176 } 1177 1178 t = t.typeSpec(); 1179 Declarator d = cse.element_spec.d; 1180 1181 caseLabelNum = cse.case_label_list.v.size(); 1182 for (int i = 0; i < caseLabelNum; i++) 1183 { 1184 Object o = cse.case_label_list.v.elementAt(i); 1185 1186 ps.println("\t\t\tlabel_any = org.omg.CORBA.ORB.init().create_any ();"); 1187 1188 if (o == null) 1189 { 1190 ps.println("\t\t\tlabel_any.insert_octet ((byte)0);"); 1191 } 1192 else if (label_t instanceof BaseType) 1193 { 1194 if ((label_t instanceof CharType) || 1195 (label_t instanceof BooleanType) || 1196 (label_t instanceof LongType) || 1197 (label_t instanceof LongLongType)) 1198 { 1199 ps.print("\t\t\tlabel_any." + label_t.printInsertExpression() + " ("); 1200 } 1201 else if (label_t instanceof ShortType) 1202 { 1203 ps.print("\t\t\tlabel_any." + label_t.printInsertExpression() + " ((short)"); 1204 } 1205 else 1206 { 1207 throw new RuntimeException ("Compiler error: unrecognized BaseType: " 1208 + label_t.typeName() + ":" + label_t + ": " + label_t.typeSpec() 1209 + ": " + label_t.getClass().getName()); 1210 } 1211 ps.println(((ConstExpr)o).value() + ");"); 1212 } 1213 else if (switch_is_enum) 1214 { 1215 String _t = ((ScopedName)o).typeName(); 1216 ps.println("\t\t\t" + _t.substring(0, _t.lastIndexOf('.')) 1217 + "Helper.insert(label_any, " + _t + ");"); 1218 } 1219 else 1220 { 1221 throw new RuntimeException ("Compiler error: unrecognized label type: " + label_t.typeName()); 1222 } 1223 1224 ps.print 1225 ( 1226 "\t\t\tmembers[" + 1227 (mi--) + 1228 "] = new org.omg.CORBA.UnionMember (\"" + 1229 d.deEscapeName() + 1230 "\", label_any, " 1231 ); 1232 1233 if (t instanceof ConstrTypeSpec) 1234 { 1235 ps.print(t.typeSpec().toString() + "Helper.type(),"); 1236 } 1237 else 1238 { 1239 ps.print(t.typeSpec().getTypeCodeExpression() + ","); 1240 } 1241 1242 ps.println("null);"); 1243 } 1244 } 1245 ps.print("\t\t\t _type = org.omg.CORBA.ORB.init().create_union_tc(id(),\"" + className() + "\","); 1246 ps.println(switch_type_spec.typeSpec().getTypeCodeExpression() + ", members);"); 1247 ps.println("\t\t}"); 1248 ps.println("\t\treturn _type;"); 1249 ps.println("\t}"); 1250 1251 ps.println("}"); } 1253 1254 1255 1256 public void print(PrintWriter ps) 1257 { 1258 setPrintPhaseNames(); 1259 1260 if (included && !generateIncluded()) 1262 return; 1263 1264 if (!written) 1266 { 1267 if (switch_type_spec != null) 1269 { 1270 try 1271 { 1272 switch_body.print(ps); 1273 1274 String className = className(); 1275 1276 String path = parser.out_dir + fileSeparator + 1277 pack_name.replace('.', fileSeparator); 1278 1279 File dir = new File (path); 1280 if (!dir.exists()) 1281 { 1282 if (!dir.mkdirs()) 1283 { 1284 org.jacorb.idl.parser.fatal_error("Unable to create " + path, null); 1285 } 1286 } 1287 1288 1289 String fname = className + ".java"; 1290 File f = new File (dir, fname); 1291 if (GlobalInputStream.isMoreRecentThan(f)) 1292 { 1293 PrintWriter printWriter = new PrintWriter (new java.io.FileWriter (f)); 1295 printUnionClass(className, printWriter); 1296 printWriter.close(); 1297 } 1298 1299 fname = className + "Holder.java"; 1300 f = new File (dir, fname); 1301 if (GlobalInputStream.isMoreRecentThan(f)) 1302 { 1303 PrintWriter printWriter = new PrintWriter (new java.io.FileWriter (f)); 1305 printHolderClass(className, printWriter); 1306 printWriter.close(); 1307 } 1308 1309 fname = className + "Helper.java"; 1310 f = new File (dir, fname); 1311 if (GlobalInputStream.isMoreRecentThan(f)) 1312 { 1313 PrintWriter printWriter = new PrintWriter (new java.io.FileWriter (f)); 1315 printHelperClass(className, printWriter); 1316 printWriter.close(); 1317 } 1318 1319 written = true; 1320 } 1321 catch (java.io.IOException i) 1322 { 1323 throw new RuntimeException ("File IO error" + i); 1324 } 1325 } 1326 } 1327 } 1328 1329 private TypeSpec getElementType(ElementSpec element) 1330 { 1331 TypeSpec tspec = element.t; 1332 1333 1337 if (element.d.d instanceof ArrayDeclarator) 1338 { 1339 tspec = new ArrayTypeSpec(new_num(), tspec, 1340 (ArrayDeclarator)element.d.d, pack_name); 1341 tspec.parse(); 1342 } 1343 return tspec; 1344 } 1345 1346 1348 1349 public void accept(IDLTreeVisitor visitor) 1350 { 1351 visitor.visitUnion(this); 1352 } 1353 1354 1355} 1356 | Popular Tags |