1 26 27 package org.objectweb.openccm.ast.lib; 28 29 30 import org.objectweb.openccm.ast.api.ASTError; 31 32 33 import org.objectweb.openccm.ast.api.*; 34 35 46 47 abstract public class ScopeImpl 48 extends DeclarationImpl 49 implements org.objectweb.openccm.ast.api.Scope 50 { 51 57 61 protected java.util.List contained_decls_; 62 63 64 protected boolean content_loaded_; 65 66 72 78 protected 79 ScopeImpl(Repository rep, 80 ScopeImpl parent) 81 { 82 super(rep, parent); 84 85 contained_decls_ = new java.util.ArrayList (); 87 content_loaded_ = false; 88 } 89 90 96 101 protected void 102 addDecl(Declaration decl) 103 { 104 Declaration current = null; 106 String name = decl.getName(); 107 for (int i=0;i<contained_decls_.size();i++) 108 { 109 current = (Declaration)contained_decls_.get(i); 110 if (current.getName().equalsIgnoreCase(name)) 111 return; } 113 contained_decls_.add(decl); 115 } 116 117 124 protected boolean 125 containsDecl(Declaration decl) 126 { 127 return ( decl.getAbsoluteName().startsWith(getAbsoluteName()+"::") ); 128 } 129 130 133 protected void 134 removeDecl(String name) 135 { 136 Declaration decl = null; 137 for (int i=0;i<contained_decls_.size();i++) 138 { 139 decl = (Declaration)contained_decls_.get(i); 140 if (decl.getName().equalsIgnoreCase(name)) 141 contained_decls_.remove(i); 142 } 143 } 144 145 148 protected Declaration 149 createDeclaration(int kind, 150 java.lang.String name) 151 { 152 Declaration decl = null; 153 switch (kind) 154 { 155 case org.omg.CORBA.DefinitionKind._dk_Interface : 157 decl = startInterface(name); 158 break; 159 case org.omg.CORBA.DefinitionKind._dk_LocalInterface : 160 decl = startLocalInterface(name); 161 break; 162 case org.omg.CORBA.DefinitionKind._dk_AbstractInterface : 163 decl = startAbstractInterface(name); 164 break; 165 case org.omg.CORBA.DefinitionKind._dk_Module : 166 decl = startModule(name); 167 break; 168 case org.omg.CORBA.DefinitionKind._dk_Struct : 169 decl = startStruct(name); 170 break; 171 case org.omg.CORBA.DefinitionKind._dk_Value : 172 decl = startValue(name); 173 break; 174 case org.omg.CORBA.DefinitionKind._dk_Union : 175 decl = startUnion(name); 176 break; 177 case org.omg.CORBA.DefinitionKind._dk_Exception : 178 decl = startException(name); 179 break; 180 case org.omg.CORBA.DefinitionKind._dk_Constant : 181 decl = startConstant(name); 182 break; 183 case org.omg.CORBA.DefinitionKind._dk_Enum : 184 decl = startEnum(name); 185 break; 186 case org.omg.CORBA.DefinitionKind._dk_Alias : 187 decl = startAlias(name); 188 break; 189 case org.omg.CORBA.DefinitionKind._dk_Attribute : 190 decl = startAttribute(name); 191 break; 192 case org.omg.CORBA.DefinitionKind._dk_Operation : 193 decl = startOperation(name); 194 break; 195 case org.omg.CORBA.DefinitionKind._dk_ValueBox : 196 decl = startValueBox(name); 197 break; 198 case org.omg.CORBA.DefinitionKind._dk_Native : 199 decl = startNative(name); 200 break; 201 case org.omg.CORBA.DefinitionKind._dk_ValueMember : 202 decl = startValueMember(name); 203 break; 204 205 case org.omg.CORBA.DefinitionKind._dk_Component : 207 decl = startComponent(name); 208 break; 209 case org.omg.CORBA.DefinitionKind._dk_Home : 210 decl = startHome(name); 211 break; 212 case org.omg.CORBA.DefinitionKind._dk_Factory : 213 decl = startFactory(name); 214 break; 215 case org.omg.CORBA.DefinitionKind._dk_Finder : 216 decl = startFinder(name); 217 break; 218 case org.omg.CORBA.DefinitionKind._dk_Emits : 219 decl = startEmits(name); 220 break; 221 case org.omg.CORBA.DefinitionKind._dk_Publishes : 222 decl = startPublishes(name); 223 break; 224 case org.omg.CORBA.DefinitionKind._dk_Consumes : 225 decl = startConsumes(name); 226 break; 227 case org.omg.CORBA.DefinitionKind._dk_Provides : 228 decl = startProvides(name); 229 break; 230 case org.omg.CORBA.DefinitionKind._dk_Uses : 231 decl = startUses(name); 232 break; 233 case org.omg.CORBA.DefinitionKind._dk_Event : 234 decl = startEvent(name); 235 break; 236 default : 237 throw new ASTError(""); 239 } 240 241 return decl; 242 } 243 244 251 protected Declaration 252 loadContained(org.omg.CORBA.Contained contained) 253 { 254 DeclarationImpl decl = (DeclarationImpl) 255 createDeclaration(contained.def_kind().value(), contained.name()); 256 decl.load(contained); 257 decl.setImported(); 258 return decl; 259 } 260 261 268 protected Declaration 269 loadContainedAsMapping(org.omg.CORBA.Contained contained) 270 { 271 DeclarationImpl decl = (DeclarationImpl) 272 createDeclaration(contained.def_kind().value(), contained.name()); 273 decl.loadAsMapping(contained); 274 return decl; 275 } 276 277 284 protected StructDecl 285 startStruct(String name) 286 { 287 StructDecl decl = new StructDeclImpl(getRepository(), this); 288 decl.setName(name); 289 return decl; 290 } 291 292 299 protected UnionDecl 300 startUnion(String name) 301 { 302 UnionDecl decl = new UnionDeclImpl(getRepository(), this); 303 decl.setName(name); 304 return decl; 305 } 306 307 314 protected InterfaceDecl 315 startInterface(String name) 316 { 317 InterfaceDecl decl = new InterfaceDeclImpl(getRepository(), this); 318 decl.setName(name); 319 return decl; 320 } 321 322 329 protected LocalInterfaceDecl 330 startLocalInterface(String name) 331 { 332 LocalInterfaceDecl decl = new LocalInterfaceDeclImpl( 333 getRepository(), this); 334 decl.setName(name); 335 return decl; 336 } 337 338 345 protected AbstractInterfaceDecl 346 startAbstractInterface(String name) 347 { 348 AbstractInterfaceDecl decl = new AbstractInterfaceDeclImpl( 349 getRepository(), this); 350 decl.setName(name); 351 return decl; 352 } 353 354 361 protected ValueDecl 362 startValue(String name) 363 { 364 ValueDecl decl = new ValueDeclImpl(getRepository(), this); 365 decl.setName(name); 366 return decl; 367 } 368 369 376 protected ModuleDecl 377 startModule(String name) 378 { 379 ModuleDecl module = new ModuleDeclImpl(getRepository(), this); 380 module.setName(name); 381 return module; 382 } 383 384 391 protected ComponentDecl 392 startComponent(String name) 393 { 394 ComponentDecl decl = new ComponentDeclImpl(getRepository(), this); 395 decl.setName(name); 396 return decl; 397 } 398 399 406 protected EventDecl 407 startEvent(String name) 408 { 409 EventDecl decl = new EventDeclImpl(getRepository(), this); 410 decl.setName(name); 411 return decl; 412 } 413 414 422 protected Declaration 423 findInScope(String name) 424 { 425 Declaration decl = null; 426 for (int i=0;i<contained_decls_.size();i++) 427 { 428 decl = (Declaration)contained_decls_.get(i); 429 if (decl.getName().equalsIgnoreCase(name)) 430 return decl; 431 } 432 return null; 433 } 434 435 443 protected Declaration 444 findInIR3(String name) 445 { 446 org.omg.CORBA.Container container = getContainer(); 447 if (container == null) 448 return null; 449 450 org.omg.CORBA.Contained contained = container.lookup(name); 451 if (contained == null) 452 return null; 453 454 return loadContained(contained); 455 } 456 457 462 protected org.omg.CORBA.Container 463 getContainer() 464 { 465 return null; 467 } 468 469 472 protected org.omg.CORBA.ComponentIR.ComponentDef 473 getComponentDef() 474 { 475 return null; 477 } 478 479 482 protected org.omg.CORBA.ExtValueDef 483 getExtValueDef() 484 { 485 return null; 487 } 488 489 492 protected org.omg.CORBA.ComponentIR.HomeDef 493 getHomeDef() 494 { 495 return null; 497 } 498 499 504 protected org.omg.CORBA.ComponentIR.Container 505 getComponentContainer() 506 { 507 return null; 509 } 510 511 524 protected org.omg.CORBA.ExtAttributeDef 525 createExtAttribute(AttributeDecl attribute, 526 org.omg.CORBA.IDLType type, 527 org.omg.CORBA.AttributeMode mode, 528 org.omg.CORBA.ExceptionDef [] get_exceptions, 529 org.omg.CORBA.ExceptionDef [] set_exceptions) 530 { 531 return null; 533 } 534 535 548 protected org.omg.CORBA.OperationDef 549 createOperation(OperationDecl operation, 550 org.omg.CORBA.IDLType type, 551 org.omg.CORBA.OperationMode mode, 552 org.omg.CORBA.ParameterDescription [] params, 553 org.omg.CORBA.ExceptionDef [] exceptions, 554 String [] contexts) 555 { 556 return null; 558 } 559 560 566 572 575 public void 576 destroy() 577 { 578 Declaration decl = null; 579 for (int i=contained_decls_.size()-1;i>=0;i--) 580 { 581 decl = (Declaration)contained_decls_.get(i); 582 decl.destroy(); 583 } 584 585 if (contained_decls_.size()==0) 587 super.destroy(); 588 } 589 590 596 603 public FileScope 604 startFileScope(String filename) 605 { 606 FileScope decl = new FileScopeImpl(getRepository(), 607 this, 608 contained_decls_); 609 decl.setFileName(filename); 610 return decl; 611 } 612 613 620 public ConstantDecl 621 startConstant(String name) 622 { 623 ConstantDecl decl = new ConstantDeclImpl(getRepository(), this); 624 decl.setName(name); 625 return decl; 626 } 627 628 636 public StructDecl 637 declareStruct(String name) 638 { 639 Declaration decl = findInScope(name); 641 642 if ((decl != null) && (decl instanceof StructDecl)) 643 return (StructDecl)decl; 644 645 return startStruct(name); 647 } 648 649 657 public UnionDecl 658 declareUnion(String name) 659 { 660 Declaration decl = findInScope(name); 662 663 if ((decl != null) && (decl instanceof UnionDecl)) 664 return (UnionDecl)decl; 665 666 return startUnion(name); 668 } 669 670 677 public EnumDecl 678 startEnum(String name) 679 { 680 EnumDecl decl = new EnumDeclImpl(getRepository(), this); 681 decl.setName(name); 682 return decl; 683 } 684 685 692 public AliasDecl 693 startAlias(String name) 694 { 695 AliasDecl decl = new AliasDeclImpl(getRepository(), this); 696 decl.setName(name); 697 return decl; 698 } 699 700 708 public ModuleDecl 709 declareModule(String name) 710 { 711 713 Declaration decl = findInScope(name); 715 716 if(decl != null && decl instanceof ModuleDecl) 719 { 720 return (ModuleDecl)decl; 721 } 722 723 ModuleDeclImpl module = (ModuleDeclImpl)startModule(name); 725 726 try 728 { 729 org.omg.CORBA.Contained contained = getContainer().lookup(name); 730 if (contained!=null) 731 module.load(contained); 732 } 733 catch(org.omg.CORBA.SystemException exc) 734 { 735 } 737 738 return module; 739 } 740 741 749 public InterfaceDecl 750 declareInterface(String name) 751 { 752 Declaration decl = findInScope(name); 754 755 if (decl != null) 756 { 757 if (decl.getClass() == InterfaceDeclImpl.class) 759 return (InterfaceDecl)decl; 760 761 if (decl instanceof AbstractInterfaceDeclImpl) 763 throw new ASTError("interface `" + name + 764 "' does not match forward abstract declaration"); 765 766 if (decl instanceof LocalInterfaceDeclImpl) 768 throw new ASTError("interface `" + name + 769 "' does not match forward local declaration"); 770 } 771 772 return startInterface(name); 774 } 775 776 784 public LocalInterfaceDecl 785 declareLocalInterface(String name) 786 { 787 Declaration decl = findInScope(name); 789 790 if (decl != null) 791 { 792 if (decl instanceof LocalInterfaceDeclImpl) 794 return (LocalInterfaceDecl)decl; 795 796 if (decl instanceof AbstractInterfaceDeclImpl) 798 throw new ASTError("interface `" + name + 799 "' does not match forward abstract declaration"); 800 801 if (decl instanceof InterfaceDeclImpl) 803 throw new ASTError("interface `" + name + 804 "' does not match forward declaration"); 805 } 806 807 return startLocalInterface(name); 809 } 810 811 819 public AbstractInterfaceDecl 820 declareAbstractInterface(String name) 821 { 822 Declaration decl = findInScope(name); 824 825 if (decl != null) 826 { 827 if (decl instanceof AbstractInterfaceDeclImpl) 829 return (AbstractInterfaceDecl)decl; 830 831 if (decl instanceof LocalInterfaceDeclImpl) 833 throw new ASTError("interface `" + name + 834 "' does not match forward local declaration"); 835 836 if (decl instanceof InterfaceDeclImpl) 838 throw new ASTError("interface `" + name + 839 "' does not match forward declaration"); 840 } 841 842 return startAbstractInterface(name); 844 } 845 846 854 public ValueDecl 855 declareValue(String name) 856 { 857 Declaration decl = findInScope(name); 859 860 if ((decl != null) && (decl instanceof ValueDecl)) 861 return (ValueDecl)decl; 862 863 return startValue(name); 865 } 866 867 874 public Initializer 875 startInitializer(String name) 876 { 877 Initializer init = new InitializerImpl(); 878 init.setName(name); 879 return init; 880 } 881 882 889 public ValueMemberDecl 890 startValueMember(String name) 891 { 892 ValueMemberDecl decl = new ValueMemberDeclImpl(getRepository(), this); 893 decl.setName(name); 894 return decl; 895 } 896 897 904 public ValueBoxDecl 905 startValueBox(String name) 906 { 907 ValueBoxDecl decl = new ValueBoxDeclImpl(getRepository(), this); 908 decl.setName(name); 909 return decl; 910 } 911 912 919 public ExceptionDecl 920 startException(String name) 921 { 922 ExceptionDecl decl = new ExceptionDeclImpl(getRepository(), this); 923 decl.setName(name); 924 return decl; 925 } 926 927 934 public NativeDecl 935 startNative(String name) 936 { 937 NativeDecl decl = new NativeDeclImpl(getRepository(), this); 938 decl.setName(name); 939 return decl; 940 } 941 942 949 public AttributeDecl 950 startAttribute(String name) 951 { 952 AttributeDecl decl = new AttributeDeclImpl(getRepository(), this); 953 decl.setName(name); 954 return decl; 955 } 956 957 964 public OperationDecl 965 startOperation(String name) 966 { 967 OperationDecl decl = new OperationDeclImpl(getRepository(), this); 968 decl.setName(name); 969 return decl; 970 } 971 972 980 public EventDecl 981 declareEvent(String name) 982 { 983 Declaration decl = findInScope(name); 985 986 if ((decl != null) && (decl instanceof EventDecl)) 987 return (EventDecl)decl; 988 989 return startEvent(name); 991 } 992 993 1001 public ComponentDecl 1002 declareComponent(String name) 1003 { 1004 Declaration decl = findInScope(name); 1006 1007 if ((decl != null) && (decl instanceof ComponentDecl)) 1008 return (ComponentDecl)decl; 1009 1010 return startComponent(name); 1012 } 1013 1014 1021 public ProvidesDecl 1022 startProvides(String name) 1023 { 1024 ProvidesDecl decl = new ProvidesDeclImpl(getRepository(), this); 1025 decl.setName(name); 1026 return decl; 1027 } 1028 1029 1036 public UsesDecl 1037 startUses(String name) 1038 { 1039 UsesDecl decl = new UsesDeclImpl(getRepository(), this); 1040 decl.setName(name); 1041 return decl; 1042 } 1043 1044 1051 public ConsumesDecl 1052 startConsumes(String name) 1053 { 1054 ConsumesDecl decl = new ConsumesDeclImpl(getRepository(), this); 1055 decl.setName(name); 1056 return decl; 1057 } 1058 1059 1066 public EmitsDecl 1067 startEmits(String name) 1068 { 1069 EmitsDecl decl = new EmitsDeclImpl(getRepository(), this); 1070 decl.setName(name); 1071 return decl; 1072 } 1073 1074 1081 public PublishesDecl 1082 startPublishes(String name) 1083 { 1084 PublishesDecl decl = new PublishesDeclImpl(getRepository(), this); 1085 decl.setName(name); 1086 return decl; 1087 } 1088 1089 1096 public HomeDecl 1097 startHome(String name) 1098 { 1099 HomeDecl decl = new HomeDeclImpl(getRepository(), this); 1100 decl.setName(name); 1101 return decl; 1102 } 1103 1104 1111 public FactoryDecl 1112 startFactory(String name) 1113 { 1114 FactoryDecl decl = new FactoryDeclImpl(getRepository(), this); 1115 decl.setName(name); 1116 return decl; 1117 } 1118 1119 1126 public FinderDecl 1127 startFinder(String name) 1128 { 1129 FinderDecl decl = new FinderDeclImpl(getRepository(), this); 1130 decl.setName(name); 1131 return decl; 1132 } 1133 1134 1142 public PsdlModuleDecl 1143 declarePsdlModule(String name) 1144 { 1145 return (PsdlModuleDecl)declareModule(name); 1147 } 1148 1149 1157 public AbstractStorageHomeDecl 1158 declareAbstractStorageHome(String name) 1159 { 1160 Declaration decl = findInScope(name); 1162 1163 if ((decl != null) && (decl instanceof AbstractStorageHomeDecl)) 1164 return (AbstractStorageHomeDecl)decl; 1165 1166 AbstractStorageHomeDecl ash = new AbstractStorageHomeDeclImpl(getRepository(), this); 1168 ash.setName(name); 1169 return ash; 1170 } 1171 1172 1179 public StorageHomeDecl 1180 startStorageHome(String name) 1181 { 1182 Declaration decl = findInScope(name); 1184 1185 if ((decl != null) && (decl instanceof StorageHomeDecl)) 1186 return (StorageHomeDecl)decl; 1187 1188 StorageHomeDecl sh = new StorageHomeDeclImpl(getRepository(), this); 1189 sh.setName(name); 1190 return sh; 1191 } 1192 1193 1201 public AbstractStorageTypeDecl 1202 declareAbstractStorageType(String name) 1203 { 1204 Declaration decl = findInScope(name); 1206 1207 if ((decl != null) && (decl instanceof AbstractStorageTypeDecl)) 1208 return (AbstractStorageTypeDecl)decl; 1209 1210 AbstractStorageTypeDecl ast = new AbstractStorageTypeDeclImpl(getRepository(), this); 1212 ast.setName(name); 1213 return ast; 1214 } 1215 1216 1224 public StorageTypeDecl 1225 declareStorageType(String name) 1226 { 1227 Declaration decl = findInScope(name); 1229 1230 if ((decl != null) && (decl instanceof StorageTypeDecl)) 1231 return (StorageTypeDecl)decl; 1232 1233 StorageTypeDecl st = new StorageTypeDeclImpl(getRepository(), this); 1235 st.setName(name); 1236 return st; 1237 } 1238 1239 1246 public StorageTypeStateMemberDecl 1247 startStorageTypeStateMember(String name) 1248 { 1249 StorageTypeStateMemberDecl decl = new StorageTypeStateMemberDeclImpl(getRepository(), this); 1250 decl.setName(name); 1251 return decl; 1252 } 1253 1254 1261 public StorageTypeStoreDirectiveDecl 1262 startStorageTypeStoreDirective(String name) 1263 { 1264 StorageTypeStoreDirectiveDecl decl = new StorageTypeStoreDirectiveDeclImpl(getRepository(), this); 1265 decl.setName(name+"_store_directive"); 1266 return decl; 1267 } 1268 1269 1276 public PsdlOperationDecl 1277 startPsdlOperation(String name) 1278 { 1279 PsdlOperationDecl decl = new PsdlOperationDeclImpl(getRepository(), this); 1280 decl.setName(name); 1281 return decl; 1282 } 1283 1284 1292 public CidlModuleDecl 1293 declareCidlModule(String name) 1294 { 1295 return (CidlModuleDecl)declareModule(name); 1297 } 1298 1299 1306 public CompositionDecl 1307 startComposition(String name) 1308 { 1309 CompositionDecl decl = new CompositionDeclImpl(getRepository(), this); 1310 decl.setName(name); 1311 return decl; 1312 } 1313 1314 1326 public Declaration 1327 find(String name) 1328 { 1329 1331 Declaration decl = findInScope(name); 1332 if (decl != null) return decl; 1334 1335 if (the_parent_ != null) 1337 decl = the_parent_.find(name); 1338 1339 if (decl != null) return decl; 1341 1342 return findInIR3(name); 1344 } 1345 1346 1355 public Declaration[] 1356 getContents(boolean exclude_inherited, 1357 long limited_types) 1358 { 1359 if (!content_loaded_) 1361 { 1362 if (is_mapping_) 1365 getRepository().useIDL2Repository(); 1366 1367 org.omg.CORBA.Container container = getContainer(); 1368 if(container == null) 1369 return null; 1370 org.omg.CORBA.Contained [] content = container.contents(org.omg.CORBA.DefinitionKind.dk_all, true); 1371 1372 java.util.List ordered_decls = new java.util.ArrayList (); 1376 Declaration decl = null; 1377 1378 for (int i=0; i<content.length; i++) 1379 { 1380 decl = findInScope(content[i].name()); 1382 if (decl==null) 1383 { 1384 if (is_mapping_) 1385 decl = loadContainedAsMapping(content[i]); 1386 else 1387 decl = loadContained(content[i]); 1388 } 1390 try{ 1392 org.objectweb.openccm.ast.api.ConstantDecl c = (org.objectweb.openccm.ast.api.ConstantDecl)decl; 1394 decl = loadContained(content[i]); 1397 }catch(ClassCastException ex){ 1398 } 1400 ordered_decls.add(decl); 1402 } 1403 1404 for(int i=0; i<contained_decls_.size(); i++) 1406 { 1407 decl = (Declaration)contained_decls_.get(i); 1408 if ( (decl.getCategory() == org.objectweb.openccm.ast.api.DeclarationCategory.dc_psdl) 1409 || (decl.getCategory() == org.objectweb.openccm.ast.api.DeclarationCategory.dc_cidl) ) 1410 ordered_decls.add(decl); 1411 } 1412 1413 contained_decls_ = ordered_decls; 1414 1415 if (is_mapping_) 1417 getRepository().useIDL3Repository(); 1418 1419 content_loaded_ = true; 1420 } 1421 1422 Declaration decl = null; 1423 java.util.List selection = new java.util.ArrayList (); 1424 for (int i=0; i<contained_decls_.size(); i++) 1425 { 1426 decl = (Declaration)contained_decls_.get(i); 1427 if ((decl.getDeclKind()&limited_types)!=0) 1429 selection.add(decl); 1430 } 1431 return (Declaration[])selection.toArray(new Declaration[0]); 1433 } 1434 1435 1443 public Declaration 1444 lookup(String scoped_name) 1445 { 1446 if (scoped_name.startsWith("::")) return the_repository_.lookup(scoped_name); 1447 1448 int idx = scoped_name.indexOf("::"); 1449 String s1 = null; 1450 String s2 = null; 1451 if (idx==-1) 1452 { 1453 s1 = scoped_name; 1454 s2 = ""; 1455 } 1456 else 1457 { 1458 s1 = scoped_name.substring(0,idx); 1459 s2 = scoped_name.substring(idx+2); 1460 } 1461 1462 Declaration decl = findInScope(s1); 1463 if (decl==null) 1464 { 1465 decl = findInIR3(s1); 1467 } 1468 1469 if (s2.equals("")) 1470 return decl; 1471 1472 if (decl instanceof Scope) 1473 return ((Scope)decl).lookup(s2); 1474 1475 return null; 1476 } 1477 1478 1481 public void 1482 print() 1483 { 1484 System.out.println(getAbsoluteName()); 1485 Declaration[] decls = getContents(true, DeclarationKind.dk_all); 1486 for(int i=0; i<decls.length; i++) 1487 { 1488 System.out.print("+--"); 1489 decls[i].print(); 1490 } 1491 } 1492 1493} 1494 | Popular Tags |