1 20 21 package org.jacorb.idl; 22 23 import java.io.PrintWriter ; 24 import java.util.*; 25 26 30 31 public class OpDecl 32 extends Declaration 33 implements Operation 34 { 35 public static final int NO_ATTRIBUTE = 0; 36 public static final int ONEWAY = 1; 37 38 public int opAttribute; public TypeSpec opTypeSpec; 40 public Vector paramDecls; 41 public RaisesExpr raisesExpr; 42 public IdlSymbol myInterface; 43 44 public OpDecl( int num ) 45 { 46 super( num ); 47 paramDecls = new Vector(); 48 } 49 50 53 public OpDecl (IdlSymbol myInterface, 54 int opAttribute, 55 TypeSpec opTypeSpec, 56 String name, 57 List paramDecls, 58 RaisesExpr raisesExpr) 59 { 60 super (new_num()); 61 this.myInterface = myInterface; 62 this.opAttribute = opAttribute; 63 this.opTypeSpec = opTypeSpec; 64 this.name = name; 65 this.paramDecls = new Vector (paramDecls); 66 this.raisesExpr = raisesExpr; 67 setEnclosingSymbol (myInterface); 68 this.pack_name = myInterface.full_name(); 69 } 70 71 75 public OpDecl (IdlSymbol myInterface, 76 String name, 77 List paramDecls) 78 { 79 this (myInterface, 80 NO_ATTRIBUTE, 81 new VoidTypeSpec (new_num()), 82 name, 83 paramDecls, 84 new RaisesExpr (new_num())); 85 } 86 87 public void setPackage( String s ) 88 { 89 s = parser.pack_replace( s ); 90 91 if( pack_name.length() > 0 ) 92 pack_name = s + "." + pack_name; 93 else 94 pack_name = s; 95 opTypeSpec.setPackage( s ); 96 97 for( Enumeration e = paramDecls.elements(); 98 e.hasMoreElements(); 99 ( (ParamDecl)e.nextElement() ).setPackage( s ) 100 ) 101 ; 102 raisesExpr.setPackage( s ); 103 } 104 105 public void setEnclosingSymbol( IdlSymbol s ) 106 { 107 if( logger.isDebugEnabled() ) 108 logger.debug( "opDecl.setEnclosingSymbol " + s ); 109 110 if( enclosing_symbol != null && enclosing_symbol != s ) 111 throw new RuntimeException ( "Compiler Error: trying to reassign container for " 112 + name ); 113 if( s == null ) 114 throw new RuntimeException ( "Compiler Error: enclosing symbol is null!"); 115 116 enclosing_symbol = s; 117 raisesExpr.setEnclosingSymbol( s ); 118 } 119 120 public void parse() 121 { 122 if( enclosing_symbol == null ) 123 throw new RuntimeException ( "Compiler Error: enclosing symbol in parse is null!"); 124 125 myInterface = enclosing_symbol; 126 127 if( opAttribute == ONEWAY ) 128 { 129 if( !raisesExpr.empty() ) 130 parser.error( "Oneway operation " + full_name() + 131 " may not define a raises clause.", token ); 132 133 if( !( opTypeSpec.typeSpec() instanceof VoidTypeSpec ) ) 134 parser.error( "Oneway operation " + full_name() + 135 " may only define void as return type.", token ); 136 } 137 138 try 139 { 140 NameTable.define( full_name(), "operation" ); 141 } 142 catch( NameAlreadyDefined nad ) 143 { 144 parser.error( "Operation " + full_name() + " already defined", token ); 145 } 146 147 for( Enumeration e = paramDecls.elements(); e.hasMoreElements(); ) 148 { 149 ParamDecl param = (ParamDecl)e.nextElement(); 150 param.parse(); 151 try 152 { 153 NameTable.define( full_name() + "." + 154 param.simple_declarator.name(), 155 "argument" ); 156 } 157 catch( NameAlreadyDefined nad ) 158 { 159 parser.error( "Argument " + param.simple_declarator.name() + 160 " already defined in operation " + full_name(), 161 token ); 162 } 163 164 if( param.paramAttribute != ParamDecl.MODE_IN ) 165 { 166 myInterface.addImportedNameHolder( param.paramTypeSpec.holderName() ); 168 } 169 if( !(param.paramTypeSpec.typeSpec() instanceof BaseType )) 170 { 171 172 if( logger.isInfoEnabled() ) 173 logger.info( "classname: " + 174 param.paramTypeSpec.typeSpec().getClass().getName() ); 175 176 myInterface.addImportedName( param.paramTypeSpec.typeSpec().full_name(), 177 param.paramTypeSpec.typeSpec() ); 178 } 179 if( param.paramTypeSpec.typeSpec() instanceof ConstrTypeSpec && 180 ((ConstrTypeSpec )param.paramTypeSpec.typeSpec()).c_type_spec instanceof StructType && 181 ((StructType )((ConstrTypeSpec )param.paramTypeSpec.typeSpec()).c_type_spec).exc == true ) 182 { 183 parser.error( "Can't pass an exception as a parameter."); 184 } 185 } 186 187 188 if( opTypeSpec.typeSpec() instanceof ScopedName ) 189 { 190 TypeSpec ts = 191 ( (ScopedName)opTypeSpec.typeSpec() ).resolvedTypeSpec(); 192 193 if( ts != null ) 194 opTypeSpec = ts; 195 196 myInterface.addImportedName( opTypeSpec.typeName() ); 197 } 198 raisesExpr.parse(); 199 } 200 201 202 public void print( PrintWriter ps ) 203 { 204 if( is_pseudo ) 205 ps.print( "\tpublic abstract " + opTypeSpec.toString() ); 206 else 207 ps.print( "\t" + opTypeSpec.toString() ); 208 ps.print( " " ); 209 ps.print( name ); 210 211 ps.print( "(" ); 212 Enumeration e = paramDecls.elements(); 213 if( e.hasMoreElements() ) 214 ( (ParamDecl)e.nextElement() ).print( ps ); 215 216 for( ; e.hasMoreElements(); ) 217 { 218 ps.print( ", " ); 219 ( (ParamDecl)e.nextElement() ).print( ps ); 220 } 221 ps.print( ")" ); 222 raisesExpr.print( ps ); 223 ps.println( ";" ); 224 } 225 226 public void printMethod( PrintWriter ps, 227 String classname, 228 boolean is_local, 229 boolean is_abstract) 230 { 231 233 234 String idl_name = ( name.startsWith( "_" ) ? name.substring( 1 ) : name ); 235 236 ps.print( "\tpublic " + opTypeSpec.toString() + " " + name + "(" ); 237 238 Enumeration e = paramDecls.elements(); 239 if( e.hasMoreElements() ) 240 ( (ParamDecl)e.nextElement() ).print( ps ); 241 242 for( ; e.hasMoreElements(); ) 243 { 244 ps.print( ", " ); 245 ( (ParamDecl)e.nextElement() ).print( ps ); 246 } 247 248 ps.print( ")" ); 249 raisesExpr.print( ps ); 250 ps.println( "\n\t{" ); 251 ps.println( "\t\twhile(true)" ); 252 ps.println( "\t\t{" ); 253 if( !is_local ) 256 { 257 ps.println( "\t\tif(! this._is_local())" ); 258 ps.println( "\t\t{" ); 259 ps.println( "\t\t\torg.omg.CORBA.portable.InputStream _is = null;" ); 260 ps.println( "\t\t\ttry" ); 261 ps.println( "\t\t\t{" ); 262 ps.print( "\t\t\t\torg.omg.CORBA.portable.OutputStream _os = _request( \"" + idl_name + "\"," ); 263 264 if( opAttribute == NO_ATTRIBUTE ) 265 ps.println( " true);" ); 266 else 267 ps.println( " false);" ); 268 269 271 for( e = paramDecls.elements(); e.hasMoreElements(); ) 272 { 273 ParamDecl p = ( (ParamDecl)e.nextElement() ); 274 if( p.paramAttribute != ParamDecl.MODE_OUT ) 275 ps.println( "\t\t\t\t" + p.printWriteStatement( "_os" ) ); 276 } 277 278 ps.println( "\t\t\t\t_is = _invoke(_os);" ); 279 280 if( opAttribute == 0 && 281 !( opTypeSpec.typeSpec() instanceof VoidTypeSpec ) ) 282 { 283 ps.println( "\t\t\t\t" + opTypeSpec.toString() + " _result = " + 284 opTypeSpec.typeSpec().printReadExpression( "_is" ) + ";" ); 285 } 286 287 for( Enumeration e2 = paramDecls.elements(); e2.hasMoreElements(); ) 288 { 289 ParamDecl p = (ParamDecl)e2.nextElement(); 290 if( p.paramAttribute != ParamDecl.MODE_IN ) 291 { 292 ps.println( "\t\t\t\t" + p.simple_declarator + ".value = " + 293 p.printReadExpression( "_is" ) + ";" ); 294 } 295 } 296 297 if( opAttribute == NO_ATTRIBUTE && 298 !( opTypeSpec.typeSpec() instanceof VoidTypeSpec ) ) 299 { 300 ps.println( "\t\t\t\treturn _result;" ); 301 } 302 else 303 ps.println( "\t\t\t\treturn;" ); 304 305 306 307 ps.println( "\t\t\t}" ); 308 ps.println( "\t\t\tcatch( org.omg.CORBA.portable.RemarshalException _rx ){}" ); 309 ps.println( "\t\t\tcatch( org.omg.CORBA.portable.ApplicationException _ax )" ); 310 ps.println( "\t\t\t{" ); 311 ps.println( "\t\t\t\tString _id = _ax.getId();" ); 312 313 if( !raisesExpr.empty() ) 314 { 315 String [] exceptIds = raisesExpr.getExceptionIds(); 316 String [] classNames = raisesExpr.getExceptionClassNames(); 317 ps.print( "\t\t\t\t" ); 318 for( int i = 0; i < exceptIds.length; i++ ) 319 { 320 ps.println( "if( _id.equals(\"" + exceptIds[ i ] + "\"))" ); 321 ps.println( "\t\t\t\t{" ); 322 ps.println( "\t\t\t\t\tthrow " + classNames[ i ] + "Helper.read(_ax.getInputStream());" ); 323 ps.println( "\t\t\t\t}" ); 324 ps.print( "\t\t\t\telse " ); 325 } 326 ps.print( "\n\t" ); 327 } 328 ps.println( "\t\t\t\tthrow new RuntimeException(\"Unexpected exception \" + _id );" ); 329 ps.println( "\t\t\t}" ); 330 ps.println( "\t\t\tfinally" ); 331 ps.println( "\t\t\t{" ); 332 ps.println( "\t\t\t\tthis._releaseReply(_is);" ); 333 ps.println( "\t\t\t}" ); 334 335 ps.println( "\t\t}" ); 336 ps.println( "\t\telse" ); 338 ps.println( "\t\t{" ); 339 } 340 341 ps.println( "\t\t\torg.omg.CORBA.portable.ServantObject _so = _servant_preinvoke( \"" + idl_name + "\", _opsClass );" ); 342 343 ps.println( "\t\t\tif( _so == null )" ); 344 ps.println( "\t\t\t\tthrow new org.omg.CORBA.UNKNOWN(\"local invocations not supported!\");" ); 345 346 if( is_abstract ) 347 { 348 ps.println( "\t\t\t" + classname + " _localServant = (" + 349 classname + ")_so.servant;" ); 350 } 351 else 352 { 353 ps.println( "\t\t\t" + classname + "Operations _localServant = (" + 354 classname + "Operations)_so.servant;" ); 355 } 356 357 if( opAttribute == 0 && 358 !( opTypeSpec.typeSpec() instanceof VoidTypeSpec ) ) 359 { 360 ps.print( "\t\t\t" + opTypeSpec.toString() + " _result;" ); 361 } 362 363 ps.println( "\t\t\ttry" ); 364 ps.println( "\t\t\t{" ); 365 366 if( opAttribute == 0 && 367 !( opTypeSpec.typeSpec() instanceof VoidTypeSpec ) ) 368 { 369 ps.print( "\t\t\t_result = " ); 370 } 371 else 372 ps.print( "\t\t\t" ); 373 374 ps.print( "_localServant." + name + "(" ); 375 376 for( e = paramDecls.elements(); e.hasMoreElements(); ) 377 { 378 ParamDecl p = ( (ParamDecl)e.nextElement() ); 379 ps.print( p.simple_declarator.toString() ); 380 if( e.hasMoreElements() ) 381 ps.print( "," ); 382 } 383 ps.println( ");" ); 384 385 ps.println( "\t\t\t}" ); 386 ps.println( "\t\t\tfinally" ); 387 ps.println( "\t\t\t{" ); 388 ps.println( "\t\t\t\t_servant_postinvoke(_so);" ); 389 ps.println( "\t\t\t}" ); 390 391 if( opAttribute == 0 && !( opTypeSpec.typeSpec() instanceof VoidTypeSpec ) ) 392 { 393 ps.println( "\t\t\treturn _result;" ); 394 } 395 else 396 ps.println( "\t\t\treturn;" ); 397 398 399 if( !is_local ) ps.println( "\t\t}\n" ); 400 401 ps.println( "\t\t}\n" ); ps.println( "\t}\n" ); } 404 405 406 public void print_sendc_Method( PrintWriter ps, 407 String classname ) 408 { 409 411 412 String idl_name = ( name.startsWith( "_" ) ? name.substring( 1 ) : name ); 413 414 ps.print( "\tpublic void sendc_" + name + "(" ); 415 416 ps.print( "AMI_" + classname + "Handler ami_handler" ); 417 418 for ( Iterator i = paramDecls.iterator(); i.hasNext(); ) 419 { 420 ParamDecl p = ( ParamDecl ) i.next(); 421 if ( p.paramAttribute != ParamDecl.MODE_OUT ) 422 { 423 ps.print( ", " ); 424 p.asIn().print( ps ); 425 } 426 } 427 428 ps.print( ")" ); 429 ps.println( "\n\t{" ); 430 ps.println( "\t\twhile(true)" ); 431 ps.println( "\t\t{" ); 432 ps.println( "\t\t\ttry" ); 433 ps.println( "\t\t\t{" ); 434 ps.print( "\t\t\t\torg.omg.CORBA.portable.OutputStream _os = _request( \"" + idl_name + "\"," ); 435 436 if( opAttribute == NO_ATTRIBUTE ) 437 ps.println( " true);" ); 438 else 439 ps.println( " false);" ); 440 441 443 for( Iterator i = paramDecls.iterator(); i.hasNext(); ) 444 { 445 ParamDecl p = ( (ParamDecl)i.next() ); 446 if( p.paramAttribute != ParamDecl.MODE_OUT ) 447 ps.println( "\t\t\t\t" + p.asIn().printWriteStatement( "_os" ) ); 448 } 449 450 ps.println( "\t\t\t\t((org.jacorb.orb.Delegate)_get_delegate()).invoke(this, _os, ami_handler);" ); 451 ps.println( "\t\t\t\treturn;"); 452 453 454 455 ps.println( "\t\t\t}" ); 456 ps.println( "\t\t\tcatch( org.omg.CORBA.portable.RemarshalException _rx ){}" ); 457 ps.println( "\t\t\tcatch( org.omg.CORBA.portable.ApplicationException _ax )" ); 458 ps.println( "\t\t\t{" ); 459 ps.println( "\t\t\t\tString _id = _ax.getId();" ); 460 ps.println( "\t\t\t}" ); 461 462 ps.println( "\t\t}\n" ); ps.println( "\t}\n" ); } 465 466 public void printDelegatedMethod( PrintWriter ps ) 467 { 468 ps.print( "\tpublic " + opTypeSpec.toString() + " " + name + "(" ); 469 470 Enumeration e = paramDecls.elements(); 471 if( e.hasMoreElements() ) 472 ( (ParamDecl)e.nextElement() ).print( ps ); 473 474 for( ; e.hasMoreElements(); ) 475 { 476 ps.print( ", " ); 477 ( (ParamDecl)e.nextElement() ).print( ps ); 478 } 479 480 ps.print( ")" ); 481 raisesExpr.print( ps ); 482 ps.println( "\n\t{" ); 483 484 485 if( opAttribute == NO_ATTRIBUTE && 486 !( opTypeSpec.typeSpec() instanceof VoidTypeSpec ) ) 487 { 488 ps.print( "\t\treturn " ); 489 } 490 491 ps.print( "_delegate." + name + "(" ); 492 e = paramDecls.elements(); 493 if( e.hasMoreElements() ) 494 ps.print( ( (ParamDecl)e.nextElement() ).simple_declarator ); 495 496 for( ; e.hasMoreElements(); ) 497 { 498 ps.print( "," ); 499 ps.print( ( (ParamDecl)e.nextElement() ).simple_declarator ); 500 } 501 ps.println( ");" ); 502 ps.println( "\t}\n" ); 503 } 504 505 public void printInvocation( PrintWriter ps ) 506 { 507 if( !raisesExpr.empty() ) 508 { 509 ps.println( "\t\t\ttry" ); 510 ps.println( "\t\t\t{" ); 511 } 512 513 514 515 int argc = 0; 516 boolean holders = false; 517 518 for( Enumeration e = paramDecls.elements(); e.hasMoreElements(); ) 519 { 520 ParamDecl p = (ParamDecl)e.nextElement(); 521 TypeSpec ts = p.paramTypeSpec.typeSpec(); 522 523 boolean is_wstring = 524 ( ( ts instanceof StringType ) && ( ( (StringType)ts ).isWide() ) ); 525 526 boolean is_wchar = 527 ( ( ts instanceof CharType ) && ( ( (CharType)ts ).isWide() ) ); 528 529 if( p.paramAttribute == ParamDecl.MODE_IN ) 530 { 531 ps.println( "\t\t\t\t" + ts.toString() + " _arg" + ( argc++ ) + 532 "=" + ts.printReadExpression( "_input" ) + ";" ); 533 } 534 else 535 { 536 holders = true; 537 ps.println( "\t\t\t\t" + ts.holderName() + " _arg" + ( argc++ ) + 538 "= new " + ts.holderName() + "();" ); 539 if( p.paramAttribute == ParamDecl.MODE_INOUT ) 540 { 541 546 if( is_wchar ) 547 { 548 ps.println( "\t\t\t\t_arg" + ( argc - 1 ) + ".value = _input.read_wchar ();" ); 549 } 550 else if( is_wstring ) 551 { 552 ps.println( "\t\t\t\t_arg" + ( argc - 1 ) + ".value = _input.read_wstring ();" ); 553 } 554 else 555 { 556 ps.println( "\t\t\t\t_arg" + ( argc - 1 ) + "._read (_input);" ); 557 } 558 } 559 } 560 } 561 562 563 boolean complex = 564 ( opTypeSpec.typeSpec() instanceof ArrayTypeSpec ) || 565 ( opTypeSpec.typeSpec() instanceof FixedPointType ); 566 567 String write_str = null, 568 write_str_prefix = null, 569 write_str_suffix = null; 570 571 ps.println( "\t\t\t\t_out = handler.createReply();" ); 574 if( !( opTypeSpec.typeSpec() instanceof VoidTypeSpec ) && !complex ) 575 { 576 write_str = opTypeSpec.typeSpec().printWriteStatement( "**", "_out" ); 577 int index = write_str.indexOf( "**" ); 578 write_str_prefix = write_str.substring( 0, index ); 579 write_str_suffix = write_str.substring( index + 2 ); 580 ps.print( "\t\t\t\t" + write_str_prefix ); 581 } 582 else 583 ps.print( "\t\t\t\t" ); 584 586 587 if( complex ) 588 ps.print( opTypeSpec.typeSpec().typeName() + " _result = " ); 589 590 ps.print( name + "(" ); 591 592 for( int i = 0; i < argc; i++ ) 593 { 594 ps.print( "_arg" + i ); 595 if( i < argc - 1 ) 596 ps.print( "," ); 597 } 598 599 614 615 if( !( opTypeSpec.typeSpec() instanceof VoidTypeSpec ) ) 616 ps.print( ")" ); 617 618 if( !complex ) 619 { 620 if( opTypeSpec.typeSpec() instanceof VoidTypeSpec ) 621 ps.println( ");" ); 622 else 623 ps.println( write_str_suffix ); 624 } 625 else 626 { 627 ps.println( ";" ); 628 ps.println( opTypeSpec.typeSpec().printWriteStatement( "_result", "_out" ) ); 629 } 630 631 632 633 argc = 0; 634 for( Enumeration e = paramDecls.elements(); e.hasMoreElements(); ) 635 { 636 ParamDecl p = (ParamDecl)e.nextElement(); 637 TypeSpec ts = p.paramTypeSpec; 638 if( p.paramAttribute != ParamDecl.MODE_IN ) 639 { 640 ps.println( "\t\t\t\t" + p.printWriteStatement( ( "_arg" + ( argc ) ), "_out" ) ); 641 } 642 argc++; 643 } 644 645 if( !raisesExpr.empty() ) 646 { 647 ps.println( "\t\t\t}" ); 648 String [] excepts = raisesExpr.getExceptionNames(); 649 String [] classNames = raisesExpr.getExceptionClassNames(); 650 for( int i = 0; i < excepts.length; i++ ) 651 { 652 ps.println( "\t\t\tcatch(" + excepts[ i ] + " _ex" + i + ")" ); 653 ps.println( "\t\t\t{" ); 654 ps.println( "\t\t\t\t_out = handler.createExceptionReply();" ); 655 ps.println( "\t\t\t\t" + classNames[ i ] + "Helper.write(_out, _ex" + i + ");" ); 656 ps.println( "\t\t\t}" ); 657 } 658 } 659 660 } 661 662 public String signature() 663 { 664 StringBuffer sb = new StringBuffer (); 665 sb.append( name + "(" ); 666 667 Enumeration e = paramDecls.elements(); 668 if( e.hasMoreElements() ) 669 sb.append( ( (ParamDecl)e.nextElement() ).paramTypeSpec.toString() ); 670 671 for( ; e.hasMoreElements(); ) 672 { 673 sb.append( "," + ( (ParamDecl)e.nextElement() ).paramTypeSpec.toString() ); 674 } 675 sb.append( ")" ); 676 return sb.toString(); 677 } 678 679 680 public String name() 681 { 682 return name; 683 } 684 685 686 public String opName() 687 { 688 return name(); 689 } 690 691 public void printSignature( PrintWriter ps ) 692 { 693 printSignature( ps, false ); 694 } 695 696 699 public void printSignature( PrintWriter ps, boolean printModifiers ) 700 { 701 ps.print( "\t" ); 702 if( printModifiers ) ps.print( "public abstract " ); 703 704 ps.print( opTypeSpec.toString() + " " + name + "(" ); 705 706 for( Enumeration e = paramDecls.elements(); e.hasMoreElements(); ) 707 { 708 ( (ParamDecl)e.nextElement() ).print( ps ); 709 if( e.hasMoreElements() ) ps.print( ", " ); 710 } 711 712 ps.print( ")" ); 713 raisesExpr.print( ps ); 714 ps.println( ";" ); 715 } 716 717 718 721 722 public void getIRInfo( Hashtable irInfoTable ) 723 { 724 StringBuffer sb = new StringBuffer (); 725 boolean enter = false; 726 727 TypeSpec ts = opTypeSpec.typeSpec(); 728 729 if( ts instanceof AliasTypeSpec ) 730 { 731 sb.append( ts.full_name() ); 734 enter = true; 735 } 737 sb.append( "(" ); 738 739 for( Enumeration e = paramDecls.elements(); e.hasMoreElements(); ) 740 { 741 ParamDecl param = (ParamDecl)e.nextElement(); 742 if( param.paramAttribute == ParamDecl.MODE_INOUT ) 743 { 744 sb.append( "inout:" + param.simple_declarator.name + " " ); 745 enter = true; 746 } 747 else if( param.paramAttribute == ParamDecl.MODE_OUT ) 748 { 749 sb.append( "out:" + param.simple_declarator.name + " " ); 750 enter = true; 751 } 752 else sb.append( "in:" + param.simple_declarator.name + " " ); 754 755 ts = param.paramTypeSpec.typeSpec(); 756 757 if( ts instanceof AliasTypeSpec ) 758 { 759 sb.append( ts.full_name() ); 760 enter = true; 761 } 762 763 sb.append( "," ); 764 } 765 766 if( paramDecls.size() > 0 ) 767 { 768 sb.deleteCharAt( sb.length()-1); 770 } 771 sb.append( ")" ); 772 773 if( opAttribute == ONEWAY ) 774 sb.append( "-oneway" ); 775 776 irInfoTable.put( name, sb.toString() ); 778 779 if( logger.isDebugEnabled() ) 780 logger.debug( "OpInfo for " + name + " : " + sb.toString() ); 781 } 782 783 public void accept( IDLTreeVisitor visitor ) 784 { 785 visitor.visitOpDecl( this ); 786 } 787 788 789 790 } 791 | Popular Tags |