ocarina-analyzer-aadl-finder.adb 43.2 KB
Newer Older
1 2 3 4 5 6 7 8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--         O C A R I N A . A N A L Y Z E R . A A D L . F I N D E R          --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--          Copyright (C) 2009-2011, European Space Agency (ESA).           --
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
--                                                                          --
-- Ocarina  is free software;  you  can  redistribute  it and/or  modify    --
-- it under terms of the GNU General Public License as published by the     --
-- Free Software Foundation; either version 2, or (at your option) any      --
-- later version. Ocarina is distributed  in  the  hope  that it will be    --
-- useful, but WITHOUT ANY WARRANTY;  without even the implied warranty of  --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General --
-- Public License for more details. You should have received  a copy of the --
-- GNU General Public License distributed with Ocarina; see file COPYING.   --
-- If not, write to the Free Software Foundation, 51 Franklin Street, Fifth --
-- Floor, Boston, MA 02111-1301, USA.                                       --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable to be   --
-- covered  by the  GNU  General  Public  License. This exception does not  --
-- however invalidate  any other reasons why the executable file might be   --
-- covered by the GNU Public License.                                       --
--                                                                          --
--                 Ocarina is maintained by the Ocarina team                --
--                       (ocarina-users@listes.enst.fr)                     --
--                                                                          --
------------------------------------------------------------------------------

with Namet;

with Ocarina.Analyzer.AADL.Naming_Rules;
with Ocarina.Analyzer.Messages;

with Ocarina.ME_AADL;
with Ocarina.ME_AADL.AADL_Tree.Entities;
with Ocarina.ME_AADL.AADL_Tree.Nutils;
with Ocarina.ME_AADL.AADL_Tree.Entities.Properties;

with Ocarina.Property_Sets;

package body Ocarina.Analyzer.AADL.Finder is

   use Namet;
   use Ocarina.Analyzer.AADL.Naming_Rules;
   use Ocarina.Analyzer.Messages;
   use Ocarina.ME_AADL;
   use Ocarina.ME_AADL.AADL_Tree.Entities;
   use Ocarina.ME_AADL.AADL_Tree.Nodes;
   use Ocarina.ME_AADL.AADL_Tree.Nutils;
   use Ocarina.ME_AADL.AADL_Tree.Entities.Properties;
   use Ocarina.Property_Sets;

   function Find_AADL_Declaration_Classifier
     (Root                   : Node_Id;
      Package_Identifier     : Node_Id;
      Declaration_Identifier : Node_Id;
      Declaration_Kinds      : Node_Kind_Array)
     return Node_Id;

   function Find_Subclause_Declaration_Classifier
     (Component              : Node_Id;
      Declaration_Identifier : Node_Id;
      Subclause_Kinds        : Node_Kind_Array)
     return Node_Id;

   function Filter_Declarations_According_To_Modes
     (Declaration_Node : Node_Id;
      In_Modes         : Node_Id)
     return Node_Id;
   --  Given a chained list of homonyms 'Declaration_Node', if
   --  In_Modes is not nul, return the node coprresponding to the
   --  declaration that matches these modes or else the declaration
   --  that has no "in modes" clause or else No_Node. If In_Modes is
   --  nul, return the node coprresponding to the declaration with no
   --  "in modes" clause or else No_Node.

   --------------------------------------------
   -- Filter_Declarations_According_To_Modes --
   --------------------------------------------

   function Filter_Declarations_According_To_Modes
     (Declaration_Node : Node_Id;
      In_Modes         : Node_Id)
     return Node_Id
   is
      Pointed_Node       : Node_Id := Declaration_Node;
      Homonym_Node       : Node_Id;
      Homonym_Identifier : Node_Id;
      Success            : Boolean;
      Was_First_Homonym  : Boolean;
      Required_Mode      : Node_Id;
      Present_Mode       : Node_Id;
      Name_Id_1          : Name_Id;
      Name_Id_2          : Name_Id;
      Name_Id_1b         : Name_Id;
      Name_Id_2b         : Name_Id;
   begin
      --  If In_Modes is (none), return No_Node

      if Present (In_Modes) and then not Have_Modes (In_Modes) then
         return No_Node;
      end if;

      Homonym_Node := Pointed_Node;

      while Present (Homonym_Node) loop
         Success := True;
         Was_First_Homonym := (Homonym_Node = Pointed_Node);

         if Have_Modes (In_Modes) then
            if Present
              (Ocarina.Me_AADL.AADL_Tree.Nodes.In_Modes (Homonym_Node))
              and then
              not Have_Modes
              (Ocarina.Me_AADL.AADL_Tree.Nodes.In_Modes (Homonym_Node))
            then
               --  This means that the declarator has an 'in modes
               --  (none)' clause. This is not good for us.

               Success := False;
            elsif Have_Modes
              (Ocarina.Me_AADL.AADL_Tree.Nodes.In_Modes (Homonym_Node)) then
               --  All the modes of

               Required_Mode := First_Node (Modes (In_Modes));
               Success := False;

               --  For each required mode, we look for it in the
               --  in_modes statement.

               while Present (Required_Mode) loop
                  Present_Mode := First_Node
                    (Modes (Ocarina.Me_AADL.AADL_Tree.Nodes.In_Modes
                              (Homonym_Node)));

                  while Present (Present_Mode) loop
                     if Kind (Present_Mode) = Kind (Required_Mode) then
                        if Kind (Required_Mode) = K_Entity_Reference then
                           Name_Id_1 := Get_Name_Of_Entity_Reference
                             (Present_Mode);
                           Name_Id_2 := Get_Name_Of_Entity_Reference
                             (Required_Mode);
                           Success := (Name_Id_1 = Name_Id_2)
                             or else Success;

                        elsif Kind (Required_Mode) =
                          K_Pair_Of_Entity_References
                          and then (Second_Reference (Required_Mode) /=
                                    No_Node)
                          =  (Second_Reference (Present_Mode) /= No_Node)
                        then
                           if Second_Reference (Required_Mode) = No_Node then
                              Name_Id_1 := Get_Name_Of_Entity_Reference
                                (First_Reference (Present_Mode));
                              Name_Id_2 := Get_Name_Of_Entity_Reference
                                (First_Reference (Required_Mode));
                              Success := (Name_Id_1 = Name_Id_2)
                                or else Success;

                           else
                              Name_Id_1 := Get_Name_Of_Entity_Reference
                                (First_Reference (Present_Mode));
                              Name_Id_2 := Get_Name_Of_Entity_Reference
                                (First_Reference (Required_Mode));
                              Name_Id_1b := Get_Name_Of_Entity_Reference
                                (Second_Reference (Present_Mode));
                              Name_Id_2b := Get_Name_Of_Entity_Reference
                                (Second_Reference (Required_Mode));

                              Success := ((Name_Id_1 = Name_Id_2)
                                          and then (Name_Id_1b = Name_Id_2b))
                                or else Success;
                           end if;
                        end if;
                     end if;

                     exit when Success;
                     Present_Mode := Next_Node (Present_Mode);
                  end loop;

                  exit when not Success;
                  Required_Mode := Next_Node (Required_Mode);
               end loop;
            end if;
         end if;

         Homonym_Identifier := Homonym (Identifier (Homonym_Node));

         if Present (Homonym_Identifier) then
            Homonym_Node := Corresponding_Entity (Homonym_Identifier);
         else
            Homonym_Node := No_Node;
         end if;

         if Was_First_Homonym and then not Success then
            Pointed_Node := Homonym_Node;
         end if;
      end loop;

      return Pointed_Node;
   end Filter_Declarations_According_To_Modes;

   --------------------------------------
   -- Find_AADL_Declaration_Classifier --
   --------------------------------------

   function Find_AADL_Declaration_Classifier
     (Root                   : Node_Id;
      Package_Identifier     : Node_Id;
      Declaration_Identifier : Node_Id;
      Declaration_Kinds      : Node_Kind_Array)
     return Node_Id
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
      pragma Assert (No (Package_Identifier)
                     or else Kind (Package_Identifier) = K_Identifier);
      pragma Assert (Kind (Declaration_Identifier) = K_Identifier);
      pragma Assert (Declaration_Kinds'Length > 0);

      Pack               : Node_Id;
      Pointed_Node       : Node_Id := No_Node;
      Homonym_Node       : Node_Id;
      Homonym_Identifier : Node_Id;
      Success            : Boolean;
      Was_First_Homonym  : Boolean;
   begin
      if Present (Package_Identifier) then
         Pack := Node_In_Scope (Package_Identifier, Entity_Scope (Root));
         Pointed_Node := No_Node;

         --  Node_In_Scope returns a node with all its homonyms. We
         --  have to look for a package in this list. Naming rules
         --  ensure there is at most one package in the list.

         while Present (Pack)
           and then Kind (Pack) /= K_Package_Specification
         loop
            Homonym_Identifier := Homonym (Identifier (Pack));

            if Present (Homonym_Identifier) then
               Pack := Corresponding_Entity (Homonym_Identifier);
            else
               Pack := No_Node;
            end if;
         end loop;

         --  If the package has been found, we look for the declaration

         if Present (Pack) then
            Pointed_Node := Node_In_Scope
              (Declaration_Identifier, Entity_Scope (Pack));

            if Current_Scope /= Entity_Scope (Pack) then
               --  If the search is not done from the local package,
               --  then we must ignore the private declarations

               Homonym_Node := Pointed_Node;

               while Present (Homonym_Node) loop
                  Was_First_Homonym := (Homonym_Node = Pointed_Node);
                  Success := not Is_Private (Homonym_Node);

                  if not Success then
                     Homonym_Identifier := Remove_From_Homonyms
                       (Identifier (Pointed_Node),
                        Identifier (Homonym_Node));
                     --  Beware: Remove_From_Homonyms only handles
                     --  identifiers.
                  else
                     Homonym_Identifier := Homonym (Identifier (Homonym_Node));
                  end if;

                  if Present (Homonym_Identifier) then
                     Homonym_Node := Corresponding_Entity (Homonym_Identifier);
                  else
                     Homonym_Node := No_Node;
                  end if;

                  if Was_First_Homonym and then not Success then
                     Pointed_Node := Homonym_Node;
                  end if;
               end loop;
            end if;
         end if;
      else
         Pointed_Node := Node_In_Scope (Declaration_Identifier, Current_Scope);
         --  Current_Scope is supposed to be the one of the package
      end if;

      --  We then filter out the node kinds we do not seek

      Homonym_Node := Pointed_Node;

      while Present (Homonym_Node) loop
         Success := False;
         Was_First_Homonym := (Homonym_Node = Pointed_Node);

         for K in Declaration_Kinds'Range loop
            Success := (Kind (Pointed_Node) = Declaration_Kinds (K))
              or else Success;
         end loop;

         if not Success then
            Homonym_Identifier := Remove_From_Homonyms
              (Identifier (Pointed_Node),
               Identifier (Homonym_Node));
            --  Beware: Remove_From_Homonyms only handles identifiers.
         else
            Homonym_Identifier := Homonym (Identifier (Homonym_Node));
         end if;

         if Present (Homonym_Identifier) then
            Homonym_Node := Corresponding_Entity (Homonym_Identifier);
         else
            Homonym_Node := No_Node;
         end if;

         if Was_First_Homonym and then not Success then
            Pointed_Node := Homonym_Node;
         end if;
      end loop;

      return Pointed_Node;
   end Find_AADL_Declaration_Classifier;

   ------------------------------
   -- Find_All_Component_Types --
   ------------------------------

   function Find_All_Component_Types
     (Root      : Node_Id;
      Namespace : Node_Id := No_Node)
     return Node_List
   is
   begin
      return Find_All_Declarations (Root, (1 => K_Component_Type), Namespace);
   end Find_All_Component_Types;

   ---------------------------
   -- Find_All_Declarations --
   ---------------------------

   function Find_All_Declarations
     (Root      : Node_Id;
      Kinds     : Node_Kind_Array;
      Namespace : Node_Id := No_Node)
     return Node_List
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
      pragma Assert (No (Namespace)
                     or else Kind (Namespace) = K_AADL_Specification
                     or else Kind (Namespace) = K_Package_Specification);

      EL        : Node_List;
      List_Node : Node_Id;
   begin
      if No (Namespace) then
         Select_Nodes (Ocarina.Me_AADL.AADL_Tree.Nodes.Declarations (Root),
                       Kinds,
                       EL.First,
                       EL.Last);

         --  We first get the declarations of the unnamed namespace

         if not Is_Empty
           (Ocarina.Me_AADL.AADL_Tree.Nodes.Declarations (Root)) then
            List_Node := Ocarina.Me_AADL.AADL_Tree.Nodes.First_Node
              (Ocarina.Me_AADL.AADL_Tree.Nodes.Declarations (Root));

            while Present (List_Node) loop
               if Kind (List_Node) = K_Package_Specification then
                  --  Then those of the packages

                  Select_Nodes
                    (Ocarina.Me_AADL.AADL_Tree.Nodes.Declarations (List_Node),
                     Kinds,
                     EL.First,
                     EL.Last);
               end if;

               List_Node := Next_Node (List_Node);
            end loop;
         end if;
      else
         Select_Nodes (Ocarina.Me_AADL.AADL_Tree.Nodes.Declarations
                         (Namespace),
                       Kinds,
                       EL.First,
                       EL.Last);
      end if;

      return EL;
   end Find_All_Declarations;

   -----------------------
   -- Find_All_Features --
   -----------------------

   function Find_All_Features
     (AADL_Declaration : Node_Id)
     return Node_List
   is
   begin
      return Find_All_Subclauses
        (AADL_Declaration,
         (K_Port_Spec,
          K_Parameter,
          K_Feature_Group_Spec,
          K_Subcomponent_Access));
   end Find_All_Features;

   ------------------------------------
   -- Find_All_Property_Associations --
   ------------------------------------

   function Find_All_Property_Associations
     (AADL_Declaration : Node_Id)
     return Node_List
   is
   begin
      return Find_All_Subclauses
        (AADL_Declaration,
         (1 => K_Property_Association));
   end Find_All_Property_Associations;

   -------------------------------
   -- Find_Property_Enumeration --
   -------------------------------

   function Find_Property_Enumeration
     (Root               : Node_Id;
      Container          : Node_Id;
      Property_Container : Node_Id;
      Default_Value      : Node_Id;
      Designator         : Node_Id)
     return Node_Id
   is
      pragma Assert (Present (Root));
      pragma Assert (Present (Container));
      pragma Assert
        (Kind (Property_Container) = K_Property_Association or else
         Kind (Property_Container) = K_Constant_Property_Declaration or else
         Kind (Property_Container) = K_Property_Type or else
         Kind (Property_Container) = K_Property_Definition_Declaration or else
         Kind (Property_Container) = K_Property_Type_Declaration);

      List_Node     : Node_Id  := No_Node;
      Property_Type : Node_Id;
      Pointed_Node  : Node_Id;

   begin
      case Kind (Designator) is
         when K_Unique_Property_Type_Identifier =>

            Pointed_Node := Find_Property_Entity
              (Root,
               Identifier
               (Corresponding_Entity
                (Scope_Entity
                 (Identifier
                  (Entity
                   (Designator))))),
               Identifier (Designator));

            if Present (Pointed_Node) then
               if Kind (Pointed_Node) = K_Property_Type_Declaration then
                  Property_Type := Property_Type_Designator (Pointed_Node);
               elsif  Kind (Pointed_Node) =
                 K_Property_Definition_Declaration
               then
                  Property_Type := Property_Name_Type (Pointed_Node);
               end if;

               if Kind (Property_Type) /= K_Enumeration_Type then
                  return No_Node;
               elsif not Is_Empty (Identifiers (Property_Type)) then
                  List_Node := First_Node (Identifiers (Property_Type));
               end if;

               while Present (List_Node) loop
                  if Ocarina.Me_AADL.AADL_Tree.Nodes.Name (List_Node) =
                    Name (Identifier (Default_Value))
                  then
                     Resolve_Term_In_Property (Property_Container,
                                               Default_Value,
                                               K_Enumeration_Term);
                     return Pointed_Node;
                  end if;

                  List_Node := Next_Node (List_Node);
               end loop;
            end if;

         when K_Enumeration_Type =>
            if not Is_Empty (Identifiers (Designator)) then
               List_Node := First_Node (Identifiers (Designator));

               while Present (List_Node) loop
                  if Ocarina.Me_AADL.AADL_Tree.Nodes.Name (List_Node) =
                    Name (Identifier (Default_Value))
                  then
                     Resolve_Term_In_Property (Property_Container,
                                               Default_Value,
                                               K_Enumeration_Term);

                     return Property_Container;
                  end if;

                  List_Node := Next_Node (List_Node);
               end loop;
            end if;

         when others =>
            return No_Node;
      end case;

      return No_Node;

   end Find_Property_Enumeration;

   -------------------------------------------------------
   -- Find_All_Subclause_Declarations_Except_Properties --
   -------------------------------------------------------

   function Find_All_Subclause_Declarations_Except_Properties
     (AADL_Declaration : Node_Id)
     return Node_List
   is
   begin
      return Find_All_Subclauses
        (AADL_Declaration,
         (K_Annex_Subclause,
          K_Port_Spec,
          K_Parameter,
          K_Feature_Group_Spec,
          K_Subcomponent_Access,
          K_Flow_Spec,
          K_Flow_Implementation,
          K_End_To_End_Flow_Spec,
          K_Flow_Implementation_Refinement,
          K_End_To_End_Flow_Refinement,
          K_Mode,
          K_Connection,
          K_Subprogram_Call,
          K_Subprogram_Call_Sequence));
   end Find_All_Subclause_Declarations_Except_Properties;

   -------------------------
   -- Find_All_Subclauses --
   -------------------------

   function Find_All_Subclauses
     (AADL_Declaration : Node_Id;
      Kinds            : Node_Kind_Array)
     return Node_List
   is
      pragma Assert (Kind (AADL_Declaration) = K_Component_Implementation
                     or else Kind (AADL_Declaration) = K_Component_Type
                     or else Kind (AADL_Declaration) = K_Feature_Group_Type);

      EL               : Node_List;
      List_Node        : Node_Id;
      Declaration_Node : Node_Id;
   begin
      case Kind (AADL_Declaration) is
         when K_Component_Type =>
            Declaration_Node := AADL_Declaration;

            while Present (Declaration_Node)
              and then Kind (Declaration_Node) = K_Component_Type
            loop
               Select_Nodes (Annexes (Declaration_Node),
                             Kinds,
                             EL.First,
                             EL.Last);
               Select_Nodes (Features (Declaration_Node),
                             Kinds,
                             EL.First,
                             EL.Last);
               Select_Nodes (Flows (Declaration_Node),
                             Kinds,
                             EL.First,
                             EL.Last);
               Select_Nodes (Ocarina.Me_AADL.AADL_Tree.Nodes.Properties
                               (Declaration_Node),
                             Kinds,
                             EL.First,
                             EL.Last);

               if Present (Parent (Declaration_Node)) then
                  Declaration_Node := Corresponding_Entity
                    (Identifier
                     (Parent
                      (Declaration_Node)));
               else
                  Declaration_Node := No_Node;
               end if;
            end loop;

         when K_Component_Implementation =>

            --  We first scan the corresponding component type and its
            --  parents

            if Component_Type_Identifier (AADL_Declaration) /= No_Node then
               Declaration_Node := Corresponding_Entity
                 (Component_Type_Identifier (AADL_Declaration));

               while Present (Declaration_Node)
                 and then Kind (Declaration_Node) = K_Component_Type
               loop
                  Select_Nodes (Annexes (Declaration_Node),
                                Kinds,
                                EL.First,
                                EL.Last);
                  Select_Nodes (Features (Declaration_Node),
                                Kinds,
                                EL.First,
                                EL.Last);
                  Select_Nodes (Flows (Declaration_Node),
                                Kinds,
                                EL.First,
                                EL.Last);
                  Select_Nodes (Ocarina.Me_AADL.AADL_Tree.Nodes.Properties
                                  (Declaration_Node),
                                Kinds,
                                EL.First,
                                EL.Last);

                  if Present (Parent (Declaration_Node)) then
                     Declaration_Node := Corresponding_Entity
                       (Identifier
                        (Parent
                         (Declaration_Node)));
                  else
                     Declaration_Node := No_Node;
                  end if;
               end loop;
            end if;

            --  Then we scan the component implementation and its
            --  parents

            Declaration_Node := AADL_Declaration;

            while Present (Declaration_Node)
              and then Kind (Declaration_Node) = K_Component_Implementation
            loop
               Select_Nodes (Refines_Type (Declaration_Node),
                             Kinds,
                             EL.First,
                             EL.Last);
               Select_Nodes (Subcomponents (Declaration_Node),
                             Kinds,
                             EL.First,
                             EL.Last);
               Select_Nodes (Calls (Declaration_Node),
                             Kinds,
                             EL.First,
                             EL.Last);

               if not Is_Empty (Calls (Declaration_Node)) then
                  List_Node := Ocarina.Me_AADL.AADL_Tree.Nodes.First_Node
                    (Calls (Declaration_Node));

                  while Present (List_Node) loop
                     Select_Nodes (Subprogram_Calls (List_Node),
                                   Kinds,
                                   EL.First,
                                   EL.Last);
                     List_Node := Next_Node (List_Node);
                  end loop;
               end if;

               Select_Nodes (Annexes (Declaration_Node),
                             Kinds,
                             EL.First,
                             EL.Last);
               Select_Nodes (Connections (Declaration_Node),
                             Kinds,
                             EL.First,
                             EL.Last);
               Select_Nodes (Flows (Declaration_Node),
                             Kinds,
                             EL.First,
                             EL.Last);
               Select_Nodes (Modes (Declaration_Node),
                             Kinds,
                             EL.First,
                             EL.Last);
               Select_Nodes (Ocarina.Me_AADL.AADL_Tree.Nodes.Properties
                               (Declaration_Node),
                             Kinds,
                             EL.First,
                             EL.Last);

               if Present (Parent (Declaration_Node)) then
                  Declaration_Node := Corresponding_Entity
                    (Identifier
                     (Parent
                      (Declaration_Node)));
               else
                  Declaration_Node := No_Node;
               end if;
            end loop;

         when K_Feature_Group_Type =>
            Declaration_Node := AADL_Declaration;

            while Present (Declaration_Node)
              and then Kind (Declaration_Node) = K_Feature_Group_Type
            loop
               Select_Nodes (Features (Declaration_Node),
                             Kinds,
                             EL.First,
                             EL.Last);
               Select_Nodes (Ocarina.Me_AADL.AADL_Tree.Nodes.Properties
                               (Declaration_Node),
                             Kinds,
                             EL.First,
                             EL.Last);

               if Present (Parent (Declaration_Node)) then
                  Declaration_Node := Corresponding_Entity
                    (Identifier
                     (Parent
                      (Declaration_Node)));
               else
                  Declaration_Node := No_Node;
               end if;
            end loop;

         when others =>
            DAE (Node1 => AADL_Declaration,
                 Message1 => " is not an adequate AADL declaration");
            return (No_Node, No_Node);
      end case;

      return EL;
   end Find_All_Subclauses;

   ---------------------------
   -- Find_All_Root_Systems --
   ---------------------------

   function Find_All_Root_Systems (Root : Node_Id) return Node_List is

      pragma Assert (Kind (Root) = K_AADL_Specification);

      System_List       : Node_List;
      Top_Level_Systems : Node_List;
      List_Node         : Node_Id;
      Kept_Node         : Node_Id;
   begin
      System_List := Find_All_Declarations
        (Root, (1 => K_Component_Implementation), No_Node);

      --  First, we only retrieve the component implementations

      List_Node := System_List.First;

      while Present (List_Node) loop
         if Component_Category'Val (Category (List_Node)) = CC_System
           and then Is_Empty (Features
                              (Corresponding_Entity
                               (Component_Type_Identifier (List_Node))))
         then
            --  If the system implementation corresponds to a type
            --  that does not have any feature, we keep it.

            Kept_Node := List_Node;
         else
            Kept_Node := No_Node;
         end if;

         List_Node := Next_Entity (List_Node);

         if Present (Kept_Node) then
            Set_Next_Entity (Kept_Node, Top_Level_Systems.First);

            if Top_Level_Systems.Last = No_Node then
               Top_Level_Systems.Last := Kept_Node;
            end if;

            Top_Level_Systems.First := Kept_Node;
         end if;
      end loop;

      return Top_Level_Systems;
   end Find_All_Root_Systems;

   -------------------------------
   -- Find_Component_Classifier --
   -------------------------------

   function Find_Component_Classifier
     (Root                 : Node_Id;
      Package_Identifier   : Node_Id;
      Component_Identifier : Node_Id)
     return Node_Id
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
      pragma Assert (No (Package_Identifier)
                     or else Kind (Package_Identifier) = K_Identifier);
      pragma Assert (Kind (Component_Identifier) = K_Identifier);
   begin
      return Find_AADL_Declaration_Classifier
        (Root,
         Package_Identifier,
         Component_Identifier,
         (K_Component_Type, K_Component_Implementation));
   end Find_Component_Classifier;

   ---------------------
   -- Find_Connection --
   ---------------------

   function Find_Connection
     (Component             : Node_Id;
      Connection_Identifier : Node_Id;
      In_Modes              : Node_Id := No_Node)
     return Node_Id
   is
      pragma Assert (Kind (Component) = K_Component_Implementation);
      pragma Assert (Kind (Connection_Identifier) = K_Identifier);

      Pointed_Node : Node_Id;
   begin
      Pointed_Node := Find_Subclause_Declaration_Classifier
        (Component,
         Connection_Identifier,
         (1 => K_Connection));

      Pointed_Node := Filter_Declarations_According_To_Modes
        (Pointed_Node,
         In_Modes);

      return Pointed_Node;
   end Find_Connection;

   ------------------
   -- Find_Feature --
   ------------------

   function Find_Feature
     (Component          : Node_Id;
      Feature_Identifier : Node_Id)
     return Node_Id
   is
      pragma Assert (Kind (Component) = K_Component_Implementation
                       or else Kind (Component) = K_Component_Type
                       or else Kind (Component) = K_Subcomponent_Access
                       or else Kind (Component) = K_Feature_Group_Type);
      pragma Assert (Kind (Feature_Identifier) = K_Identifier);

      Pointed_Node : Node_Id;
   begin
      Pointed_Node := Find_Subclause_Declaration_Classifier
        (Component, Feature_Identifier,
         (K_Port_Spec, K_Parameter,
          K_Feature_Group_Spec, K_Subcomponent_Access,
          K_Subprogram_Spec));

      if No (Pointed_Node)
        and then Kind (Component) = K_Feature_Group_Type
        and then Present (Inverse_Of (Component))
      then
         Pointed_Node := Find_Feature
           (Get_Referenced_Entity (Inverse_Of (Component)),
            Feature_Identifier);

         Pointed_Node := Inversed_Entity (Pointed_Node);
      end if;

      return Pointed_Node;
   end Find_Feature;

   --------------------
   -- Find_Flow_Spec --
   --------------------

   function Find_Flow_Spec
     (Component       : Node_Id;
      Flow_Identifier : Node_Id)
     return Node_Id
   is
      pragma Assert (Kind (Component) = K_Component_Implementation
                     or else Kind (Component) = K_Component_Type);
      pragma Assert (Kind (Flow_Identifier) = K_Identifier);
   begin
      return Find_Subclause_Declaration_Classifier
        (Component,
         Flow_Identifier,
         (1 => K_Flow_Spec));
   end Find_Flow_Spec;

   ---------------
   -- Find_Mode --
   ---------------

   function Find_Mode
     (Component       : Node_Id;
      Mode_Identifier : Node_Id)
     return Node_Id
   is
      pragma Assert (Kind (Component) = K_Component_Implementation
                     or else Kind (Component) = K_Component_Type
                     or else Kind (Component) = K_Feature_Group_Type);
      pragma Assert (Kind (Mode_Identifier) = K_Identifier);
   begin
      return Find_Subclause_Declaration_Classifier
        (Component,
         Mode_Identifier,
         (1 => K_Mode));
   end Find_Mode;

   --------------------------------
   -- Find_Port_Group_Classifier --
   --------------------------------

   function Find_Port_Group_Classifier
     (Root                  : Node_Id;
      Package_Identifier    : Node_Id;
      Port_Group_Identifier : Node_Id)
     return Node_Id
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
      pragma Assert (No (Package_Identifier)
                     or else Kind (Package_Identifier) = K_Identifier);
      pragma Assert (Kind (Port_Group_Identifier) = K_Identifier);
   begin
      return Find_AADL_Declaration_Classifier
        (Root,
         Package_Identifier, Port_Group_Identifier,
         (1 => K_Feature_Group_Type));
   end Find_Port_Group_Classifier;

   -------------------------------
   -- Find_Property_Association --
   -------------------------------

   function Find_Property_Association
     (AADL_Declaration          : Node_Id;
      Property_Association_Name : Name_Id)
     return Node_Id
   is
      pragma Assert (Present (AADL_Declaration));

      All_Properties : constant Node_List
        := Find_All_Property_Associations (AADL_Declaration);
      List_Node      : Node_Id;
   begin
      if All_Properties.First /= No_Node then
         List_Node := All_Properties.First;

         while Present (List_Node) loop
            if Name (Identifier (List_Node)) = Property_Association_Name then
               return List_Node;
            end if;

            List_Node := Next_Entity (List_Node);
         end loop;
      end if;

      return No_Node;
   end Find_Property_Association;

   --------------------------
   -- Find_Property_Entity --
   --------------------------

   function Find_Property_Entity
     (Root                    : Node_Id;
      Property_Set_Identifier : Node_Id;
      Property_Identifier     : Node_Id)
     return Node_Id
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
      pragma Assert (No (Property_Set_Identifier)
                     or else Kind (Property_Set_Identifier) = K_Identifier);
      pragma Assert (Kind (Property_Identifier) = K_Identifier);

      Property_Set               : Node_Id;
      Found_Property_Declaration : Node_Id := No_Node;
   begin
991 992 993 994
      --  Per strict conformance to AADL legality rules, all property
      --  entities should be either fully qualified, or part of
      --  pre-declared property sets.

995 996 997 998
      if Present (Property_Set_Identifier) then
         Property_Set := Node_In_Scope
           (Property_Set_Identifier, Entity_Scope (Root));

999 1000
         --  If we found the corresponding property set, then we look
         --  for the property in it.
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012

         if Present (Property_Set) then
            Found_Property_Declaration := Node_In_Scope
              (Property_Identifier, Entity_Scope (Property_Set));
         else
            Found_Property_Declaration := No_Node;
         end if;

      else
         --  If we did not find anything so far, we try the implicit
         --  property sets.

1013 1014 1015
         for S in Standard_Property_Set_Type'Range loop
            Set_Str_To_Name_Buffer (Image (S));
            Property_Set := Node_In_Scope (Name_Find, Entity_Scope (Root));
1016

1017 1018 1019 1020
            if Present (Property_Set) then
               Found_Property_Declaration := Node_In_Scope
                 (Property_Identifier, Entity_Scope (Property_Set));
            end if;
1021

1022 1023
            exit when Present (Found_Property_Declaration);
         end loop;
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
      end if;

      return Found_Property_Declaration;
   end Find_Property_Entity;

   --------------------
   -- Find_Subclause --
   --------------------

   function Find_Subclause
     (Component  : Node_Id;
      Identifier : Node_Id)
     return Node_Id
   is
      pragma Assert (Kind (Component) = K_Component_Implementation
                     or else Kind (Component) = K_Component_Type);
      pragma Assert (Kind (Identifier) = K_Identifier);
   begin
      return Find_Subclause_Declaration_Classifier
        (Component,
         Identifier,
         (K_Flow_Spec,
          K_Flow_Implementation,
          K_Flow_Implementation_Refinement,
          K_End_To_End_Flow_Spec,
          K_End_To_End_Flow_Refinement,
          K_Connection,
          K_Subcomponent,
          K_Port_Spec,
          K_Parameter,
          K_Feature_Group_Spec,
          K_Subcomponent_Access,
          K_Subprogram_Spec,
          K_Mode,
          K_Subprogram_Call));
   end Find_Subclause;

   -------------------------------------------
   -- Find_Subclause_Declaration_Classifier --
   -------------------------------------------

   function Find_Subclause_Declaration_Classifier
     (Component              : Node_Id;
      Declaration_Identifier : Node_Id;
      Subclause_Kinds        : Node_Kind_Array)
     return Node_Id
   is
      pragma Assert (Kind (Component) = K_Component_Implementation
                       or else Kind (Component) = K_Component_Type
                       or else Kind (Component) = K_Feature_Group_Type
                       or else Kind (Component) = K_Subcomponent_Access);
      pragma Assert (Kind (Declaration_Identifier) = K_Identifier);
      pragma Assert (Subclause_Kinds'Length > 0);

      Pointed_Node : Node_Id := No_Node;
   begin
      if Kind (Component) = K_Subcomponent_Access
hugues.jerome's avatar
hugues.jerome committed
1081 1082
        and then Component_Category'Val (Subcomponent_Category
                                           (Component)) = CC_Subprogram
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
      then
         Pointed_Node := Node_In_Scope
           (Declaration_Identifier, Entity_Scope
              (Get_Referenced_Entity (Entity_Ref (Component))));
      else
         Pointed_Node := Node_In_Scope
           (Declaration_Identifier, Entity_Scope (Component));
      end if;

      return Pointed_Node;
   end Find_Subclause_Declaration_Classifier;

   -----------------------
   -- Find_Subcomponent --
   -----------------------

   function Find_Subcomponent
     (Component               : Node_Id;
      Subcomponent_Identifier : Node_Id;
      In_Modes                : Node_Id := No_Node)
     return Node_Id
   is
      pragma Assert (Kind (Component) = K_Component_Implementation);
      pragma Assert (Kind (Subcomponent_Identifier) = K_Identifier);

      Pointed_Node : Node_Id;
   begin
      Pointed_Node := Find_Subclause_Declaration_Classifier
        (Component,
         Subcomponent_Identifier,
         (1 => K_Subcomponent));

      Pointed_Node := Filter_Declarations_According_To_Modes
        (Pointed_Node,
         In_Modes);

      return Pointed_Node;
   end Find_Subcomponent;

   --------------------------
   -- Find_Subprogram_Call --
   --------------------------

   function Find_Subprogram_Call
     (Component       : Node_Id;
      Call_Identifier : Node_Id;
      In_Modes        : Node_Id := No_Node)
     return Node_Id
   is
      pragma Assert (Kind (Component) = K_Component_Implementation);
      pragma Assert (Kind (Call_Identifier) = K_Identifier);

      Pointed_Node : Node_Id;
   begin
      Pointed_Node := Find_Subclause_Declaration_Classifier
        (Component,
         Call_Identifier,
         (1 => K_Subprogram_Call));

      Pointed_Node := Filter_Declarations_According_To_Modes
        (Pointed_Node,
         In_Modes);

      return Pointed_Node;
   end Find_Subprogram_Call;

   --------------------------------
   -- Find_In_Import_Declaration --
   --------------------------------

   function Find_In_Import_Declaration
     (Package_Container : Node_Id;
      Node              : Node_Id)
     return Boolean
   is
      Identifier           : Node_Id  := No_Node;
      Pack_Identifier      : Node_Id;
      Import_Node          : Node_Id;
      List_Node            : Node_Id;
      Name_Visibility_Node : Node_Id;
      Success              : Boolean  := False;
   begin
      if Kind (Node) = K_Identifier then
         Identifier := Node;
      elsif Kind (Node) = K_Entity_Reference then
         Identifier := Namespace_Identifier (Node);
      end if;

      if Present (Package_Container)
        and then not Is_Empty (Declarations (Package_Container))
        and then (Kind (First_Node (Declarations
                     (Package_Container)))) = K_Name_Visibility_Declaration
      then
         Name_Visibility_Node := First_Node (Declarations (Package_Container));

         if not Is_Empty (List_Items (Name_Visibility_Node)) then
            List_Node := First_Node (List_Items (Name_Visibility_Node));
         end if;

         --  XXX fixme : when pack2 'with' pack1, pack1 identifier must
         --  be add to the scope of pack2 ?

         while Present (List_Node) loop
            if Kind (List_Node) = K_Import_Declaration
              and then not Is_Empty (List_Items (List_Node))
            then
               Import_Node := First_Node (List_Items (List_Node));
            end if;

            while Present (Import_Node) loop
               if Kind (Import_Node) = K_Package_Name then
                  Pack_Identifier := Build_Package_Identifier (Import_Node);

                  if Name (Pack_Identifier) = Name (Identifier) then
                     Success := True;
                  end if;
               else
                  if Name (Import_Node) = Name (Identifier) then
                     Success := True;
                  end if;
               end if;

               Import_Node := Next_Node (Import_Node);
            end loop;

            List_Node := Next_Node (List_Node);
         end loop;
      end if;

      return Success;
   end Find_In_Import_Declaration;

   ------------------
   -- Select_Nodes --
   ------------------

   procedure Select_Nodes
     (Decl_List  :        List_Id;
      Kinds      :        Node_Kind_Array;
      First_Node : in out Node_Id;
      Last_Node  : in out Node_Id)
   is
      Success         : Boolean;
      Local_List_Node : Node_Id;
   begin
      if not Is_Empty (Decl_List) then
         Local_List_Node :=
           Ocarina.Me_AADL.AADL_Tree.Nodes.First_Node (Decl_List);

         while Present (Local_List_Node) loop
            Success := False;

            for K in Kinds'Range loop
               Success := Success
                 or else (Kind (Local_List_Node) = Kinds (K));
            end loop;

            if Success then
               if No (First_Node) then
                  First_Node := Local_List_Node;
                  Last_Node := Local_List_Node;
               else
                  Set_Next_Entity (Last_Node, Local_List_Node);
                  Set_Next_Entity (Local_List_Node, No_Node);
                  Last_Node := Local_List_Node;
               end if;
            end if;

            Local_List_Node := Next_Node (Local_List_Node);
         end loop;
      end if;
   end Select_Nodes;

end Ocarina.Analyzer.AADL.Finder;