ocarina-analyzer-aadl-finder.adb 43.8 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 Telecom ParisTech, 2010-2014 ESA & ISAE.        --
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
--                                                                          --
-- 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.                                       --
--                                                                          --
jhugues's avatar
jhugues committed
29 30
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
31 32 33
--                                                                          --
------------------------------------------------------------------------------

34
with Ocarina.Namet;
35 36 37 38 39 40 41 42 43 44 45 46 47

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

48
   use Ocarina.Namet;
49 50 51 52 53 54 55 56 57 58 59 60 61
   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;
62
      Declaration_Kinds      : Node_Kind_Array) return Node_Id;
63 64 65 66

   function Find_Subclause_Declaration_Classifier
     (Component              : Node_Id;
      Declaration_Identifier : Node_Id;
67
      Subclause_Kinds        : Node_Kind_Array) return Node_Id;
68 69 70

   function Filter_Declarations_According_To_Modes
     (Declaration_Node : Node_Id;
71
      In_Modes         : Node_Id) return Node_Id;
72 73 74 75 76 77 78 79 80 81 82 83 84
   --  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;
85
      In_Modes         : Node_Id) return Node_Id
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
   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
108
         Success           := True;
109 110 111 112
         Was_First_Homonym := (Homonym_Node = Pointed_Node);

         if Have_Modes (In_Modes) then
            if Present
113 114 115
                (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))
116 117 118 119 120 121
            then
               --  This means that the declarator has an 'in modes
               --  (none)' clause. This is not good for us.

               Success := False;
            elsif Have_Modes
122 123
                (Ocarina.ME_AADL.AADL_Tree.Nodes.In_Modes (Homonym_Node))
            then
124 125 126
               --  All the modes of

               Required_Mode := First_Node (Modes (In_Modes));
127
               Success       := False;
128 129 130 131 132

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

               while Present (Required_Mode) loop
133 134 135 136 137
                  Present_Mode :=
                    First_Node
                      (Modes
                         (Ocarina.ME_AADL.AADL_Tree.Nodes.In_Modes
                            (Homonym_Node)));
138 139 140 141

                  while Present (Present_Mode) loop
                     if Kind (Present_Mode) = Kind (Required_Mode) then
                        if Kind (Required_Mode) = K_Entity_Reference then
142 143 144 145 146
                           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;
147 148 149

                        elsif Kind (Required_Mode) =
                          K_Pair_Of_Entity_References
150 151 152
                          and then
                            (Second_Reference (Required_Mode) /= No_Node) =
                            (Second_Reference (Present_Mode) /= No_Node)
153 154
                        then
                           if Second_Reference (Required_Mode) = No_Node then
155 156 157 158 159 160 161 162
                              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;
163 164

                           else
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
                              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))
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
                                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;
220
      Declaration_Kinds      : Node_Kind_Array) return Node_Id
221 222
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
223 224 225
      pragma Assert
        (No (Package_Identifier)
         or else Kind (Package_Identifier) = K_Identifier);
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
      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.

244
         while Present (Pack) and then Kind (Pack) /= K_Package_Specification
245 246 247 248 249 250 251 252 253 254 255 256 257
         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
258 259
            Pointed_Node :=
              Node_In_Scope (Declaration_Identifier, Entity_Scope (Pack));
260 261 262 263 264 265 266 267 268

            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);
269
                  Success           := not Is_Private (Homonym_Node);
270 271

                  if not Success then
272 273 274 275 276 277
                     Homonym_Identifier :=
                       Remove_From_Homonyms
                         (Identifier (Pointed_Node),
                          Identifier (Homonym_Node));
                  --  Beware: Remove_From_Homonyms only handles
                  --  identifiers.
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
                  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
304
         Success           := False;
305 306 307
         Was_First_Homonym := (Homonym_Node = Pointed_Node);

         for K in Declaration_Kinds'Range loop
308 309
            Success :=
              (Kind (Pointed_Node) = Declaration_Kinds (K)) or else Success;
310 311 312
         end loop;

         if not Success then
313 314 315 316 317
            Homonym_Identifier :=
              Remove_From_Homonyms
                (Identifier (Pointed_Node),
                 Identifier (Homonym_Node));
         --  Beware: Remove_From_Homonyms only handles identifiers.
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
         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;
342
      Namespace : Node_Id := No_Node) return Node_List
343 344 345 346 347 348 349 350 351 352 353 354
   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;
355
      Namespace : Node_Id := No_Node) return Node_List
356 357
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
358 359 360 361
      pragma Assert
        (No (Namespace)
         or else Kind (Namespace) = K_AADL_Specification
         or else Kind (Namespace) = K_Package_Specification);
362 363 364 365 366

      EL        : Node_List;
      List_Node : Node_Id;
   begin
      if No (Namespace) then
367 368 369 370 371
         Select_Nodes
           (Ocarina.ME_AADL.AADL_Tree.Nodes.Declarations (Root),
            Kinds,
            EL.First,
            EL.Last);
372 373 374 375

         --  We first get the declarations of the unnamed namespace

         if not Is_Empty
376 377 378 379 380
             (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));
381 382 383 384 385 386

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

                  Select_Nodes
387
                    (Ocarina.ME_AADL.AADL_Tree.Nodes.Declarations (List_Node),
388 389 390 391 392 393 394 395 396
                     Kinds,
                     EL.First,
                     EL.Last);
               end if;

               List_Node := Next_Node (List_Node);
            end loop;
         end if;
      else
397 398 399 400 401
         Select_Nodes
           (Ocarina.ME_AADL.AADL_Tree.Nodes.Declarations (Namespace),
            Kinds,
            EL.First,
            EL.Last);
402 403 404 405 406 407 408 409 410
      end if;

      return EL;
   end Find_All_Declarations;

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

411
   function Find_All_Features (AADL_Declaration : Node_Id) return Node_List is
412 413
   begin
      return Find_All_Subclauses
414 415 416 417 418
          (AADL_Declaration,
           (K_Port_Spec,
            K_Parameter,
            K_Feature_Group_Spec,
            K_Subcomponent_Access));
419 420 421 422 423 424 425
   end Find_All_Features;

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

   function Find_All_Property_Associations
426
     (AADL_Declaration : Node_Id) return Node_List
427 428 429
   is
   begin
      return Find_All_Subclauses
430 431
          (AADL_Declaration,
           (1 => K_Property_Association));
432 433 434 435 436 437 438 439 440 441 442
   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;
443
      Designator         : Node_Id) return Node_Id
444 445 446 447
   is
      pragma Assert (Present (Root));
      pragma Assert (Present (Container));
      pragma Assert
448 449 450 451 452
        (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);
453

454
      List_Node     : Node_Id := No_Node;
455 456 457 458 459 460
      Property_Type : Node_Id;
      Pointed_Node  : Node_Id;

   begin
      case Kind (Designator) is
         when K_Unique_Property_Type_Identifier =>
461 462 463 464 465 466 467
            Pointed_Node :=
              Find_Property_Entity
                (Root,
                 Identifier
                   (Corresponding_Entity
                      (Scope_Entity (Identifier (Entity (Designator))))),
                 Identifier (Designator));
468 469 470 471

            if Present (Pointed_Node) then
               if Kind (Pointed_Node) = K_Property_Type_Declaration then
                  Property_Type := Property_Type_Designator (Pointed_Node);
472
               elsif Kind (Pointed_Node) =
473 474 475 476 477 478 479 480 481 482 483 484
                 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
485
                  if Ocarina.ME_AADL.AADL_Tree.Nodes.Name (List_Node) =
486 487
                    Name (Identifier (Default_Value))
                  then
488 489 490 491
                     Resolve_Term_In_Property
                       (Property_Container,
                        Default_Value,
                        K_Enumeration_Term);
492 493 494 495 496 497 498 499 500 501 502 503
                     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
504
                  if Ocarina.ME_AADL.AADL_Tree.Nodes.Name (List_Node) =
505 506
                    Name (Identifier (Default_Value))
                  then
507 508 509 510
                     Resolve_Term_In_Property
                       (Property_Container,
                        Default_Value,
                        K_Enumeration_Term);
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

                     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
532
     (AADL_Declaration : Node_Id) return Node_List
533 534 535
   is
   begin
      return Find_All_Subclauses
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
          (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));
551 552 553 554 555 556 557 558
   end Find_All_Subclause_Declarations_Except_Properties;

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

   function Find_All_Subclauses
     (AADL_Declaration : Node_Id;
559
      Kinds            : Node_Kind_Array) return Node_List
560
   is
561 562 563 564
      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);
565 566 567 568 569 570 571 572 573 574 575 576

      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
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
               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);
598 599

               if Present (Parent (Declaration_Node)) then
600 601 602
                  Declaration_Node :=
                    Corresponding_Entity
                      (Identifier (Parent (Declaration_Node)));
603 604 605 606 607 608 609 610 611 612 613
               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
614 615 616
               Declaration_Node :=
                 Corresponding_Entity
                   (Component_Type_Identifier (AADL_Declaration));
617 618 619 620

               while Present (Declaration_Node)
                 and then Kind (Declaration_Node) = K_Component_Type
               loop
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
                  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);
642 643

                  if Present (Parent (Declaration_Node)) then
644 645 646
                     Declaration_Node :=
                       Corresponding_Entity
                         (Identifier (Parent (Declaration_Node)));
647 648 649 650 651 652 653 654 655 656 657 658 659 660
                  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
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
               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);
676 677

               if not Is_Empty (Calls (Declaration_Node)) then
678 679 680
                  List_Node :=
                    Ocarina.ME_AADL.AADL_Tree.Nodes.First_Node
                      (Calls (Declaration_Node));
681 682

                  while Present (List_Node) loop
683 684 685 686 687
                     Select_Nodes
                       (Subprogram_Calls (List_Node),
                        Kinds,
                        EL.First,
                        EL.Last);
688 689 690 691
                     List_Node := Next_Node (List_Node);
                  end loop;
               end if;

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
               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);
718 719

               if Present (Parent (Declaration_Node)) then
720 721 722
                  Declaration_Node :=
                    Corresponding_Entity
                      (Identifier (Parent (Declaration_Node)));
723 724 725 726 727 728 729 730 731 732 733
               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
734 735 736 737 738 739 740 741 742 743 744
               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);
745 746

               if Present (Parent (Declaration_Node)) then
747 748 749
                  Declaration_Node :=
                    Corresponding_Entity
                      (Identifier (Parent (Declaration_Node)));
750 751 752 753 754 755
               else
                  Declaration_Node := No_Node;
               end if;
            end loop;

         when others =>
756 757 758
            DAE
              (Node1    => AADL_Declaration,
               Message1 => " is not an adequate AADL declaration");
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
            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
778 779 780 781 782
      System_List :=
        Find_All_Declarations
          (Root,
           (1 => K_Component_Implementation),
           No_Node);
783 784 785 786 787 788 789

      --  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
790 791 792
           and then Is_Empty
             (Features
                (Corresponding_Entity (Component_Type_Identifier (List_Node))))
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
         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;
825
      Component_Identifier : Node_Id) return Node_Id
826 827
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
828 829 830
      pragma Assert
        (No (Package_Identifier)
         or else Kind (Package_Identifier) = K_Identifier);
831
      pragma Assert (Kind (Component_Identifier) = K_Identifier);
832 833 834

      Pointed_Node : Node_Id;

835
   begin
836 837 838 839 840 841 842 843
      Pointed_Node :=
        Find_AADL_Declaration_Classifier
          (Root,
           Package_Identifier,
           Component_Identifier,
           (K_Component_Type,
            K_Component_Implementation,
            K_Alias_Declaration));
844 845 846 847 848 849 850 851 852 853

      --  In case the classifier is an alias, return the renamed entity

      if Present (Pointed_Node)
        and then Kind (Pointed_Node) = K_Alias_Declaration
      then
         Pointed_Node := Renamed_Entity (Pointed_Node);
      end if;

      return Pointed_Node;
854 855 856 857 858 859 860 861 862
   end Find_Component_Classifier;

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

   function Find_Connection
     (Component             : Node_Id;
      Connection_Identifier : Node_Id;
863
      In_Modes              : Node_Id := No_Node) return Node_Id
864 865 866 867 868 869
   is
      pragma Assert (Kind (Component) = K_Component_Implementation);
      pragma Assert (Kind (Connection_Identifier) = K_Identifier);

      Pointed_Node : Node_Id;
   begin
870 871 872 873 874
      Pointed_Node :=
        Find_Subclause_Declaration_Classifier
          (Component,
           Connection_Identifier,
           (1 => K_Connection));
875

876 877
      Pointed_Node :=
        Filter_Declarations_According_To_Modes (Pointed_Node, In_Modes);
878 879 880 881 882 883 884 885 886 887

      return Pointed_Node;
   end Find_Connection;

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

   function Find_Feature
     (Component          : Node_Id;
888
      Feature_Identifier : Node_Id) return Node_Id
889
   is
890 891 892 893 894
      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);
895 896 897 898
      pragma Assert (Kind (Feature_Identifier) = K_Identifier);

      Pointed_Node : Node_Id;
   begin
899 900 901 902 903 904 905 906 907
      Pointed_Node :=
        Find_Subclause_Declaration_Classifier
          (Component,
           Feature_Identifier,
           (K_Port_Spec,
            K_Parameter,
            K_Feature_Group_Spec,
            K_Subcomponent_Access,
            K_Subprogram_Spec));
908 909 910 911 912

      if No (Pointed_Node)
        and then Kind (Component) = K_Feature_Group_Type
        and then Present (Inverse_Of (Component))
      then
913 914 915 916
         Pointed_Node :=
           Find_Feature
             (Get_Referenced_Entity (Inverse_Of (Component)),
              Feature_Identifier);
917 918 919 920 921 922 923 924 925 926 927 928 929

         Pointed_Node := Inversed_Entity (Pointed_Node);
      end if;

      return Pointed_Node;
   end Find_Feature;

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

   function Find_Flow_Spec
     (Component       : Node_Id;
930
      Flow_Identifier : Node_Id) return Node_Id
931
   is
932 933 934
      pragma Assert
        (Kind (Component) = K_Component_Implementation
         or else Kind (Component) = K_Component_Type);
935 936 937
      pragma Assert (Kind (Flow_Identifier) = K_Identifier);
   begin
      return Find_Subclause_Declaration_Classifier
938 939 940
          (Component,
           Flow_Identifier,
           (1 => K_Flow_Spec));
941 942 943 944 945 946 947 948
   end Find_Flow_Spec;

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

   function Find_Mode
     (Component       : Node_Id;
949
      Mode_Identifier : Node_Id) return Node_Id
950
   is
951 952 953 954
      pragma Assert
        (Kind (Component) = K_Component_Implementation
         or else Kind (Component) = K_Component_Type
         or else Kind (Component) = K_Feature_Group_Type);
955 956 957
      pragma Assert (Kind (Mode_Identifier) = K_Identifier);
   begin
      return Find_Subclause_Declaration_Classifier
958 959 960
          (Component,
           Mode_Identifier,
           (1 => K_Mode));
961 962 963 964 965 966 967 968 969
   end Find_Mode;

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

   function Find_Port_Group_Classifier
     (Root                  : Node_Id;
      Package_Identifier    : Node_Id;
970
      Port_Group_Identifier : Node_Id) return Node_Id
971 972
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
973 974 975
      pragma Assert
        (No (Package_Identifier)
         or else Kind (Package_Identifier) = K_Identifier);
976 977 978
      pragma Assert (Kind (Port_Group_Identifier) = K_Identifier);
   begin
      return Find_AADL_Declaration_Classifier
979 980 981 982
          (Root,
           Package_Identifier,
           Port_Group_Identifier,
           (1 => K_Feature_Group_Type));
983 984 985 986 987 988 989 990
   end Find_Port_Group_Classifier;

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

   function Find_Property_Association
     (AADL_Declaration          : Node_Id;
991
      Property_Association_Name : Name_Id) return Node_Id
992 993 994
   is
      pragma Assert (Present (AADL_Declaration));

995 996 997
      All_Properties : constant Node_List :=
        Find_All_Property_Associations (AADL_Declaration);
      List_Node : Node_Id;
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
   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;
1021
      Property_Identifier     : Node_Id) return Node_Id
1022 1023
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
1024 1025 1026
      pragma Assert
        (No (Property_Set_Identifier)
         or else Kind (Property_Set_Identifier) = K_Identifier);
1027 1028 1029 1030 1031
      pragma Assert (Kind (Property_Identifier) = K_Identifier);

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

1036
      if Present (Property_Set_Identifier) then
1037 1038
         Property_Set :=
           Node_In_Scope (Property_Set_Identifier, Entity_Scope (Root));
1039

1040 1041
         --  If we found the corresponding property set, then we look
         --  for the property in it.
1042 1043

         if Present (Property_Set) then
1044 1045
            Found_Property_Declaration :=
              Node_In_Scope (Property_Identifier, Entity_Scope (Property_Set));
1046 1047 1048 1049 1050 1051 1052 1053
         else
            Found_Property_Declaration := No_Node;
         end if;

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

1054 1055 1056
         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));
1057

1058
            if Present (Property_Set) then
1059 1060 1061 1062
               Found_Property_Declaration :=
                 Node_In_Scope
                   (Property_Identifier,
                    Entity_Scope (Property_Set));
1063
            end if;
1064

1065 1066
            exit when Present (Found_Property_Declaration);
         end loop;
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
      end if;

      return Found_Property_Declaration;
   end Find_Property_Entity;

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

   function Find_Subclause
     (Component  : Node_Id;
1078
      Identifier : Node_Id) return Node_Id
1079
   is
1080 1081 1082
      pragma Assert
        (Kind (Component) = K_Component_Implementation
         or else Kind (Component) = K_Component_Type);
1083 1084 1085
      pragma Assert (Kind (Identifier) = K_Identifier);
   begin
      return Find_Subclause_Declaration_Classifier
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
          (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));
1102 1103 1104 1105 1106 1107 1108 1109 1110
   end Find_Subclause;

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

   function Find_Subclause_Declaration_Classifier
     (Component              : Node_Id;
      Declaration_Identifier : Node_Id;
1111
      Subclause_Kinds        : Node_Kind_Array) return Node_Id
1112
   is
1113 1114 1115 1116 1117
      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);
1118 1119 1120 1121 1122 1123
      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
1124 1125 1126
        and then
          Component_Category'Val (Subcomponent_Category (Component)) =
          CC_Subprogram
1127
      then
1128 1129 1130 1131
         Pointed_Node :=
           Node_In_Scope
             (Declaration_Identifier,
              Entity_Scope (Get_Referenced_Entity (Entity_Ref (Component))));
1132
      else
1133 1134
         Pointed_Node :=
           Node_In_Scope (Declaration_Identifier, Entity_Scope (Component));
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
      end if;

      return Pointed_Node;
   end Find_Subclause_Declaration_Classifier;

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

   function Find_Subcomponent
     (Component               : Node_Id;
      Subcomponent_Identifier : Node_Id;
1147
      In_Modes                : Node_Id := No_Node) return Node_Id
1148 1149 1150 1151 1152 1153
   is
      pragma Assert (Kind (Component) = K_Component_Implementation);
      pragma Assert (Kind (Subcomponent_Identifier) = K_Identifier);

      Pointed_Node : Node_Id;
   begin
1154 1155 1156 1157 1158
      Pointed_Node :=
        Find_Subclause_Declaration_Classifier
          (Component,
           Subcomponent_Identifier,
           (1 => K_Subcomponent));
1159

1160 1161
      Pointed_Node :=
        Filter_Declarations_According_To_Modes (Pointed_Node, In_Modes);
1162 1163 1164 1165

      return Pointed_Node;
   end Find_Subcomponent;

1166 1167 1168 1169 1170 1171
   --------------------
   -- Find_Prototype --
   --------------------

   function Find_Prototype
     (Component            : Node_Id;
1172
      Prototype_Identifier : Node_Id) return Node_Id
1173 1174 1175 1176 1177 1178
   is
      pragma Assert (Kind (Component) = K_Component_Type);
      pragma Assert (Kind (Prototype_Identifier) = K_Identifier);

      Pointed_Node : Node_Id;
   begin
1179 1180 1181 1182 1183
      Pointed_Node :=
        Find_Subclause_Declaration_Classifier
          (Component,
           Prototype_Identifier,
           (1 => K_Prototype));
1184 1185 1186 1187

      return Pointed_Node;
   end Find_Prototype;

1188 1189 1190 1191 1192 1193 1194
   --------------------------
   -- Find_Subprogram_Call --
   --------------------------

   function Find_Subprogram_Call
     (Component       : Node_Id;
      Call_Identifier : Node_Id;
1195
      In_Modes        : Node_Id := No_Node) return Node_Id
1196 1197 1198 1199 1200 1201
   is
      pragma Assert (Kind (Component) = K_Component_Implementation);
      pragma Assert (Kind (Call_Identifier) = K_Identifier);

      Pointed_Node : Node_Id;
   begin
1202 1203 1204 1205 1206
      Pointed_Node :=
        Find_Subclause_Declaration_Classifier
          (Component,
           Call_Identifier,
           (1 => K_Subprogram_Call));
1207

1208 1209
      Pointed_Node :=
        Filter_Declarations_According_To_Modes (Pointed_Node, In_Modes);
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219

      return Pointed_Node;
   end Find_Subprogram_Call;

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

   function Find_In_Import_Declaration
     (Package_Container : Node_Id;
1220
      Node              : Node_Id) return Boolean
1221
   is
1222
      Identifier           : Node_Id := No_Node;
1223 1224 1225 1226
      Pack_Identifier      : Node_Id;
      Import_Node          : Node_Id;
      List_Node            : Node_Id;
      Name_Visibility_Node : Node_Id;
1227
      Success              : Boolean := False;
1228 1229 1230 1231 1232 1233 1234 1235 1236
   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))
1237 1238 1239
        and then
          (Kind (First_Node (Declarations (Package_Container)))) =
          K_Name_Visibility_Declaration
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
      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 :=
1295
           Ocarina.ME_AADL.AADL_Tree.Nodes.First_Node (Decl_List);
1296 1297 1298 1299 1300

         while Present (Local_List_Node) loop
            Success := False;

            for K in Kinds'Range loop
1301
               Success := Success or else (Kind (Local_List_Node) = Kinds (K));
1302 1303 1304 1305 1306
            end loop;

            if Success then
               if No (First_Node) then
                  First_Node := Local_List_Node;
1307
                  Last_Node  := Local_List_Node;
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
               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;