1 26 27 package org.objectweb.openccm.generator.idl.lib; 28 29 import org.objectweb.openccm.ast.api.AST; 31 import org.objectweb.openccm.ast.api.AnyValue; 32 import org.objectweb.openccm.ast.api.AttributeDecl; 33 import org.objectweb.openccm.ast.api.ComponentBase; 34 import org.objectweb.openccm.ast.api.ComponentDecl; 35 import org.objectweb.openccm.ast.api.ConstantDecl; 36 import org.objectweb.openccm.ast.api.Declaration; 37 import org.objectweb.openccm.ast.api.DeclarationKind; 38 import org.objectweb.openccm.ast.api.EnumDecl; 39 import org.objectweb.openccm.ast.api.ExceptionDecl; 40 import org.objectweb.openccm.ast.api.ExceptionList; 41 import org.objectweb.openccm.ast.api.HomeDecl; 42 import org.objectweb.openccm.ast.api.Initializer; 43 import org.objectweb.openccm.ast.api.InterfaceDecl; 44 import org.objectweb.openccm.ast.api.InterfaceList; 45 import org.objectweb.openccm.ast.api.OperationDecl; 46 import org.objectweb.openccm.ast.api.Parameter; 47 import org.objectweb.openccm.ast.api.Scope; 48 import org.objectweb.openccm.ast.api.StructDecl; 49 import org.objectweb.openccm.ast.api.TypeRef; 50 import org.objectweb.openccm.ast.api.UnionDecl; 51 import org.objectweb.openccm.ast.api.UnionMember; 52 import org.objectweb.openccm.ast.api.UsesDecl; 53 import org.objectweb.openccm.ast.api.ValueDecl; 54 import org.objectweb.openccm.ast.api.ValueMemberDecl; 55 import org.objectweb.openccm.ast.api.WithNameTypeRef; 56 import org.objectweb.openccm.generator.common.lib.GenerationException; 57 import org.objectweb.openccm.generator.translator.ast2idl.api.AST_IDLTranslator; 58 59 import java.util.ArrayList ; 60 import java.util.List ; 61 62 63 71 72 public class IDL3Generator 73 extends org.objectweb.openccm.generator.common.lib.GeneratorBase 74 implements org.objectweb.openccm.generator.idl.api.IDL3Generator 75 { 76 77 83 86 public AST_IDLTranslator translator_; 87 88 91 protected String last_prefix_; 92 93 96 protected boolean print_macro_; 97 98 99 105 110 111 public IDL3Generator(AST ast) 112 { 113 super(ast); 115 116 translator_ = new org.objectweb.openccm.generator.translator.ast2idl.lib.AST_CIDLTranslator(); 118 last_prefix_ = ""; 119 print_macro_ = true; 120 } 121 122 128 135 protected String 136 get_macro(Scope target_decl) 137 { 138 int idx, idx2; 139 String abs_name = null, 140 res = ""; 141 try{ 142 abs_name = target_decl.getAbsoluteName(); 143 144 idx = abs_name.indexOf(':', 2); 145 idx2 = 2; 146 while (idx!=-1) 147 { 148 res = res + abs_name.substring(idx2, idx) + '_'; 149 idx2 = idx+2; 150 idx = abs_name.indexOf(':', idx2); 151 } 152 res = res + abs_name.substring(idx2); 153 res = "__" + res.toUpperCase() + "__"; 154 }catch(Exception e){ 155 res = "__NO_NAME__"; 156 print_macro_ = false; 157 } 158 return res; 159 } 160 161 165 protected void 166 after_generation() 167 { 168 close("out"); 169 } 170 171 172 178 185 public void 186 initialize( String outputfile, 187 ArrayList imports, 188 String app_name ) 189 { 190 List list = new java.util.ArrayList (); 191 192 193 194 list.clear(); 196 list.add("org/objectweb/openccm/generator/common/common.vm"); 197 list.add("org/objectweb/openccm/generator/idl/idl3.vm"); 198 setLibrary(list); 199 200 open(outputfile, "out"); 202 203 super.init(); 205 206 207 put("which", app_name); 208 put("gen", this); 209 put("declarator", ""); 210 put("limited_types" , new Long (org.objectweb.openccm.ast.api.DeclarationKind.dk_all)); 211 put("imports", imports); 212 } 213 214 222 public void 223 generate(String name) 224 throws GenerationException 225 { 226 Scope target_decl = null; 227 228 try{ 229 target_decl = (Scope)getDeclaration(name); 230 }catch(ClassCastException ex){ 231 String msg = name + "is not a scope!"; 232 throw new GenerationException(msg); 233 } 234 generate(target_decl); 235 } 236 237 244 public void 245 generate(Scope target_decl) 246 throws GenerationException 247 { 248 target_decl_ = target_decl; 249 250 put("obj", target_decl); 252 put("FILE_MACRO", get_macro(target_decl)); 253 map("FILE_HEADER"); 254 visit(target_decl); 255 after_generation(); 256 } 257 258 267 public void 268 generate(Scope target_decl, long limited_types) 269 throws GenerationException 270 { 271 List vect = null; 272 273 vect = getDeclarations(target_decl, limited_types); 274 put("limited_types" , new Long (limited_types)); 275 put("FILE_MACRO", get_macro(target_decl)); 276 map("FILE_HEADER"); 277 forward(vect); 278 map("END_LINE"); 279 visit(vect); 280 after_generation(); 281 } 282 283 288 public void 289 close(String id) 290 { 291 if ( print_macro_ ) 292 print("\n#endif", id); 293 super.close(id); 294 } 295 296 302 309 public String 310 checkName(String id) 311 { 312 return translator_.checkKeywords(id); 313 } 314 315 323 public String 324 getAbsoluteName(Declaration decl) 325 { 326 return translator_.getAbsoluteName(decl); 327 } 328 329 334 public long 335 forward_check() 336 { 337 return (DeclarationKind.dk_interface + 338 DeclarationKind.dk_local_interface + 339 DeclarationKind.dk_abstract_interface + 340 DeclarationKind.dk_component + 341 DeclarationKind.dk_value + 342 DeclarationKind.dk_struct + 343 DeclarationKind.dk_union + 344 DeclarationKind.dk_event); 345 } 346 347 352 public long 353 interface_check() 354 { 355 return (DeclarationKind.dk_attribute + 356 DeclarationKind.dk_constant + 357 DeclarationKind.dk_exception + 358 DeclarationKind.dk_operation + 359 DeclarationKind.dk_alias + 360 DeclarationKind.dk_struct + 361 DeclarationKind.dk_union + 362 DeclarationKind.dk_enum + 363 DeclarationKind.dk_native); 364 } 365 366 371 public long 372 component_check() 373 { 374 return (DeclarationKind.dk_provides + 375 DeclarationKind.dk_uses + 376 DeclarationKind.dk_emits + 377 DeclarationKind.dk_publishes + 378 DeclarationKind.dk_consumes + 379 DeclarationKind.dk_attribute); 380 } 381 382 387 public long 388 home_check() 389 { 390 return (DeclarationKind.dk_factory + 391 DeclarationKind.dk_finder + 392 interface_check()); 393 } 394 395 400 public long 401 value_check() 402 { 403 return (DeclarationKind.dk_value_member + 404 interface_check()); 405 } 406 407 414 public long 415 getDeclarationKind(String att) 416 { 417 java.lang.reflect.Field field = null; 418 long res = 0; 419 420 try{ 421 field = Class.forName("org.objectweb.openccm.ast.api.DeclarationKind").getField(att); 422 res = field.getLong(field); 423 }catch(Exception e){ 424 e.printStackTrace(); 425 } 426 427 return res; 428 } 429 430 436 439 public String 440 macro(Declaration obj) 441 { 442 String res = ""; 443 String version = obj.getVersion(); 444 String id = obj.getId(); 445 String prefix = obj.getPrefix(); 446 447 if (!prefix.equals(last_prefix_)) 449 { 450 res = res + "#pragma prefix \""+prefix+"\"\n"; 451 last_prefix_ = prefix; 452 } 453 else 454 { 455 String p_id = obj.getParent().getId(); 456 int idx1 = p_id.indexOf(':'); 457 int idx2 = p_id.lastIndexOf(':'); 458 if (id.indexOf(p_id.substring(idx1, idx2))==-1) 459 res = res + "#pragma id "+obj.getName()+" \""+id+"\"\n"; 460 } 461 if (!version.equals("1.0")) 462 res = res + "#pragma version "+obj.getName()+" "+version+"\n"; 463 464 return res; 465 } 466 467 473 public void 474 struct_members(Declaration obj, String obj_declarator) 475 { 476 boolean is_inner = false; 477 int multiple_idx = -1; 478 WithNameTypeRef[] members = new WithNameTypeRef[0]; 479 480 try 482 { 483 members = ((ExceptionDecl) obj).getMemberList().getStructMembers(); 484 }catch(ClassCastException e){ 485 try{ 486 members = ((StructDecl) obj).getMemberList().getStructMembers(); 487 }catch(ClassCastException ex){ 488 members = ((UnionDecl) obj).getMemberList().getUnionMembers(); 489 } 490 } 491 493 for (int i=0; i<members.length; i++) 495 { 496 String name = checkName( members[i].getName() ); 497 TypeRef type = members[i].getType(); 498 499 if ((type.getTypeKind() == org.objectweb.openccm.ast.api.TypeKind.tk_struct) || 501 (type.getTypeKind() == org.objectweb.openccm.ast.api.TypeKind.tk_union) || 502 (type.getTypeKind() == org.objectweb.openccm.ast.api.TypeKind.tk_enum)) 503 { 504 Declaration decl = null; 505 decl = (Declaration)type; 506 if (decl.getParent().getId().equals(obj.getId())) 507 is_inner = true; 508 else 509 is_inner = false; 510 } 511 else 512 is_inner = false; 513 514 515 String declarator = ""; 517 if ( (i<members.length-1) && (type==members[i+1].getType()) ) 520 multiple_idx = i; 521 while ( (i<members.length-1) && (type==members[i+1].getType()) ) 522 i++; 523 524 if (multiple_idx==-1) 525 declarator = name + translator_.toIDLPostfix(type); 526 else 527 { 528 for (int j=multiple_idx; j<i+1; j++) 529 declarator = declarator + checkName(members[j].getName()) 530 + translator_.toIDLPostfix(members[j].getType()) + ", "; 531 declarator = declarator.substring(0, declarator.length()-2); 532 multiple_idx = -1; 533 } 534 535 if (is_inner) 536 { 537 Declaration decl = null; 539 String id = ""; 540 541 decl = (Declaration)type; 542 id = org.objectweb.openccm.ast.lib.DeclarationKindImpl.toString(decl.getDeclKind()).toUpperCase()+ "_INLINED"; 543 544 put("obj", decl); 545 put("declarator", declarator); 546 map(id); 547 } 548 else 549 { 550 put("type_spec", translator_.toIDL(type) ); 551 put("name", declarator); 552 map("STRUCT_MEMBER"); 553 } 554 } 556 put("declarator",obj_declarator); 557 put("obj", obj); 558 } 559 560 561 566 public void 567 union_members(UnionDecl union, String obj_declarator) 568 { 569 UnionMember[] members = null; 570 boolean is_default = false; 571 boolean print = true; 572 boolean is_inner = false; 573 int di = union.getDefaultIndex(); 574 575 members = union.getMemberList().getUnionMembers(); 576 577 for (int i=0; i<members.length; i++) 580 { 581 String name = checkName( members[i].getName() ); 582 TypeRef type = members[i].getType(); 583 AnyValue val = members[i].getAnyValue(); 584 585 if ( ( i<members.length-1 ) && 588 ( name.equals( checkName(members[i+1].getName()) ) ) 589 ) 590 print = false; 591 else 592 print = true; 593 594 if (translator_.toString(val, union.getDiscriminator()).equals("")) 597 is_default = true; 598 else 599 is_default = false; 600 602 if ( (type.getTypeKind() == org.objectweb.openccm.ast.api.TypeKind.tk_struct) || 604 (type.getTypeKind() == org.objectweb.openccm.ast.api.TypeKind.tk_union) || 605 (type.getTypeKind() == org.objectweb.openccm.ast.api.TypeKind.tk_enum)) 606 { 607 Declaration decl = null; 608 decl = (Declaration)type; 609 if (decl.getParent().getId().equals(union.getId())) 610 is_inner = true; 611 else 612 is_inner = false; 613 } 614 else 615 is_inner = false; 616 617 if (!print) 618 { 619 if (is_default) 621 print("default :\n"); 622 else 623 { 624 String value = translator_.toString(val, union.getDiscriminator()); 625 print("case " + value + " :\n"); 626 } 627 } 628 else if (is_inner) 629 { 630 if (is_default) 632 print("default :\n"); 633 else 634 { 635 String value = translator_.toString(val, union.getDiscriminator()); 636 print("case " + value + " :\n"); 637 } 638 639 Declaration decl = null; 641 String id = ""; 642 643 decl = (Declaration)type; 644 id = org.objectweb.openccm.ast.lib.DeclarationKindImpl.toString(decl.getDeclKind()).toUpperCase()+ 645 "_INLINED"; 646 647 put("obj", decl); 648 put("declarator", name+translator_.toIDLPostfix(type)); 649 map(id); 652 } 654 else 655 { 656 put("value", translator_.toString(val, union.getDiscriminator())); 657 put("type_spec",translator_.toIDL(type)); 658 put("name", name+translator_.toIDLPostfix(type)); 659 if (is_default) 660 map("UNION_DEFAULT_MEMBER"); 661 else 662 map("UNION_MEMBER"); 663 } 664 } put("declarator",obj_declarator); 666 put("obj", union); 667 } 668 669 670 675 public void 676 enum_members(EnumDecl enumDecl) 677 { 678 String [] members = enumDecl.getMemberList().getStrings(); 679 String res = ""; 680 int i; 681 682 for (i=0; i<members.length-1; i++) 683 print( members[i] + ",\n"); 684 685 if (i<members.length) 686 print( members[i] ); 687 } 688 689 690 695 public void 696 modifier(ValueDecl value) 697 { 698 if (value.isCustom()) 699 print("custom "); 700 else if (value.isAbstract()) 701 print("abstract "); 702 } 703 704 705 712 public String 713 base(ComponentDecl comp) 714 { 715 ComponentDecl inh = comp.getBaseComponent(); 716 if (inh != null) 717 return (" : " + getAbsoluteName(inh)); 718 return ""; 719 } 720 721 728 public String 729 base(HomeDecl home) 730 { 731 HomeDecl inh = home.getBaseHome(); 732 if (inh != null) 733 return (" : "+ getAbsoluteName(inh)); 734 return ""; 735 } 736 737 738 745 public String 746 supports(InterfaceDecl[] itfs) 747 { 748 String res = ""; 749 750 if ( (itfs!=null) && (itfs.length>0) ) 751 { 752 res += "\n supports "; 753 int i; 754 for (i=0; i<itfs.length-1; i++) 755 res += getAbsoluteName(itfs[i]) + ", "; 756 res += getAbsoluteName(itfs[i]); 757 } 758 return res; 759 } 760 761 768 public String 769 supported(ComponentBase comp) 770 { 771 InterfaceList list = null; 772 773 list = comp.getSupportedInterfaceList(); 774 if (list != null) 775 return supports( list.getInterfaces() ); 776 return ""; 777 } 778 779 786 public String 787 supported(ValueDecl value) 788 { 789 InterfaceList list = null; 790 791 list = value.getSupportedInterfaceList(); 792 if (list != null) 793 return supports( list.getInterfaces() ); 794 return ""; 795 } 796 797 804 public String 805 bases(InterfaceDecl itf) 806 { 807 String res = ""; 808 Declaration[] itfs = itf.getInheritedInterfaceList().getInterfaces(); 809 810 if (itfs.length>0) 811 res += "\n : "; 812 813 for (int i=0; i<itfs.length; i++) 814 { 815 res += getAbsoluteName(itfs[i]); 816 if (i<itfs.length-1) 817 res += ",\n " ; 818 } 819 return res; 820 } 821 822 829 public String 830 mode(OperationDecl op) 831 { 832 if (op.isOneway()) 833 return("oneway "); 834 else 835 return ""; 836 } 837 838 845 public String 846 toStringMode(Parameter param) 847 { 848 if ( param.isIn() ) 849 return "in "; 850 else if ( param.isOut() ) 851 return "out "; 852 else 853 return "inout "; 854 } 855 856 863 public String 864 type(TypeRef obj) 865 { 866 return translator_.toIDL(obj); 867 } 868 869 876 public String 877 params(OperationDecl op) 878 { 879 String res = ""; 880 Parameter[] par = op.getParameterList().getParameters(); 881 882 for (int i=0; i<par.length; i++) 883 res += toStringMode(par[i]) + translator_.toIDL(par[i].getType()) 884 + " " + checkName(par[i].getName()) + ", "; 885 886 if (!res.equals("")) 887 res = res.substring(0, res.length()-2); 888 889 return res; 890 } 891 892 899 public String 900 raise(String raises_format, ExceptionList list) 901 { 902 ExceptionDecl[] excs = list.getExceptions(); 903 String res = ""; 904 905 if ( (excs != null) && (excs.length>0)) 906 { 907 int i; 908 909 res += ("\n " + raises_format + "("); 910 for (i=0; i<excs.length-1; i++) 911 res += getAbsoluteName(excs[i]) + ", "; 912 res += getAbsoluteName(excs[i]) + ")"; 913 } 914 return res; 915 } 916 917 924 public String 925 raises(AttributeDecl attr) 926 { 927 return raise("raises", attr.getExceptionList()); 928 } 929 930 937 public String 938 getraises(AttributeDecl attr) 939 { 940 return raise("getraises", attr.getGetExceptionList()); 941 } 942 943 950 public String 951 setraises(AttributeDecl attr) 952 { 953 return raise("setraises", attr.getSetExceptionList()); 954 } 955 956 963 public String 964 access(AttributeDecl attr) 965 { 966 if (attr.isReadonly()) 967 return "readonly "; 968 return ""; 969 } 970 971 978 public String 979 multiple(UsesDecl uses) 980 { 981 if (uses.isMultiple()) 982 return "multiple "; 983 else 984 return ""; 985 } 986 987 994 public String 995 primary_key(HomeDecl home) 996 { 997 Declaration pk = home.getPrimaryKey(); 998 999 if (pk == null) 1000 return ""; 1001 else 1002 return ("\n primary key " + getAbsoluteName(pk)); 1003 } 1004 1005 1012 public String 1013 base_value(ValueDecl value) 1014 { 1015 ValueDecl base = value.getBaseValue(); 1016 if (base!=null) 1017 { 1018 if (value.isTruncatable()) 1019 return " : truncatable " + getAbsoluteName(base); 1020 else 1021 return " : " + getAbsoluteName(base); 1022 } 1023 return ""; 1024 } 1025 1026 1027 1034 public String 1035 abstract_values(ValueDecl value) 1036 { 1037 ValueDecl[] bases = value.getAbstractValueList().getValues(); 1038 String res = ""; 1039 1040 if (bases.length>0) 1041 { 1042 if (value.getBaseValue() == null) 1043 res += " : "; 1044 else 1045 res += ", "; 1046 int i; 1047 for (i=0; i<bases.length-1; i++) 1048 res += getAbsoluteName(bases[i]) + ", "; 1049 res += getAbsoluteName(bases[i]); 1050 } 1051 return res; 1052 } 1053 1054 1061 public String 1062 visibility(ValueMemberDecl vm) 1063 { 1064 if (vm.isPrivate()) 1065 return "private"; 1066 else 1067 return "public"; 1068 } 1069 1070 1077 public String 1078 array(TypeRef obj) 1079 { 1080 return translator_.toIDLPostfix(obj); 1081 } 1082 1083 1090 public String 1091 contexts(OperationDecl op) 1092 { 1093 String [] ctxs = op.getContextList().getStrings(); 1094 String res = ""; 1095 1096 if (ctxs.length > 0) 1097 { 1098 res += " context("; 1099 for (int i=0; i<ctxs.length; i++) 1100 res += "\"" + ctxs[i] + "\", "; 1101 res += ")"; 1102 } 1103 1104 return res; 1105 } 1106 1107 1108 1115 public String 1116 value(ConstantDecl constant) 1117 { 1118 return translator_.toString(constant.getAnyValue(), constant.getType()); 1119 } 1120 1121 1122 1129 public String 1130 initializer_params(Initializer obj) 1131 { 1132 Parameter[] params = null; 1133 params = obj.getParameterList().getParameters(); 1134 String res = ""; 1135 1136 for (int i=0; i<params.length; i++) 1137 { 1138 res += toStringMode(params[i]); 1139 res += translator_.toIDL(params[i].getType()); 1140 res += " " + checkName(params[i].getName()) + ", "; 1141 } 1142 1143 if (!res.equals("")) 1144 res = res.substring(0, res.length()-2); 1145 1146 return res; 1147 } 1148 1149 1150 1155 public void 1156 initializers(ValueDecl value) 1157 { 1158 Initializer[] ops = value.getInitializerList().getInitializers(); 1159 1160 for (int i=0;i<ops.length;i++) 1161 { 1162 put("init", ops[i]); 1163 map("INITIALIZER"); 1164 } 1165 } 1166} 1167 | Popular Tags |