ocarina-analyzer-aadl-links.adb 139 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 . L I N K S           --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--       Copyright (C) 2009 Telecom ParisTech, 2010-2013 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
--                                                                          --
------------------------------------------------------------------------------

with Utils;

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

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

package body Ocarina.Analyzer.AADL.Links is

   use Utils;
   use Ocarina.Analyzer.Messages;
   use Ocarina.Analyzer.AADL.Finder;
   use Ocarina.Analyzer.AADL.Naming_Rules;
   use Ocarina.ME_AADL;
   use Ocarina.ME_AADL.AADL_Tree.Nodes;
   use Ocarina.ME_AADL.AADL_Tree.Nutils;
   use Ocarina.ME_AADL.AADL_Tree.Entities;
   use Ocarina.ME_AADL.AADL_Tree.Entities.Properties;

59 60 61
   Global_Root : Node_Id := No_Node;
   --  Store the root of the current AADL_Specification

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
   function Link_Declarations_Of_Package
     (Root    : Node_Id;
      Node    : Node_Id)
     return Boolean;
   --  Perform all the designator and identifier links in the
   --  declarations of an AADL package.

   function Link_Declarations_Of_Property_Set
     (Root    : Node_Id;
      Node    : Node_Id)
     return Boolean;
   --  Perform all the designator and identifier links in the
   --  declarations of an AADL property set.

   function Link_Component_Implementation_Subclauses
     (Root    : Node_Id;
      Node    : Node_Id)
     return Boolean;
   --  Perform all the designator and identifier links in the
   --  subclauses (call sequences, subcomponents...) of a component
   --  implementation.

   function Link_Component_Type_Subclauses
     (Root    : Node_Id;
      Node    : Node_Id)
     return Boolean;
   --  Perform all the designator and identifier links in the
89
   --  subclauses (features...) of a component type.
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

   function Link_Feature_Group_Type_Subclauses
     (Root    : Node_Id;
      Node    : Node_Id)
     return Boolean;
   --  Perform all the designator and identifier links in the
   --  subclauses (features...) of a port group (AADL_V1) or
   --  a feature group (AADL_V2)

   function Link_Properties_Of_Component_Type
     (Root    : Node_Id;
      Node    : Node_Id)
     return Boolean;
   --  Perform all the designator and identifier links in the property
   --  associations of a component type.

   function Link_Properties_Of_Component_Implementation
     (Root    : Node_Id;
      Node    : Node_Id)
     return Boolean;
   --  Perform all the designator and identifier links in the property
   --  associations of a component implementation.

   function Link_Properties_Of_Feature_Group_Type
     (Root    : Node_Id;
      Node    : Node_Id)
     return Boolean;
   --  Perform all the designator and identifier links in the property
   --  associations of a port group (AADL_V1) or a feature group (AADL_V2)

   function Link_Property_Value
     (Root               : Node_Id;
      Container          : Node_Id;
      Property_Container : Node_Id;
      Node               : Node_Id;
      Property_Type      : Node_Id)
     return Boolean;
   --  Perform all the designator and identifier links in the property
   --  value 'Node'.

   function Link_Type_Designator
     (Root       : Node_Id;
      Designator : Node_Id)
     return Boolean;
   --  Perform the designator and identifier link of a property type.

   function Link_Properties_Of_Package
     (Root    : Node_Id;
      Node    : Node_Id)
     return Boolean;
   --  Perform all the designator and identifier links in the property
   --  associations of an AADL package.

   procedure Retrieve_Connection_End
     (Component          :     Node_Id;
      Connection_End     :     Node_Id;
      Corresponding_Node : out Node_Id;
      Is_Local           : out Boolean);
   --  Find the node corresponding to the end of a connection

   function Link_Flow_Feature
     (Feature_Identifier : Node_Id;
      Component          : Node_Id)
     return Node_Id;
   --  Return the feature instance having the identifier
   --  'Feature_Identifier' in the component 'Component'. Perform all
   --  the necessary links between the reference and the found
   --  entities.

   function Link_Flow_Of_Subcomponent
     (Flow_Identifier : Node_Id;
      Component       : Node_Id;
      In_Modes        : Node_Id := No_Node)
     return Node_Id;
   --  Return the flow instance having the identifier
   --  'Flow_Identifier' in the component 'Component' in the modes
   --  'In_Modes'. This function performs all the necessary links
   --  between the reference and the found entities.

   function Link_Flow_Connections (Flow : Node_Id) return Boolean;
   --  Performs links and checks on the flow connection list.

   function Equals (Unit_Id_1 : Node_Id; Unit_Id_2 : Node_Id) return Boolean;
   --  Return True when the two identifiers have the same name. This
   --  function is *not* case sensitive.

   function Unwind_Units_Type
     (Root          : Node_Id;
      Property_Type : Node_Id)
     return Node_Id;
   --  Return the units type declaration corresponding to the given
   --  property type. If the type definition does not contain any unit
   --  definition, then return No_Bode.

   ------------
   -- Equals --
   ------------

   function Equals (Unit_Id_1 : Node_Id; Unit_Id_2 : Node_Id) return Boolean is
   begin
      return To_Lower (Name (Unit_Id_1)) = To_Lower (Name (Unit_Id_2));
   end Equals;

   -------------------------------
   -- Link_Flow_Of_Subcomponent --
   -------------------------------

   function Link_Flow_Of_Subcomponent
     (Flow_Identifier : Node_Id;
      Component       : Node_Id;
      In_Modes        : Node_Id := No_Node)
     return Node_Id
   is
      pragma Assert (Kind (Flow_Identifier) = K_Entity_Reference);
      pragma Assert (Kind (Component) = K_Component_Implementation
                     or else Kind (Component) = K_Component_Type);

      Pointed_Node      : Node_Id;
      Pointed_Component : Node_Id;
   begin
      --  The entity reference must be in the form "a.b", otherwise it
      --  cannot be a path of a subcomponent.

      if Length (Path (Flow_Identifier)) /= 2 then
         return No_Node;
      end if;

      --  Fetch "a" and link it

      Pointed_Node := Find_Subcomponent
        (Component               => Component,
         Subcomponent_Identifier => Item (First_Node (Path (Flow_Identifier))),
         In_Modes                => In_Modes);

      Set_Corresponding_Entity
        (Item (First_Node (Path (Flow_Identifier))),
         Pointed_Node);

      Display_Node_Link
        (Item (First_Node (Path (Flow_Identifier))),
         Pointed_Node);

      if Present (Pointed_Node) then
         --  Fetch "b" and link it

         Pointed_Component := Get_Referenced_Entity
           (Entity_Ref (Pointed_Node));

         Pointed_Node := Find_Flow_Spec
           (Pointed_Component,
            Item (Next_Node (First_Node (Path (Flow_Identifier)))));

         Set_Corresponding_Entity
           (Item (Next_Node (First_Node (Path (Flow_Identifier)))),
            Pointed_Node);

         Set_Referenced_Entity (Flow_Identifier, Pointed_Node);

         Display_Node_Link
           (Item (Next_Node (First_Node (Path (Flow_Identifier)))),
            Pointed_Node);
      end if;

      return Pointed_Node;
   end Link_Flow_Of_Subcomponent;

   -----------------------
   -- Link_Flow_Feature --
   -----------------------

   function Link_Flow_Feature
     (Feature_Identifier : Node_Id;
      Component          : Node_Id)
     return Node_Id
   is
      pragma Assert (Kind (Feature_Identifier) = K_Entity_Reference);
      pragma Assert (Kind (Component) = K_Component_Implementation
                     or else Kind (Component) = K_Component_Type);

      Pointed_Node       : Node_Id;
      Pointed_Port_Group : Node_Id;
   begin
      Pointed_Node := Find_Feature
        (Component          => Component,
         Feature_Identifier => Item (First_Node (Path (Feature_Identifier))));

      Set_Corresponding_Entity
        (Item (First_Node (Path (Feature_Identifier))),
         Pointed_Node);

      Display_Node_Link
        (Item (First_Node (Path (Feature_Identifier))),
         Pointed_Node);

      if Present (Next_Node (First_Node (Path (Feature_Identifier)))) then
         Pointed_Port_Group := Get_Referenced_Entity
           (Entity_Ref (Pointed_Node));
         Pointed_Node := Find_Feature
           (Component          => Pointed_Port_Group,
            Feature_Identifier => Item
              (Next_Node (First_Node (Path (Feature_Identifier)))));

         Set_Corresponding_Entity
           (Item (Next_Node (First_Node (Path (Feature_Identifier)))),
            Pointed_Node);

         Set_Referenced_Entity (Feature_Identifier, Pointed_Node);

         Display_Node_Link
           (Next_Node (First_Node (Path (Feature_Identifier))),
            Pointed_Node);
      end if;

      if Present (Pointed_Node)
        and then Kind (Pointed_Node) /= K_Port_Spec
        and then Kind (Pointed_Node) /= K_Feature_Group_Spec
        and then Kind (Pointed_Node) /= K_Parameter
      then
         return No_Node;
      else
         return Pointed_Node;
      end if;
   end Link_Flow_Feature;

   ---------------
   -- Link_Call --
   ---------------

   function Link_Call
     (Root    : Node_Id;
      Node    : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
      pragma Assert (Kind (Node) = K_Subprogram_Call);
      pragma Assert (Kind (Entity_Ref (Node)) = K_Entity_Reference);

      Success            : Boolean := True;
      Pointed_Node       : Node_Id := No_Node;
      Other_Pointed_Node : Node_Id := No_Node;

      Subprogram_Ref  : constant Node_Id := Entity_Ref (Node);
      Pack_Identifier : constant Node_Id := Namespace_Identifier
        (Subprogram_Ref);

      Pointed_Node_Is_Ok       : Boolean;
      Other_Pointed_Node_Is_Ok : Boolean;
   begin
338 339
      --  Either look in available components

340 341 342 343 344
      Pointed_Node := Find_Component_Classifier
        (Root                 => Root,
         Package_Identifier   => Pack_Identifier,
         Component_Identifier => Identifier (Subprogram_Ref));

345 346 347 348 349 350 351 352
      --  or in local subclauses

      if No (Pointed_Node) then
         Pointed_Node := Find_Subclause
           (Container_Component (Parent_Sequence (Node)),
            Identifier (Subprogram_Ref));
      end if;

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
      if Present (Next_Node (First_Node (Path (Subprogram_Ref)))) then
         Other_Pointed_Node := Find_Component_Classifier
           (Root                 => Root,
            Package_Identifier   => Pack_Identifier,
            Component_Identifier => Item (First_Node (Path (Subprogram_Ref))));

         if Present (Other_Pointed_Node)
           and then Kind (Other_Pointed_Node) = K_Component_Type
           and then (Component_Category'Val
                     (Category (Other_Pointed_Node)) = CC_Thread
                     or else Component_Category'Val
                     (Category (Other_Pointed_Node)) = CC_Data)
         then
            --  Link the Identifier to its corresponding component

            Set_Corresponding_Entity
              (Item (First_Node (Path (Subprogram_Ref))),
               Other_Pointed_Node);

            Other_Pointed_Node := Find_Feature
              (Component          => Other_Pointed_Node,
               Feature_Identifier => Item
                 (Next_Node (First_Node (Path (Subprogram_Ref)))));
         else
            Other_Pointed_Node := No_Node;
         end if;
      end if;

      Pointed_Node_Is_Ok := Present (Pointed_Node)
        and then
        ((Kind (Pointed_Node) = K_Component_Type
384 385 386
            or else Kind (Pointed_Node) = K_Component_Implementation
            or else Kind (Pointed_Node) = K_Subcomponent)

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
         and then Component_Category'Val (Category (Pointed_Node)) =
         CC_Subprogram);

      case AADL_Version is
         when AADL_V1 =>
            Other_Pointed_Node_Is_Ok := Present (Other_Pointed_Node)
              and then Kind (Other_Pointed_Node) = K_Subprogram_Spec;

         when AADL_V2 =>
            Other_Pointed_Node_Is_Ok := Present (Other_Pointed_Node)
              and then Kind (Other_Pointed_Node) = K_Subcomponent_Access;
      end case;

      if Pointed_Node_Is_Ok and then Other_Pointed_Node_Is_Ok then
         DAE (Node1    => Node,
              Message1 => " points to ",
              Node2    => Pointed_Node);
         DAE (Node1    => Node,
              Message1 => " also points to ",
              Node2    => Other_Pointed_Node);
         Success := False;

      elsif Pointed_Node_Is_Ok then
         Set_Referenced_Entity (Entity_Ref (Node), Pointed_Node);

      elsif Other_Pointed_Node_Is_Ok then
         --  In this case, the Other_Pointed_Node is a subprogram
         --  spec, we must link it now because the data component the
         --  subprogram spec may be declared at the end of the AADL
         --  specification.

418
         Success := Link_Feature (Root, Other_Pointed_Node, No_Node)
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
           and then Success;

         Set_Referenced_Entity (Entity_Ref (Node), Other_Pointed_Node);

      else
         DLTWN (Node, Pointed_Node);
         Success := False;
      end if;

      return Success;
   end Link_Call;

   ----------------------------------------------
   -- Link_Component_Implementation_Subclauses --
   ----------------------------------------------

   function Link_Component_Implementation_Subclauses
     (Root    : Node_Id;
      Node    : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
      pragma Assert (Kind (Node) = K_Component_Implementation);

      List_Node         : Node_Id;
      Call_List_Node    : Node_Id;
      Success           : Boolean := True;
      Subclause_Success : Boolean := True;
   begin
      --  modes, connections and flows are linked only if
      --  subcomponents and features were correctly linked. Indeed,
      --  those subclauses may access elements pointed by
      --  subcomponents or features.

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

         while Present (List_Node) loop
459
            Success := Link_Feature (Root, List_Node, No_Node)
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
              and then Success;
            List_Node := Next_Node (List_Node);
         end loop;
      end if;

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

         while Present (List_Node) loop
            Success := Link_Subcomponent (Root, List_Node)
              and then Link_In_Modes_Statement (Node, In_Modes (List_Node))
              and then Success;
            List_Node := Next_Node (List_Node);
         end loop;
      end if;

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

         while Present (List_Node) loop
            Success := Link_In_Modes_Statement (Node, In_Modes (List_Node))
              and then Success;

            if not Is_Empty (Subprogram_Calls (List_Node)) then
               Call_List_Node := First_Node (Subprogram_Calls (List_Node));

               while Present (Call_List_Node) loop
                  Success := Link_Call (Root, Call_List_Node)
                    and then Success;
                  Call_List_Node := Next_Node (Call_List_Node);
               end loop;
            end if;

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

      Subclause_Success := Success;

      if Subclause_Success
        and then not Is_Empty
        (Ocarina.Me_AADL.AADL_Tree.Nodes.Connections (Node))
      then
         List_Node :=
           First_Node (Ocarina.Me_AADL.AADL_Tree.Nodes.Connections (Node));

         while Present (List_Node) loop
510
            Global_Root := Root;
511 512 513 514
            Success := Link_Connection (Node, List_Node)
              and then Link_In_Modes_Statement (Node, In_Modes (List_Node))
              and then Success;
            List_Node := Next_Node (List_Node);
515
            Global_Root := No_Node;
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
         end loop;
      end if;

      if Subclause_Success
        and then not Is_Empty (Ocarina.Me_AADL.AADL_Tree.Nodes.Flows (Node))
      then
         List_Node :=
           First_Node (Ocarina.Me_AADL.AADL_Tree.Nodes.Flows (Node));

         while Present (List_Node) loop
            if Kind (List_Node) = K_End_To_End_Flow_Refinement
              or else Kind (List_Node) = K_End_To_End_Flow_Spec
            then
               Success := Link_End_To_End_Flow_Spec (Node, List_Node)
                 and then Link_In_Modes_Statement (Node, In_Modes (List_Node))
                 and then Success;
            elsif Kind (List_Node) = K_Flow_Implementation_Refinement
              or else Kind (List_Node) = K_Flow_Implementation
            then
               Success := Link_Flow_Implementation (Node, List_Node)
                 and then Link_In_Modes_Statement (Node, In_Modes (List_Node))
                 and then Success;
            end if;

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

      if Subclause_Success
        and then not Is_Empty (Ocarina.Me_AADL.AADL_Tree.Nodes.Modes (Node))
      then
         List_Node :=
           First_Node (Ocarina.Me_AADL.AADL_Tree.Nodes.Modes (Node));

         while Present (List_Node) loop
            if Kind (List_Node) = K_Mode_Transition then
               Success := Link_Mode_Transition (Node, List_Node)
                 and then Success;
            end if;

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

      return Success;
   end Link_Component_Implementation_Subclauses;

   -----------------------------------------------------
   -- Link_Component_Implementation_To_Component_Type --
   -----------------------------------------------------

   function Link_Component_Implementation_To_Component_Type
     (Root    : Node_Id;
      Node    : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
      pragma Assert (Kind (Node) = K_Component_Implementation);

      Success      : Boolean := True;
      Pointed_Node : Node_Id;
   begin
      Pointed_Node := Find_Component_Classifier
        (Root                 => Root,
         Package_Identifier   => No_Node,
         Component_Identifier => Component_Type_Identifier (Node));
      --  According to the AADL syntax, the component type must be in
      --  the same namespace as the implementations.

      if No (Pointed_Node) then
         DAE (Node1    => Node,
              Message1 => " implements a component type that does not exist");
         Success := False;
589

590 591 592 593 594 595
      elsif Kind (Pointed_Node) /= K_Component_Type then
         DAE (Node1    => Node,
              Message1 => " implements ",
              Node2    => Pointed_Node,
              Message2 => ", which is not a component type");
         Success := False;
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
      elsif Category (Pointed_Node) /= Category (Node) then
         DAE (Node1    => Node,
              Message1 => " implements ",
              Node2    => Pointed_Node,
              Message2 => ", which is of different kind");
         Success := False;
      else
         Set_Corresponding_Entity (Component_Type_Identifier (Node),
                                   Pointed_Node);
         Success := True;
      end if;

      return Success;
   end Link_Component_Implementation_To_Component_Type;

   -----------------------------------------------
   -- Link_Component_Or_Feature_Group_Extension --
   -----------------------------------------------

   function Link_Component_Or_Feature_Group_Extension
     (Root    : Node_Id;
      Node    : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
      pragma Assert (Kind (Node) = K_Component_Implementation
                     or else Kind (Node) = K_Component_Type
                     or else Kind (Node) = K_Feature_Group_Type);

      Success      : Boolean := True;
      Pointed_Node : Node_Id;
   begin
      if Present (Parent (Node)) then
         declare
            Component_Ref   : constant Node_Id :=  Parent (Node);
            Pack_Identifier : Node_Id;
         begin
            Pack_Identifier := Namespace_Identifier (Component_Ref);

            if Kind (Node) = K_Feature_Group_Type then
               Pointed_Node := Find_Port_Group_Classifier
                 (Root                  => Root,
                  Package_Identifier    => Pack_Identifier,
                  Port_Group_Identifier => Identifier (Component_Ref));
            else
               Pointed_Node := Find_Component_Classifier
                 (Root                 => Root,
                  Package_Identifier   => Pack_Identifier,
                  Component_Identifier => Identifier (Component_Ref));

            end if;
         end;

         if No (Pointed_Node) then
            DAE (Node1    => Node,
                 Message1 => " extends something that does not exist");
            Success := False;
         elsif Kind (Pointed_Node) /= Kind (Node) then
            DAE (Node1    => Node,
                 Message1 => " extends ",
                 Node2    => Pointed_Node,
                 Message2 => ", which is not of the same kind");
            Success := False;
         elsif Kind (Node) /= K_Feature_Group_Type
           and then (Category (Pointed_Node) /=
                       Component_Category'Pos (CC_Abstract)
           and then Category (Pointed_Node) /= Category (Node))
         then
            DAE (Node1    => Node,
                 Message1 => " extends ",
                 Node2    => Pointed_Node,
                 Message2 => ", which is of different type");
            Success := False;
         else
            Set_Referenced_Entity (Parent (Node), Pointed_Node);
            Success := True;
         end if;
      end if;

      return Success;
   end Link_Component_Or_Feature_Group_Extension;

   ------------------------------------
   -- Link_Component_Type_Subclauses --
   ------------------------------------

   function Link_Component_Type_Subclauses
     (Root    : Node_Id;
      Node    : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
      pragma Assert (Kind (Node) = K_Component_Type);

      List_Node : Node_Id;
      Success   : Boolean := True;
   begin
      if not Is_Empty (Features (Node)) then
         List_Node := First_Node (Features (Node));

         while Present (List_Node) loop
698
            Success := Link_Feature (Root, List_Node, Node)
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
              and then Success;
            List_Node := Next_Node (List_Node);
         end loop;
      end if;

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

         while Present (List_Node) loop
            Success := Link_Flow_Spec (Node, List_Node)
              and then Success;
            List_Node := Next_Node (List_Node);
         end loop;
      end if;

      return Success;
   end Link_Component_Type_Subclauses;

   ---------------------
   -- Link_Connection --
   ---------------------

   function Link_Connection
     (Component : Node_Id;
      Node      : Node_Id)
     return Boolean
   is
      Success              : Boolean := True;
      Source_Node          : Node_Id;
      Destination_Node     : Node_Id;
      Source_Is_Local      : Boolean;
      Destination_Is_Local : Boolean;
   begin
      if Is_Refinement (Node) then
         return True;
      end if;

      pragma Assert (Kind (Component) = K_Component_Implementation);
      pragma Assert (Kind (Node) = K_Connection);
      pragma Assert (Kind (Source (Node)) = K_Entity_Reference);
      pragma Assert (Kind (Destination (Node)) = K_Entity_Reference);

      --  Connection source

      Retrieve_Connection_End
        (Component          => Component,
         Connection_End     => Source (Node),
         Corresponding_Node => Source_Node,
         Is_Local           => Source_Is_Local);

      if No (Source_Node) then
         DAE (Node1    => Source (Node),
              Message1 => "does not point to anything");
         Success := False;
      end if;

      --  Connection destination

      Retrieve_Connection_End
        (Component          => Component,
         Connection_End     => Destination (Node),
         Corresponding_Node => Destination_Node,
         Is_Local           => Destination_Is_Local);

      if No (Destination_Node) then
         DAE (Node1    => Destination (Node),
              Message1 => "does not point to anything");
         Success := False;
      end if;

      if Success then
         Set_Referenced_Entity (Source (Node), Source_Node);
         Display_Node_Link (Identifier (Source (Node)), Source_Node);
         Set_Referenced_Entity (Destination (Node), Destination_Node);
         Display_Node_Link (Identifier (Destination (Node)), Destination_Node);
      end if;

      return Success;
   end Link_Connection;

   -------------------------------------
   -- Link_Declarations_Of_Namespaces --
   -------------------------------------

784
   function Link_Declarations_Of_Namespaces (Root : Node_Id) return Boolean is
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 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
      pragma Assert (Kind (Root) = K_AADL_Specification);

      List_Node : Node_Id;
      Success   : Boolean := True;
   begin
      Push_Scope (Entity_Scope (Root));

      if not Is_Empty (Declarations (Root)) then
         List_Node := First_Node (Declarations (Root));

         while Present (List_Node) loop
            if Kind (List_Node) = K_Package_Specification then
               Success := Link_Declarations_Of_Package
                 (Root    => Root,
                  Node    => List_Node)
                 and then Success;

            elsif Kind (List_Node) = K_Property_Set then
               Success := Link_Declarations_Of_Property_Set
                 (Root    => Root,
                  Node    => List_Node)
                 and then Success;

            elsif Kind (List_Node) = K_Component_Type
              or else Kind (List_Node) = K_Component_Implementation
              or else Kind (List_Node) = K_Feature_Group_Type
            then
               Success := Link_Component_Or_Feature_Group_Extension
                 (Root => Root,
                  Node => List_Node)
                 and then Success;
            end if;

            if Kind (List_Node) = K_Component_Implementation then
               Success := Link_Component_Implementation_To_Component_Type
                 (Root, List_Node)
                 and then Success;
            end if;

            if Kind (List_Node) = K_Feature_Group_Type then
               Success := Link_Inverse_Of_Feature_Group_Type
                 (Root, List_Node)
                 and then Success;
            end if;

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

      Pop_Scope;
      return Success;
   end Link_Declarations_Of_Namespaces;

   ----------------------------------
   -- Link_Declarations_Of_Package --
   ----------------------------------

   function Link_Declarations_Of_Package
     (Root    : Node_Id;
      Node    : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
      pragma Assert (Kind (Node) = K_Package_Specification);

      List_Node : Node_Id;
      Success   : Boolean := True;
   begin
      Push_Scope (Entity_Scope (Node));

      if not Is_Empty (Declarations (Node)) then
         List_Node := First_Node (Declarations (Node));

         while Present (List_Node) loop
            if Kind (List_Node) = K_Component_Type
              or else Kind (List_Node) = K_Component_Implementation
              or else Kind (List_Node) = K_Feature_Group_Type
            then
               Success := Link_Component_Or_Feature_Group_Extension
                 (Root, List_Node)
                 and then Success;
            end if;

            if Kind (List_Node) = K_Component_Implementation then
               Success := Link_Component_Implementation_To_Component_Type
                 (Root, List_Node)
                 and then Success;
            end if;

            if Kind (List_Node) = K_Feature_Group_Type then
               Success := Link_Inverse_Of_Feature_Group_Type
                 (Root, List_Node)
                 and then Success;
            end if;

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

      Pop_Scope;
      return Success;
   end Link_Declarations_Of_Package;

   ---------------------------------------
   -- Link_Declarations_Of_Property_Set --
   ---------------------------------------

   function Link_Declarations_Of_Property_Set
     (Root    : Node_Id;
      Node    : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
      pragma Assert (Kind (Node) = K_Property_Set);

      List_Node : Node_Id;
      Success   : Boolean := True;
   begin
      Push_Scope (Entity_Scope (Node));

      if not Is_Empty (Declarations (Node)) then
         List_Node := First_Node (Declarations (Node));

         while Present (List_Node) loop
            case Kind (List_Node) is
               when K_Property_Definition_Declaration =>
                  Success := Link_Property_Name
                    (Root, List_Node)
                    and then Success;

               when K_Property_Type_Declaration =>
                  Success := Link_Property_Type
                    (Root, List_Node)
                    and then Success;

               when K_Constant_Property_Declaration =>
                  Success := Link_Property_Constant
                    (Root, List_Node)
                    and then Success;

               when others =>
                  raise Program_Error;
            end case;

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

      Pop_Scope;
      return Success;
   end Link_Declarations_Of_Property_Set;

   -------------------------------
   -- Link_End_To_End_Flow_Spec --
   -------------------------------

   function Link_End_To_End_Flow_Spec
     (Component : Node_Id;
      Flow      : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Component) = K_Component_Implementation);
      pragma Assert (Kind (Flow) = K_End_To_End_Flow_Refinement
                     or else Kind (Flow) = K_End_To_End_Flow_Spec);

      Success      : Boolean := True;
      Pointed_Node : Node_Id;
   begin
      case Kind (Flow) is
         when K_End_To_End_Flow_Spec =>
            --  The Source_Flow field must point to a subcomponent
            --  flow source or path.

            Pointed_Node := Link_Flow_Of_Subcomponent
              (Component       => Component,
               Flow_Identifier => Source_Flow (Flow),
               In_Modes        => In_Modes (Flow));

            if No (Pointed_Node) then
               DLTWN (Source_Flow (Flow), Pointed_Node);
               Success := False;
            elsif Flow_Category'Val (Category (Pointed_Node)) /= FC_Source
              and then Flow_Category'Val (Category (Pointed_Node)) /= FC_Path
            then
               DAE
                 (Node1    => Source_Flow (Flow),
                  Message1 => " points to ",
                  Node2    => Pointed_Node,
                  Message2 => " which should be a flow source or flow path");
               Success := False;
            end if;

            --  The Sink_Flow field must point to a subcomponent flow
            --  sink or path.

            Pointed_Node := Link_Flow_Of_Subcomponent
              (Component       => Component,
               Flow_Identifier => Sink_Flow (Flow),
               In_Modes        => In_Modes (Flow));

            if No (Pointed_Node) then
               DLTWN (Sink_Flow (Flow), Pointed_Node);
               Success := False;
            elsif Flow_Category'Val (Category (Pointed_Node)) /= FC_Sink
              and then Flow_Category'Val (Category (Pointed_Node)) /= FC_Path
            then
               DAE
                 (Node1    => Sink_Flow (Flow),
                  Message1 => " points to ",
                  Node2    => Pointed_Node,
                  Message2 => " which should be a flow sink or flow path");
               Success := False;
            end if;

            Success := Link_Flow_Connections (Flow) and then Success;

         when others =>
            null;
      end case;

      return Success;
   end Link_End_To_End_Flow_Spec;

   ------------------
   -- Link_Feature --
   ------------------

   function Link_Feature
     (Root    : Node_Id;
1014 1015
      Node    : Node_Id;
      Component_Type : Node_Id)
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
     return Boolean
   is
      use Ocarina.Analyzer.AADL.Semantics;

      pragma Assert (Kind (Root) = K_AADL_Specification);
      pragma Assert (Kind (Node) = K_Port_Spec
                     or else Kind (Node) = K_Parameter
                     or else Kind (Node) = K_Feature_Group_Spec
                     or else Kind (Node) = K_Subprogram_Spec
                     or else Kind (Node) = K_Subcomponent_Access);

      Success            : Boolean := True;
      Pointed_Node       : Node_Id := No_Node;
      Other_Pointed_Node : Node_Id := No_Node;
      No_Ref_Given       : Boolean := False;
1031
      --  Some features may not refer to components (e.g. data ports)
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
   begin
      if AADL_Version = AADL_V2
        and then Present (Entity_Ref (Node))
        and then Present (Namespace_Identifier (Entity_Ref (Node)))
      then
         Success := Check_Qualified_References
           (Namespace (Container_Component (Node)),
            Entity_Ref (Node));
      end if;

      if Success then
         case Kind (Node) is
            when K_Port_Spec
              | K_Parameter =>
               if Kind (Node) = K_Parameter
                 or else Is_Data (Node)
               then
                  declare
                     Component_Ref : constant Node_Id := Entity_Ref (Node);
                  begin
                     if Present (Component_Ref) then
                        Pointed_Node := Find_Component_Classifier
                          (Root                 => Root,
                           Package_Identifier   => Namespace_Identifier
                             (Component_Ref),
                           Component_Identifier => Identifier
                             (Component_Ref));
1059 1060 1061 1062 1063 1064 1065 1066 1067

                        if No (Pointed_Node)
                          and then Present (Component_Type)
                        then
                           Pointed_Node := Find_Prototype
                             (Component_Type,
                              Identifier (Component_Ref));
                        end if;

1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
                        if No (Pointed_Node) then
                           DLTWN (Node, Component_Ref);
                           Success := False;
                        end if;
                     else
                        Pointed_Node := No_Node;
                        No_Ref_Given := True;
                     end if;

                     if No (Pointed_Node) then
                        if No_Ref_Given then
                           Success := True;
                        else
                           DLTWN (Node, Pointed_Node);
                           Success := False;
                        end if;

                     elsif not
                       ((Kind (Pointed_Node) = K_Component_Type
                           or else Kind (Pointed_Node) =
                           K_Component_Implementation)
                        and then Component_Category'Val
                          (Category (Pointed_Node)) = CC_Data)
1091
                       and then not (Kind (Pointed_Node) = K_Prototype)
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
                     then
                        DLTWN (Node, Pointed_Node);
                        Success := False;
                     else
                        Set_Referenced_Entity (Component_Ref, Pointed_Node);
                        Success := True;
                     end if;
                  end;
               else
                  --  If we are dealing with an event port

                  No_Ref_Given := True;
                  Success := True;
               end if;

            when K_Feature_Group_Spec =>
               declare
1109
                  Port_Group_Ref : Node_Id := Entity_Ref (Node);
1110
               begin
1111 1112 1113 1114 1115
                  if No (Port_Group_Ref) then
                     --  If Entity_Ref is null, look for inverse
                     Port_Group_Ref := Inverse_Of (Node);
                  end if;

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 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 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
                  if Present (Port_Group_Ref) then
                     Pointed_Node := Find_Port_Group_Classifier
                       (Root                  => Root,
                        Package_Identifier    => Namespace_Identifier
                          (Port_Group_Ref),
                        Port_Group_Identifier => Identifier (Port_Group_Ref));
                  else
                     Pointed_Node := No_Node;
                     No_Ref_Given := True;
                  end if;

                  if No (Pointed_Node) then
                     if No_Ref_Given then
                        Success := True;
                     else
                        DLTWN (Node, Pointed_Node);
                        Success := False;
                     end if;

                  elsif Kind (Pointed_Node) /= K_Feature_Group_Type then
                     DLTWN (Node, Pointed_Node);
                     Success := False;
                  else
                     Set_Referenced_Entity (Port_Group_Ref, Pointed_Node);
                     Success := True;
                  end if;
               end;

            when K_Subprogram_Spec =>
               declare
                  Subprog_Ref : constant Node_Id := Entity_Ref (Node);
               begin
                  if Present (Subprog_Ref) then
                     Pointed_Node := Find_Component_Classifier
                       (Root                 => Root,
                        Package_Identifier   => Namespace_Identifier
                          (Subprog_Ref),
                        Component_Identifier => Identifier (Subprog_Ref));

                     Other_Pointed_Node := Find_Component_Classifier
                       (Root                 => Root,
                        Package_Identifier   => Namespace_Identifier
                          (Subprog_Ref),
                        Component_Identifier => Item
                          (First_Node (Path (Subprog_Ref))));

                     if Present (Other_Pointed_Node)
                       and then Kind (Other_Pointed_Node) = K_Component_Type
                       and then Next_Node (First_Node
                                             (Path (Subprog_Ref))) /= No_Node
                     then
                        Other_Pointed_Node := Find_Feature
                          (Component          => Other_Pointed_Node,
                           Feature_Identifier =>
                             Item (Next_Node (First_Node
                                                (Path (Subprog_Ref)))));
                     else
                        Other_Pointed_Node := No_Node;
                     end if;

                  else
                     Pointed_Node := No_Node;
                     No_Ref_Given := True;
                  end if;

                  if Present (Pointed_Node)
                    and then Present (Other_Pointed_Node)
                  then
                     DAE (Node1    => Node,
                          Message1 => " points to ",
                          Node2    => Pointed_Node);
                     DAE (Node1    => Node,
                          Message1 => " also points to ",
                          Node2    => Other_Pointed_Node);
                     Success := False;
                  else
                     if No (Pointed_Node) then
                        Pointed_Node := Other_Pointed_Node;
                     end if;

                     if No (Pointed_Node) then
                        if No_Ref_Given then
                           Success := True;
                           --  Nothing was to be found. It is OK

                        else
                           DLTWN (Node, Pointed_Node);
                           Success := False;
                        end if;

                     elsif not
                       (((Kind (Pointed_Node) = K_Component_Type
                            or else Kind (Pointed_Node) =
                            K_Component_Implementation)
                         and then Component_Category'Val
                           (Category (Pointed_Node)) = CC_Subprogram)
                        or else (Kind (Pointed_Node) = K_Subprogram_Spec
                                   and then not Is_Server (Pointed_Node)))
                     then
                        DLTWN (Node, Pointed_Node);
                        Success := False;
                     else
                        Set_Referenced_Entity (Subprog_Ref, Pointed_Node);
                        Success := True;
                     end if;
                  end if;
               end;

            when K_Subcomponent_Access =>
               declare
                  Subcomp_Ref : constant Node_Id := Entity_Ref (Node);
               begin
                  if Present (Subcomp_Ref) then
                     Pointed_Node := Find_Component_Classifier
                       (Root                 => Root,
                        Package_Identifier   => Namespace_Identifier
                          (Subcomp_Ref),
                        Component_Identifier => Identifier (Subcomp_Ref));
                  else
                     Pointed_Node := No_Node;
                     No_Ref_Given := True;
                  end if;

                  if No (Pointed_Node) then
                     if No_Ref_Given then
                        Success := True;
                     else
                        DLTWN (Node, Pointed_Node);
                        Success := False;
                     end if;

                  elsif not
                    ((Kind (Pointed_Node) = K_Component_Type
                        or else Kind (Pointed_Node) =
                        K_Component_Implementation)
                     and then Category (Pointed_Node) =
                       Subcomponent_Category (Node))
                  then
                     DLTWN (Node, Pointed_Node);
                     Success := False;
                  else
                     Set_Referenced_Entity (Subcomp_Ref, Pointed_Node);
                     Success := True;
                  end if;
               end;

            when others =>
               raise Program_Error;
         end case;
      end if;

      if not No_Ref_Given then
         Display_Node_Link (Node, Pointed_Node);
      end if;

      return Success;
   end Link_Feature;

   ------------------------------
   -- Link_Flow_Implementation --
   ------------------------------

   function Link_Flow_Implementation
     (Component : Node_Id;
      Flow      : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Component) = K_Component_Implementation);
      pragma Assert (Kind (Flow) = K_Flow_Implementation_Refinement
                     or else Kind (Flow) = K_Flow_Implementation);
      Success      : Boolean := True;
      C            : constant Flow_Category
        := Flow_Category'Val (Category (Flow));
      Pointed_Node : Node_Id;
   begin
      case Kind (Flow) is
         when K_Flow_Implementation =>
            --  Check for existing flow spec

            if No (Corresponding_Flow_Spec (Flow)) then
               DAE (Node1    => Flow,
                    Message1 => " does not have a corresponding flow spec");
               Success := False;
            else
               --  In case of a flow source or a flow path, the
               --  Source_Flow field is a feature.

               if C = FC_Source or else C = FC_Path then
                  Pointed_Node := Link_Flow_Feature
                    (Component          => Component,
                     Feature_Identifier => Source_Flow (Flow));

                  Set_Corresponding_Entity
                    (Item (First_Node (Path (Source_Flow (Flow)))),
                     Pointed_Node);
                  Set_Referenced_Entity (Source_Flow (Flow), Pointed_Node);

                  Display_Node_Link (Source_Flow (Flow), Pointed_Node);

                  if No (Pointed_Node) then
                     DAE (Node1    => Source_Flow (Flow),
                          Message1 => " does not point to a feature");
                     Success := False;
                  else
                     --  Check that the source is the same as in the
                     --  flow spec.

                     if Get_Referenced_Entity (Source_Flow (Flow))
                       /= Get_Referenced_Entity
                            (Source_Flow
                             (Corresponding_Flow_Spec
                              (Flow)))
                     then
                        DAE (Node1    => Flow,
                             Message1 => " and its corresponding flow spec"
                               & " have different sources");
                        Success := False;
                     end if;
                  end if;
               end if;

               --  In case of a flow sink or a flow path, the
               --  Sink_Flow field is a feature.

               if C = FC_Sink or else C = FC_Path then
                  Pointed_Node := Link_Flow_Feature
                    (Component          => Component,
                     Feature_Identifier => Sink_Flow (Flow));

                  Set_Corresponding_Entity
                    (Item (First_Node (Path (Sink_Flow (Flow)))),
                     Pointed_Node);
                  Set_Referenced_Entity (Sink_Flow (Flow), Pointed_Node);

                  Display_Node_Link (Sink_Flow (Flow), Pointed_Node);

                  if No (Pointed_Node) then
                     DAE (Node1    => Sink_Flow (Flow),
                          Message1 => " does not point to a feature");
                     Success := False;
                  else
                     --  Check that the sink is the same as in the
                     --  flow spec.

                     if Get_Referenced_Entity (Sink_Flow (Flow))
                       /= Get_Referenced_Entity
                            (Sink_Flow
                             (Corresponding_Flow_Spec
                              (Flow)))
                     then
                        DAE (Node1    => Flow,
                             Message1 => " and its corresponding flow spec"
                               & " have different sinks");
                        Success := False;
                     end if;
                  end if;
               end if;

               --  We do not try to link connection if the flow
               --  extremities are erronous because this can cause an
               --  error cascade.

               Success := Success and then Link_Flow_Connections (Flow);
            end if;

         when others =>
            null;
      end case;

      return Success;
   end Link_Flow_Implementation;

   --------------------
   -- Link_Flow_Spec --
   --------------------

   function Link_Flow_Spec
     (Component : Node_Id;
      Flow      : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Component) = K_Component_Type);
      pragma Assert (Kind (Flow) = K_Flow_Spec);

      Success      : Boolean := True;
      Pointed_Node : Node_Id := No_Node;
   begin
      --  Flow refinements do not contain elements to link. The
      --  semantic part of the analyzer should check if a flow
      --  refinement corresponds to an existing flow
      --  implementation. It should also check that the different
      --  elements of the flow are compatible (e.g. connections
      --  actually connect the flows and ports specified).

      if Is_Refinement (Flow) then
         return True;
      end if;

      if Flow_Category'Val (Category (Flow)) = FC_Source
        or else Flow_Category'Val (Category (Flow)) = FC_Path
      then
         Pointed_Node := Link_Flow_Feature
           (Feature_Identifier => Source_Flow (Flow),
            Component          => Component);

         if No (Pointed_Node) then
            DLTWN (Source_Flow (Flow), Pointed_Node);
            Success := False;
         else
            if Next_Node (First_Node
                          (Path (Source_Flow (Flow)))) = No_Node
            then
               Display_Node_Link
                 (Item (First_Node (Path (Source_Flow (Flow)))), Pointed_Node);
               Set_Corresponding_Entity
                 (Item (First_Node (Path (Source_Flow (Flow)))), Pointed_Node);
            else
               Display_Node_Link
                 (Item (Next_Node (First_Node (Path (Source_Flow (Flow))))),
                  Pointed_Node);
               Set_Corresponding_Entity
                 (Item (Next_Node (First_Node (Path (Source_Flow (Flow))))),
                  Pointed_Node);
            end if;

            Set_Referenced_Entity (Source_Flow (Flow), Pointed_Node);
         end if;
      end if;

      if Flow_Category'Val (Category (Flow)) = FC_Sink
        or else Flow_Category'Val (Category (Flow)) = FC_Path
      then
         Pointed_Node := Link_Flow_Feature
           (Feature_Identifier => Sink_Flow (Flow),
            Component          => Component);

         if No (Pointed_Node) then
            DLTWN (Sink_Flow (Flow), Pointed_Node);
            Success := False;
         else
            if Next_Node (First_Node (Path (Sink_Flow (Flow)))) = No_Node then
               Display_Node_Link (Item (First_Node (Path (Sink_Flow (Flow)))),
                                  Pointed_Node);
               Set_Corresponding_Entity
                 (Item (First_Node (Path (Sink_Flow (Flow)))), Pointed_Node);
            else
               Display_Node_Link
                 (Item (Next_Node (First_Node (Path (Sink_Flow (Flow))))),
                  Pointed_Node);
               Set_Corresponding_Entity
                 (Item (Next_Node (First_Node (Path (Sink_Flow (Flow))))),
                  Pointed_Node);
            end if;

            Set_Referenced_Entity (Sink_Flow (Flow), Pointed_Node);
         end if;
      end if;

      return Success;
   end Link_Flow_Spec;

   -----------------------------
   -- Link_In_Modes_Statement --
   -----------------------------

   function Link_In_Modes_Statement
     (Component : Node_Id;
      In_Modes  : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Component) = K_Component_Implementation);

      function Set_Corresponding_Mode
        (Component      : Node_Id;
         Mode_Reference : Node_Id)
        return Boolean;

      ----------------------------
      -- Set_Corresponding_Mode --
      ----------------------------

      function Set_Corresponding_Mode
        (Component      : Node_Id;
         Mode_Reference : Node_Id)
        return Boolean
      is
         Pointed_Node : Node_Id;
         Success      : Boolean := True;
      begin
         Pointed_Node := Find_Mode
           (Component, Identifier (Mode_Reference));

         if No (Pointed_Node)
           or else Kind (Pointed_Node) /= K_Mode
         then
            DLTWN (Mode_Reference, Pointed_Node);
            Success := False;

         else
            Set_Referenced_Entity (Mode_Reference, Pointed_Node);
         end if;

         return Success;
      end Set_Corresponding_Mode;

      List_Node : Node_Id;
      Success : Boolean := True;
   begin
      if Present (In_Modes) then
         List_Node :=
           First_Node (Ocarina.Me_AADL.AADL_Tree.Nodes.Modes (In_Modes));

         while Present (List_Node) loop
            if Kind (List_Node) = K_Entity_Reference then
               Success := Set_Corresponding_Mode (Component, List_Node)
                 and then Success;

            elsif Kind (List_Node) = K_Pair_Of_Entity_References then
               Success := Set_Corresponding_Mode
                 (Component, First_Reference (List_Node))
                 and then Set_Corresponding_Mode
                 (Component, Second_Reference (List_Node))
                 and then Success;
            else
               raise Program_Error;
            end if;

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

      return Success;
   end Link_In_Modes_Statement;

   ----------------------------------------
   -- Link_Inverse_Of_Feature_Group_Type --
   ----------------------------------------

   function Link_Inverse_Of_Feature_Group_Type
     (Root    : Node_Id;
      Node    : Node_Id)
     return Boolean
   is
      Success      : Boolean := True;
      Pointed_Node : Node_Id := No_Node;
   begin
      if Present (Inverse_Of (Node)) then
         Pointed_Node := Find_Port_Group_Classifier
           (Root                  => Root,
            Package_Identifier    => Namespace_Identifier (Inverse_Of (Node)),
            Port_Group_Identifier => Identifier (Inverse_Of (Node)));

         if No (Pointed_Node)
           or else Kind (Pointed_Node) /= K_Feature_Group_Type
         then
            DLTWN (Node, Pointed_Node);
            Success := False;

         else
            Set_Referenced_Entity (Inverse_Of (Node), Pointed_Node);
         end if;
      end if;

      return Success;
   end Link_Inverse_Of_Feature_Group_Type;

   --------------------------
   -- Link_Mode_Transition --
   --------------------------

   function Link_Mode_Transition
     (Component : Node_Id;
      Node      : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Component) = K_Component_Implementation);
      pragma Assert (Kind (Node) = K_Mode_Transition);

      Source_Mode_List : constant List_Id := Source_Modes (Node);
      Port_List        : constant List_Id := Triggers (Node);
      Destination_Mode : constant Node_Id :=
        Ocarina.Me_AADL.AADL_Tree.Nodes.Destination_Mode (Node);
      List_Node        : Node_Id;
      Entity_Node      : Node_Id;
      Pointed_Node     : Node_Id;
      SC_Owned         : Boolean;
      Success          : Boolean := True;
   begin
      --  We first link with the in event ports

      if not Is_Empty (Port_List) then
         List_Node := First_Node (Port_List);

         while Present (List_Node) loop
            if Kind (List_Node) = K_Mode_Transition_Trigger then
               Entity_Node := Identifier (List_Node);
            else
               Entity_Node := List_Node;
            end if;

            if No (Next_Node (First_Node (Path (Entity_Node)))) then
               --  We look for a feature of the component

               Pointed_Node :=
                 Find_Feature (Component          => Component,
                               Feature_Identifier => Item
                                 (First_Node (Path (Entity_Node))));

               SC_Owned := False;
            else
               --  We look for a feature of a subcomponent

               Pointed_Node :=
                 Find_Subcomponent
                 (Component               => Component,
                  Subcomponent_Identifier => Item
                    (First_Node (Path (Entity_Node))));
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
               if Present (Pointed_Node) then
                  if Present (Entity_Ref (Pointed_Node))
                    and then Present (Get_Referenced_Entity
                                        (Entity_Ref (Pointed_Node)))
                  then
                     Pointed_Node :=
                       Find_Feature
                       (Component          => Get_Referenced_Entity
                          (Entity_Ref (Pointed_Node)),
                        Feature_Identifier => Item
                          (Next_Node (First_Node (Path (Entity_Node)))));

                     SC_Owned := True;
                  elsif Present (Inverse_Of (Pointed_Node))
                    and then Present (Get_Referenced_Entity
                                        (Inverse_Of (Pointed_Node)))
                  then
                     Pointed_Node :=
                       Find_Feature
                       (Component          => Get_Referenced_Entity
                          (Inverse_Of (Pointed_Node)),
                        Feature_Identifier => Item
                          (Next_Node (First_Node (Path (Entity_Node)))));

                     SC_Owned := True;
                  end if;
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
               end if;
            end if;

            if Present (Pointed_Node) then
               if Kind (Pointed_Node) /= K_Port_Spec
                 or else not Is_Event (Pointed_Node)
                 or else Is_Data (Pointed_Node)
               then
                  --  Mode triggers must be pure event ports

                  DAE (Node1    => Entity_Node,
                       Message1 => " points to ",
                       Node2    => Pointed_Node,
                      Message2  => ", which is not an event port");
                  Success := False;
               elsif SC_Owned and then not Is_Out (Pointed_Node) then
                  --  Mode triggers belonging to a subcomponent must
                  --  be OUT or IN OUT.

                  DAE (Node1    => Entity_Node,
                       Message1 => " points to subcomponent port ",
                       Node2    => Pointed_Node,
                       Message2 =>
                         ", which is not an OUT nor INOUT event port");
                  Success := False;
               elsif not SC_Owned and then not Is_In (Pointed_Node) then
                  --  Mode triggers belonging to the current component
                  --  must be IN or IN OUT.

                  DAE (Node1    => Entity_Node,
                       Message1 => " points to port ",
                       Node2    => Pointed_Node,
                       Message2 =>
                         ", which is not an IN nor INOUT event port");
                  Success := False;
               else
                  --  Everything is fine

                  Set_Corresponding_Entity
                    (Item (First_Node (Path (Entity_Node))), Pointed_Node);
               end if;
            else
               DLTWN (Entity_Node, Pointed_Node);
               Success := False;
            end if;

            List_Node := Next_Node (List_Node);
         end loop;
      else
         DAE (Message0 => "Mode transition ",
              Node1    => Node,
              Message1 => " depends on no in event port");
         Success := False;
      end if;

      --  Then we link the source modes with the modes declared within
      --  the component implementation.

      if not Is_Empty (Source_Mode_List) then
         List_Node := First_Node (Source_Mode_List);

         while Present (List_Node) loop
            Pointed_Node := Find_Mode
              (Component       => Component,
               Mode_Identifier => List_Node);

            if Present (Pointed_Node) then
               Set_Corresponding_Entity (List_Node, Pointed_Node);
            else
               DLTWN (List_Node, Pointed_Node);
               Success := False;
            end if;

            List_Node := Next_Node (List_Node);
         end loop;
      else
         DAE (Message0 => "warning: ",
              Node1    => Node,
              Message1 => " has no source mode");
      end if;

      --  Finally we link the destination mode

      Pointed_Node := Find_Mode
        (Component       => Component,
         Mode_Identifier => Destination_Mode);

      if Present (Pointed_Node) then
         Set_Corresponding_Entity (Destination_Mode, Pointed_Node);
      else
         DLTWN (Destination_Mode, Pointed_Node);
         Success := False;
      end if;

      return Success;
   end Link_Mode_Transition;

   ----------------------------------------
   -- Link_Feature_Group_Type_Subclauses --
   ----------------------------------------

   function Link_Feature_Group_Type_Subclauses
     (Root    : Node_Id;
      Node    : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
      pragma Assert (Kind (Node) = K_Feature_Group_Type);

      List_Node : Node_Id;
      Success   : Boolean := True;
   begin
      if not Is_Empty (Ocarina.Me_AADL.AADL_Tree.Nodes.Features (Node)) then
         List_Node :=
           First_Node (Ocarina.Me_AADL.AADL_Tree.Nodes.Features (Node));

         while Present (List_Node) loop
1776
            Success := Link_Feature (Root, List_Node, No_Node)
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890
              and then Success;
            List_Node := Next_Node (List_Node);
         end loop;
      end if;

      return Success;
   end Link_Feature_Group_Type_Subclauses;

   -------------------------------
   -- Link_Property_Association --
   -------------------------------

   function Link_Property_Association
     (Root      : Node_Id;
      Container : Node_Id;
      Node      : Node_Id)
     return Boolean
   is
      use Ocarina.Analyzer.AADL.Semantics;

      pragma Assert (Kind (Root) = K_AADL_Specification);
      pragma Assert (Kind (Node) = K_Property_Association);
      pragma Assert
        (Kind (Container) = K_Component_Implementation
         or else Kind (Container) = K_Component_Type
         or else Kind (Container) = K_Feature_Group_Type
         or else Kind (Container) = K_Package_Specification
         or else Kind (Container) = K_Flow_Spec
         or else Kind (Container) = K_Flow_Implementation
         or else Kind (Container) = K_Flow_Implementation_Refinement
         or else Kind (Container) = K_End_To_End_Flow_Spec
         or else Kind (Container) = K_End_To_End_Flow_Refinement
         or else Kind (Container) = K_Connection
         or else Kind (Container) = K_Subcomponent
         or else Kind (Container) = K_Port_Spec
         or else Kind (Container) = K_Parameter
         or else Kind (Container) = K_Feature_Group_Spec
         or else Kind (Container) = K_Subcomponent_Access
         or else Kind (Container) = K_Subprogram_Spec
         or else Kind (Container) = K_Mode
         or else Kind (Container) = K_Subprogram_Call);

      Success                 : Boolean := True;
      Pointed_Node            : Node_Id;
      Corresponding_Container : Node_Id;
      List_Node               : Node_Id;
      Tmp_Node                : Node_Id;
      Property_Type           : Node_Id := No_Node;
   begin
      if AADL_Version = AADL_V2
        and then Present (Namespace_Identifier (Property_Name (Node)))
        and then Present (Value_Container (Property_Association_Value (Node)))
      then
         Success := Check_Qualified_References
                        (Value_Container (Property_Association_Value (Node)),
                        Namespace_Identifier (Property_Name (Node)));
      end if;

      if Success then
         Pointed_Node := Find_Property_Entity
           (Root                    => Root,
            Property_Set_Identifier => Namespace_Identifier
              (Property_Name (Node)),
            Property_Identifier     => Identifier (Property_Name (Node)));

         if No (Pointed_Node) then
            DAE (Node1    => Node,
                 Message1 => "does not point to anything");
            Success := False;

         elsif Kind (Pointed_Node) /= K_Property_Definition_Declaration then
            DAE (Node1    => Node,
                 Message1 => " points to ",
                 Node2    => Pointed_Node,
                 Message2 => ", which is not a property name");
            Success := False;
         else
            Set_Referenced_Entity (Property_Name (Node), Pointed_Node);

            --  Get the type of the property association

            Property_Type := Property_Name_Type (Pointed_Node);
         end if;

         --  Link to the referenced entity if it is relevant

         if Present (Property_Association_Value (Node)) then
            if Present (Single_Value (Property_Association_Value (Node))) then
               Success := Link_Property_Value
                 (Root,
                  Container,
                  Node,
                  Single_Value (Property_Association_Value (Node)),
                  Property_Type)
                 and then Success;
            else
               List_Node := First_Node
                 (Multi_Value
                    (Property_Association_Value
                       (Node)));

               while Present (List_Node) loop
                  Success := Link_Property_Value
                    (Root,
                     Container,
                     Node,
                     List_Node,
                     Property_Type)
                    and then Success;
                  List_Node := Next_Node (List_Node);
               end loop;
            end if;
         end if;

1891
         --  Link 'applies to' statement
1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916

         if not Is_Empty (Applies_To_Prop (Node)) then
            List_Node := First_Node (Applies_To_Prop (Node));
            Pointed_Node := Container;

            while Present (List_Node) loop
               case Kind (Pointed_Node) is
                  when K_Subcomponent
                    | K_Port_Spec
                    | K_Feature_Group_Spec
                    | K_Feature_Group_Type
                    | K_Parameter
                    | K_Subcomponent_Access
                    | K_Subprogram_Spec
                    | K_Subprogram_Call =>
                     Corresponding_Container :=
                       Get_Referenced_Entity (Entity_Ref (Pointed_Node));
                     --  For subclauses that can refer to a component, we
                     --  retrieve the corresponding entity.

                  when K_Component_Type
                    | K_Component_Implementation =>
                     Corresponding_Container := Pointed_Node;

                  when others =>
1917 1918
                     --  These entities cannot have 'applies to'
                     --  clause in their property associations.
1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930

                     Corresponding_Container := No_Node;
               end case;

               if Present (Corresponding_Container) then
                  Tmp_Node := First_Node (List_Items (List_Node));

                  if Kind (Tmp_Node) = K_Array_Selection then
                     Pointed_Node := Find_Subclause (Corresponding_Container,
                                                     Identifier (Tmp_Node));
                     Tmp_Node := Identifier (Tmp_Node);
                  else
1931 1932 1933
                     while Tmp_Node /= No_Node loop
                        Pointed_Node := Find_Subclause
                          (Corresponding_Container, Tmp_Node);
1934

1935 1936 1937 1938 1939
                        if Present (Pointed_Node) then
                           Set_Corresponding_Entity (Tmp_Node, Pointed_Node);
                           Set_Corresponding_Entity
                             (First_Node (List_Items (List_Node)),
                              Pointed_Node);
1940

1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
                        else
                           DAE (Node1    => Node,
                                Message1 => "points to ",
                                Node2 => Tmp_Node,
                                Message2 =>
                                  "that is not a valid subcomponent");
                           Success := False;
                           exit;
                        end if;

                        Tmp_Node := Next_Node (Tmp_Node);
                        if No (Tmp_Node) then
                           exit;
                        end if;
                        Corresponding_Container :=
                          Get_Referenced_Entity (Entity_Ref (Pointed_Node));
                     end loop;
                  end if;
1959 1960 1961

               else
                  DAE (Node1    => Node,
1962 1963 1964 1965
                       Message1 =>
                         "applies to something that cannot be found");
                  --  XXX is this the correct error message?
                  Pointed_Node := No_Node;
1966
               end if;
1967
               List_Node := Next_Node (List_Node);
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984