ocarina-analyzer-aadl-queries.adb 33.4 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 . Q U E R I E S         --
--                                                                          --
--                                 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 35 36 37 38 39
--                                                                          --
------------------------------------------------------------------------------

with GNAT.Table;

with Ocarina.ME_AADL;
with Ocarina.ME_AADL.AADL_Tree.Nodes;
with Ocarina.ME_AADL.AADL_Tree.Entities;
with Ocarina.ME_AADL.AADL_Tree.Entities.Properties;
40
with Locations;
41 42 43 44 45 46 47 48 49 50 51 52
with Ocarina.Annotations;

package body Ocarina.Analyzer.AADL.Queries is

   use Ocarina.ME_AADL;
   use Ocarina.ME_AADL.AADL_Tree.Nodes;
   use Ocarina.ME_AADL.AADL_Tree.Entities;
   use Ocarina.ME_AADL.AADL_Tree.Entities.Properties;
   use Ocarina.Annotations;

   package ATN renames Ocarina.ME_AADL.AADL_Tree.Nodes;

hugues.jerome's avatar
hugues.jerome committed
53
   function Get_Category_Of_Entity (Entity : Node_Id) return Named_Element;
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

   ----------------------------
   -- Compute_Property_Value --
   ----------------------------

   function Compute_Property_Value (Property_Value : Node_Id) return Node_Id is

      pragma Assert (Kind (Property_Value) = K_Property_Value);

      Property_Expression : Node_Id;
   begin
      if Expanded_Single_Value (Property_Value) /= No_Node then
         Property_Expression := Expanded_Single_Value (Property_Value);
      elsif Expanded_Multi_Value (Property_Value) /= No_List then
         Property_Expression :=
           First_Node (Expanded_Multi_Value (Property_Value));
      elsif Single_Value (Property_Value) /= No_Node then
         Property_Expression := Single_Value (Property_Value);
      elsif Multi_Value (Property_Value) /= No_List then
         Property_Expression := First_Node (Multi_Value (Property_Value));
      else
         Property_Expression := No_Node;
      end if;

      return Property_Expression;
   end Compute_Property_Value;

   ------------------------------
   -- Get_Property_Association --
   ------------------------------

   function Get_Property_Association
     (Entity  : Node_Id;
      Name    : Name_Id;
88
      In_Mode : Name_Id := No_Name) return Node_Id
89 90 91
   is
   begin
      return Find_Property_Association_From_Name
92 93 94
          (Property_List => ATN.Properties (Entity),
           Property_Name => Name,
           In_Mode       => In_Mode);
95 96 97 98 99 100 101 102 103
   end Get_Property_Association;

   --------------------------
   -- Get_Boolean_Property --
   --------------------------

   function Get_Boolean_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
104
      In_Mode : Name_Id := No_Name) return Boolean
105 106 107
   is
      Property_Value : Node_Id;
   begin
108 109
      Property_Value :=
        Get_Value_Of_Property_Association (Entity, Name, In_Mode);
110 111

      if Property_Value /= No_Node then
112
         if Get_Type_Of_Property_Value (Property_Value, True) = PT_Boolean then
113 114 115 116 117 118 119 120 121 122 123 124 125
            return Get_Boolean_Of_Property_Value (Property_Value);
         else
            return False;
         end if;
      else
         return False;
      end if;
   end Get_Boolean_Property;

   ----------------------------
   -- Get_Category_Of_Entity --
   ----------------------------

hugues.jerome's avatar
hugues.jerome committed
126
   function Get_Category_Of_Entity (Entity : Node_Id) return Named_Element is
127
      pragma Assert (Present (Entity));
hugues.jerome's avatar
hugues.jerome committed
128

129 130
   begin
      case Kind (Entity) is
131 132 133 134
         when K_Component_Type        |
           K_Component_Implementation |
           K_Subcomponent             |
           K_Subcomponent_Access      =>
135 136 137 138 139 140 141 142 143 144 145 146
            return PO_Component_Category;

         when K_Mode =>
            return PO_Mode;

         when K_Feature_Group_Type =>
            if AADL_Version = AADL_V1 then
               return PO_Port_Group;
            else
               return PO_Feature_Group;
            end if;

147 148 149 150 151
         when K_Flow_Spec                   |
           K_Flow_Implementation            |
           K_End_To_End_Flow_Spec           |
           K_Flow_Implementation_Refinement |
           K_End_To_End_Flow_Refinement     =>
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
            return PO_Flow;

         when K_Port_Spec =>
            if Is_Event (Entity) and then Is_Data (Entity) then
               return PO_Event_Data_Port;
            elsif Is_Event (Entity) then
               return PO_Event_Port;
            else
               return PO_Data_Port;
            end if;

         when K_Parameter =>
            return PO_Parameter;

         when K_Subprogram_Spec =>
            if Is_Server (Entity) then
               return PO_Server_Subprogram;
            else
               return PO_Component_Category;
            end if;

         when K_Subprogram_Call =>
            return PO_Component_Category;

         when K_Connection =>
            case Get_Category_Of_Connection (Entity) is
               when CT_Error =>
                  raise Program_Error;
               when CT_Port_Connection =>
                  return PO_Data_Port_Connections;
182 183
               --  XXX Incomplete TODO
               when CT_Data | CT_Data_Delayed =>
184 185 186 187 188 189 190
                  return PO_Data_Port_Connections;
               when CT_Event =>
                  return PO_Event_Port_Connections;
               when CT_Event_Data =>
                  return PO_Event_Data_Port_Connections;
               when CT_Parameter =>
                  return PO_Parameter_Connections;
191 192
               when CT_Feature =>
                  return PO_Feature;
193 194 195 196 197 198 199
               when CT_Feature_Group =>
                  case AADL_Version is
                     when AADL_V1 =>
                        return PO_Port_Group_Connections;
                     when AADL_V2 =>
                        return PO_Feature_Group_Connection;
                  end case;
200 201 202 203 204 205
               when CT_Access_Bus           |
                 CT_Access_Data             |
                 CT_Access_Subprogram       |
                 CT_Access_Subprogram_Group |
                 CT_Access_Virtual_Bus      |
                 CT_Access                  =>
206 207 208 209
                  return PO_Access_Connection;
            end case;

         when others =>
210 211 212 213 214
            raise Program_Error
              with "Invalid kind " &
              Kind (Entity)'Img &
              " at " &
              Locations.Image (Loc (Entity));
215 216 217 218 219 220 221 222 223 224
      end case;
   end Get_Category_Of_Entity;

   ------------------------------
   -- Get_Enumeration_Property --
   ------------------------------

   function Get_Enumeration_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
225
      In_Mode : Name_Id := No_Name) return String
226 227 228
   is
      Property_Value : Node_Id;
   begin
229 230
      Property_Value :=
        Get_Value_Of_Property_Association (Entity, Name, In_Mode);
231 232

      if Property_Value /= No_Node then
233 234
         if Get_Type_Of_Property_Value (Property_Value, True) =
           PT_Enumeration
235 236 237 238 239 240 241 242 243 244 245 246 247
         then
            return Get_Enumeration_Of_Property_Value (Property_Value);
         else
            return "";
         end if;
      else
         return "";
      end if;
   end Get_Enumeration_Property;

   function Get_Enumeration_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
248
      In_Mode : Name_Id := No_Name) return Name_Id
249 250 251
   is
      Property_Value : Node_Id;
   begin
252 253
      Property_Value :=
        Get_Value_Of_Property_Association (Entity, Name, In_Mode);
254 255

      if Property_Value /= No_Node then
256 257
         if Get_Type_Of_Property_Value (Property_Value, True) =
           PT_Enumeration
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
         then
            return Get_Enumeration_Of_Property_Value (Property_Value);
         else
            return No_Name;
         end if;
      else
         return No_Name;
      end if;
   end Get_Enumeration_Property;

   ------------------------
   -- Get_Float_Property --
   ------------------------

   function Get_Float_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
275
      In_Mode : Name_Id := No_Name) return Long_Long_Float
276 277 278
   is
      Property_Value : Node_Id;
   begin
279 280
      Property_Value :=
        Get_Value_Of_Property_Association (Entity, Name, In_Mode);
281 282 283

      if Property_Value /= No_Node then
         if Get_Type_Of_Property_Value (Property_Value, True) = PT_Float
284 285 286
           or else
             Get_Type_Of_Property_Value (Property_Value, True) =
             PT_Unsigned_Float
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
         then
            return Get_Float_Of_Property_Value (Property_Value);
         else
            return 0.0;
         end if;
      else
         return 0.0;
      end if;
   end Get_Float_Property;

   --------------------------
   -- Get_Integer_Property --
   --------------------------

   function Get_Integer_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
304
      In_Mode : Name_Id := No_Name) return Unsigned_Long_Long
305 306 307
   is
      Property_Value : Node_Id;
   begin
308 309
      Property_Value :=
        Get_Value_Of_Property_Association (Entity, Name, In_Mode);
310 311 312

      if Property_Value /= No_Node then
         if Get_Type_Of_Property_Value (Property_Value, True) = PT_Integer
313 314 315
           or else
             Get_Type_Of_Property_Value (Property_Value, True) =
             PT_Unsigned_Integer
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
         then
            return Get_Integer_Of_Property_Value (Property_Value);
         else
            return 0;
         end if;
      else
         return 0;
      end if;
   end Get_Integer_Property;

   ----------------------------
   -- Get_Reference_Property --
   ----------------------------

   function Get_Reference_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
333
      In_Mode : Name_Id := No_Name) return Node_Id
334 335 336
   is
      Property_Value : Node_Id;
   begin
337 338
      Property_Value :=
        Get_Value_Of_Property_Association (Entity, Name, In_Mode);
339 340

      if Property_Value /= No_Node then
341 342
         if Get_Type_Of_Property_Value (Property_Value, True) =
           PT_Reference
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
         then
            return Get_Reference_Of_Property_Value (Property_Value);
         else
            return No_Node;
         end if;
      else
         return No_Node;
      end if;
   end Get_Reference_Property;

   -----------------------------
   -- Get_Classifier_Property --
   -----------------------------

   function Get_Classifier_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
360
      In_Mode : Name_Id := No_Name) return Node_Id
361 362 363
   is
      Property_Value : Node_Id;
   begin
364 365
      Property_Value :=
        Get_Value_Of_Property_Association (Entity, Name, In_Mode);
366 367

      if Property_Value /= No_Node then
368 369
         if Get_Type_Of_Property_Value (Property_Value, True) =
           PT_Classifier
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
         then
            return Get_Classifier_Of_Property_Value (Property_Value);
         else
            return No_Node;
         end if;
      else
         return No_Node;
      end if;
   end Get_Classifier_Property;

   ------------------------
   -- Get_Range_Property --
   ------------------------

   function Get_Range_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
387
      In_Mode : Name_Id := No_Name) return Node_Id
388
   is
389 390 391 392 393
      Property : constant Node_Id :=
        Find_Property_Association_From_Name
          (Property_List => ATN.Properties (Entity),
           Property_Name => Name,
           In_Mode       => In_Mode);
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
   begin

      if Property = No_Node then
         return No_Node;
      end if;

      if Get_Type_Of_Property (Property) /= PT_Range then
         return No_Node;
      end if;

      return Expanded_Single_Value (Property_Association_Value (Property));
   end Get_Range_Property;

   -----------------------
   -- Get_List_Property --
   -----------------------

   function Get_List_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
414
      In_Mode : Name_Id := No_Name) return List_Id
415
   is
416 417 418 419 420
      Property : constant Node_Id :=
        Find_Property_Association_From_Name
          (Property_List => ATN.Properties (Entity),
           Property_Name => Name,
           In_Mode       => In_Mode);
421
   begin
hugues.jerome's avatar
hugues.jerome committed
422 423
      if No (Property)
        or else not Type_Of_Property_Is_A_List
424
          (Get_Referenced_Entity (Property_Name (Property)))
425 426 427 428 429 430 431 432 433 434 435 436 437 438
      then
         return No_List;
      end if;

      return Expanded_Multi_Value (Property_Association_Value (Property));
   end Get_List_Property;

   -------------------------
   -- Get_String_Property --
   -------------------------

   function Get_String_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
439
      In_Mode : Name_Id := No_Name) return String
440 441 442
   is
      Property_Value : Node_Id;
   begin
443 444
      Property_Value :=
        Get_Value_Of_Property_Association (Entity, Name, In_Mode);
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463

      if Property_Value /= No_Node then
         if Get_Type_Of_Property_Value (Property_Value, True) = PT_String then
            return Get_String_Of_Property_Value (Property_Value);
         else
            return "";
         end if;
      else
         return "";
      end if;
   end Get_String_Property;

   -------------------------
   -- Get_String_Property --
   -------------------------

   function Get_String_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
464
      In_Mode : Name_Id := No_Name) return Name_Id
465 466 467
   is
      Property_Value : Node_Id;
   begin
468 469
      Property_Value :=
        Get_Value_Of_Property_Association (Entity, Name, In_Mode);
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488

      if Property_Value /= No_Node then
         if Get_Type_Of_Property_Value (Property_Value, True) = PT_String then
            return Get_String_Of_Property_Value (Property_Value);
         else
            return No_Name;
         end if;
      else
         return No_Name;
      end if;
   end Get_String_Property;

   ---------------------------------------
   -- Get_Value_Of_Property_Association --
   ---------------------------------------

   function Get_Value_Of_Property_Association
     (Entity  : Node_Id;
      Name    : Name_Id;
489
      In_Mode : Name_Id := No_Name) return Node_Id
490
   is
491 492 493 494 495
      Property : constant Node_Id :=
        Find_Property_Association_From_Name
          (Property_List => ATN.Properties (Entity),
           Property_Name => Name,
           In_Mode       => In_Mode);
496 497
   begin
      if Property /= No_Node then
498
         return Compute_Property_Value (Property_Association_Value (Property));
499 500 501 502 503 504 505 506 507 508 509
      else
         return No_Node;
      end if;
   end Get_Value_Of_Property_Association;

   ---------------------
   -- Is_An_Extension --
   ---------------------

   function Is_An_Extension
     (Component : Node_Id;
510
      Ancestor  : Node_Id) return Boolean
511
   is
512 513 514 515
      pragma Assert
        (Kind (Component) = K_Component_Implementation
         or else Kind (Component) = K_Component_Type
         or else Kind (Component) = K_Feature_Group_Type);
516

517 518 519 520 521
      pragma Assert
        (No (Ancestor)
         or else Kind (Ancestor) = K_Component_Implementation
         or else Kind (Ancestor) = K_Component_Type
         or else Kind (Ancestor) = K_Feature_Group_Type);
522 523 524 525 526 527 528 529 530 531 532 533 534

      Temp_Node : Node_Id := Component;
      Type_Node : Node_Id := Component;
   begin
      if Ancestor = No_Node then
         return False;
      end if;

      while Temp_Node /= No_Node loop
         if Temp_Node = Ancestor then
            return True;

         elsif Kind (Component) = K_Component_Implementation then
535 536
            Type_Node :=
              Corresponding_Entity (Component_Type_Identifier (Temp_Node));
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

            while Type_Node /= No_Node loop
               if Type_Node = Ancestor then
                  return True;
               end if;

               if Parent (Type_Node) /= No_Node then
                  Type_Node := Get_Referenced_Entity (Parent (Type_Node));
               else
                  Type_Node := No_Node;
               end if;
            end loop;
         end if;

         if Parent (Temp_Node) /= No_Node then
            Temp_Node := Get_Referenced_Entity (Parent (Temp_Node));
         else
            Temp_Node := No_Node;
         end if;
      end loop;

      return False;
   end Is_An_Extension;

   ---------------------------------
   -- Is_Defined_Boolean_Property --
   ---------------------------------

   function Is_Defined_Boolean_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
568
      In_Mode : Name_Id := No_Name) return Boolean
569 570 571
   is
      Property_Value : Node_Id;
   begin
572 573
      Property_Value :=
        Get_Value_Of_Property_Association (Entity, Name, In_Mode);
574

575 576
      return Present (Property_Value)
        and then
577 578 579 580 581 582 583 584 585 586
        (Get_Type_Of_Property_Value (Property_Value, True) = PT_Boolean);
   end Is_Defined_Boolean_Property;

   -------------------------------------
   -- Is_Defined_Enumeration_Property --
   -------------------------------------

   function Is_Defined_Enumeration_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
587
      In_Mode : Name_Id := No_Name) return Boolean
588 589 590
   is
      Property_Value : Node_Id;
   begin
591 592
      Property_Value :=
        Get_Value_Of_Property_Association (Entity, Name, In_Mode);
593

594 595 596 597
      return Present (Property_Value)
        and then
          Get_Type_Of_Property_Value (Property_Value, True) =
          PT_Enumeration;
598 599 600 601 602 603 604 605 606
   end Is_Defined_Enumeration_Property;

   -------------------------------
   -- Is_Defined_Float_Property --
   -------------------------------

   function Is_Defined_Float_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
607
      In_Mode : Name_Id := No_Name) return Boolean
608 609 610
   is
      Property_Value : Node_Id;
   begin
611 612
      Property_Value :=
        Get_Value_Of_Property_Association (Entity, Name, In_Mode);
613

614 615
      return Present (Property_Value)
        and then
616
        (Get_Type_Of_Property_Value (Property_Value, True) = PT_Float
617 618 619
         or else
           Get_Type_Of_Property_Value (Property_Value, True) =
           PT_Unsigned_Float);
620 621 622 623 624 625 626 627 628
   end Is_Defined_Float_Property;

   ---------------------------------
   -- Is_Defined_Integer_Property --
   ---------------------------------

   function Is_Defined_Integer_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
629
      In_Mode : Name_Id := No_Name) return Boolean
630 631 632
   is
      Property_Value : Node_Id;
   begin
633 634
      Property_Value :=
        Get_Value_Of_Property_Association (Entity, Name, In_Mode);
635

636 637
      return Present (Property_Value)
        and then
638
        (Get_Type_Of_Property_Value (Property_Value, True) = PT_Integer
639 640 641
         or else
           Get_Type_Of_Property_Value (Property_Value, True) =
           PT_Unsigned_Integer);
642 643 644 645 646 647 648 649 650
   end Is_Defined_Integer_Property;

   -------------------------
   -- Is_Defined_Property --
   -------------------------

   function Is_Defined_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
651
      In_Mode : Name_Id := No_Name) return Boolean
652 653 654 655
   is
   begin
      --  A property is defined if it exists and has a value

656 657
      return Get_Value_Of_Property_Association (Entity, Name, In_Mode) /=
        No_Node;
658 659 660 661 662 663 664 665 666
   end Is_Defined_Property;

   -----------------------------------
   -- Is_Defined_Reference_Property --
   -----------------------------------

   function Is_Defined_Reference_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
667
      In_Mode : Name_Id := No_Name) return Boolean
668 669 670
   is
      Property_Value : Node_Id;
   begin
671 672
      Property_Value :=
        Get_Value_Of_Property_Association (Entity, Name, In_Mode);
673

674 675 676 677
      return Present (Property_Value)
        and then
          Get_Type_Of_Property_Value (Property_Value, True) =
          PT_Reference;
678 679 680 681 682
   end Is_Defined_Reference_Property;

   function Is_Defined_Classifier_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
683
      In_Mode : Name_Id := No_Name) return Boolean
684 685 686
   is
      Property_Value : Node_Id;
   begin
687 688
      Property_Value :=
        Get_Value_Of_Property_Association (Entity, Name, In_Mode);
689

690 691 692 693
      return Present (Property_Value)
        and then
          Get_Type_Of_Property_Value (Property_Value, True) =
          PT_Classifier;
694 695 696 697 698 699 700 701 702
   end Is_Defined_Classifier_Property;

   -------------------------------
   -- Is_Defined_Range_Property --
   -------------------------------

   function Is_Defined_Range_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
703
      In_Mode : Name_Id := No_Name) return Boolean
704
   is
705 706 707 708 709
      Property : constant Node_Id :=
        Find_Property_Association_From_Name
          (Property_List => ATN.Properties (Entity),
           Property_Name => Name,
           In_Mode       => In_Mode);
710
   begin
711 712 713 714 715 716
      return Present (Property)
        and then
          Get_Type_Of_Property_Value
            (Property_Association_Value (Property),
             True) =
          PT_Range;
717 718 719 720 721 722 723 724 725
   end Is_Defined_Range_Property;

   ------------------------------
   -- Is_Defined_List_Property --
   ------------------------------

   function Is_Defined_List_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
726
      In_Mode : Name_Id := No_Name) return Boolean
727
   is
728 729 730 731 732
      Property : constant Node_Id :=
        Find_Property_Association_From_Name
          (Property_List => ATN.Properties (Entity),
           Property_Name => Name,
           In_Mode       => In_Mode);
733 734 735
   begin
      return Present (Property)
        and then Type_Of_Property_Is_A_List
736
          (Get_Referenced_Entity (Property_Name (Property)));
737 738 739 740 741 742 743 744 745
   end Is_Defined_List_Property;

   --------------------------------
   -- Is_Defined_String_Property --
   --------------------------------

   function Is_Defined_String_Property
     (Entity  : Node_Id;
      Name    : Name_Id;
746
      In_Mode : Name_Id := No_Name) return Boolean
747 748 749 750 751
   is
      pragma Assert (Present (Entity));

      Property_Value : Node_Id;
   begin
752 753
      Property_Value :=
        Get_Value_Of_Property_Association (Entity, Name, In_Mode);
754

755 756
      return Present (Property_Value)
        and then Get_Type_Of_Property_Value (Property_Value, True) = PT_String;
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
   end Is_Defined_String_Property;

   ---------------
   -- Needed_By --
   ---------------

   function Needed_By (N : Node_Id; Entity : Node_Id) return Boolean is
      --  During the expansion phase, each component or property
      --  declaration is annotated with the components that need
      --  it. For exemple each AADL component C corresponding to a
      --  subcomponent S of another component D will be annotated with
      --  D. Therefore, D "needs" C.

      --  To see whether N is needed by Entity, we loop through all
      --  the annotations of N and see whether one of them is eqaul to
      --  Entity or if it is (recursively) "needed" by Entity.

      --  To avoid infinite recursion (that may occurs between data
      --  having a subprogram as a feature and a being access-required
      --  by the same subprogram), we build a list of the visited
      --  nodes to guarantee they are checked only once.

      package Checked_Nodes is new GNAT.Table (Node_Id, Nat, 1, 10, 50);
      --  This is the list of the already checked "nodes"

      function Is_Checked (E : Node_Id) return Boolean;
      --  See whether E has already been ckecked or not

      procedure Set_Checked (E : Node_Id);
      --  Append E to the checked node table

      function Need (E : Node_Id) return Boolean;
      --  This is the Actual recursive routine. It returns True when
      --  Entity is an annotation of E or else whether it is "needs"
      --  one of the annotations of E.

      ----------------
      -- Is_Checked --
      ----------------

      function Is_Checked (E : Node_Id) return Boolean is
         use Checked_Nodes;
      begin
         for J in First .. Last loop
            if Table (J) = E then
               return True;
            end if;
         end loop;

         return False;
      end Is_Checked;

      -----------------
      -- Set_Checked --
      -----------------

      procedure Set_Checked (E : Node_Id) is
         use Checked_Nodes;
      begin
         Append (E);
      end Set_Checked;

      ----------
      -- Need --
      ----------

      function Need (E : Node_Id) return Boolean is
         A      : Annotation_Id;
         Result : Boolean;

      begin
         Set_Checked (E);

         A := First_Annotation (E);
         if A = No_Annotation then
            Result := False;

         elsif Annotation_Index (E, Entity) /= 0 then
            Result := True;

         else
            Result := False;
            while A /= No_Annotation loop
               if not Is_Checked (Annotation_Node (A)) then
                  Result := Need (Annotation_Node (A));
                  exit when Result;
               end if;

               A := Next_Annotation (A);
            end loop;
         end if;

         return Result;
      end Need;

      Result : Boolean;
   begin
      --  Always return true when asked for a dependency upon a
      --  property declaration.

857 858 859 860 861
      Result :=
        Kind (N) = K_Property_Definition_Declaration
        or else Kind (N) = K_Property_Type_Declaration
        or else Kind (N) = K_Constant_Property_Declaration
        or else Need (N);
862 863 864 865 866 867 868 869 870 871 872 873 874 875

      --  Deallocate the checked nodes list

      Checked_Nodes.Free;

      return Result;
   end Needed_By;

   ----------------------------------
   -- Property_Can_Apply_To_Entity --
   ----------------------------------

   function Property_Can_Apply_To_Entity
     (Property : Node_Id;
876
      Entity   : Node_Id) return Boolean
877 878 879 880
   is
      pragma Assert (Kind (Property) = K_Property_Association);
      pragma Assert (Present (Entity));

881 882 883
      Property_Name : constant Node_Id :=
        Get_Referenced_Entity
          (Ocarina.ME_AADL.AADL_Tree.Nodes.Property_Name (Property));
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899

      pragma Assert
        (Is_All (Applies_To (Property_Name))
         or else Owner_Categories (Applies_To (Property_Name)) /= No_List);

      List_Node                  : Node_Id := No_Node;
      Corresponding_Component    : Node_Id := No_Node;
      Category_Of_Property_Owner : Named_Element;
      Category_Of_Component      : Component_Category;
      Success                    : Boolean;
      Can_Apply                  : Boolean;
   begin
      if Is_All (Applies_To (Property_Name)) then
         Success := True;

      else
900 901
         Success                    := False;
         Category_Of_Property_Owner := Get_Category_Of_Entity (Entity);
902 903 904 905

         --  Get the corresponding component, for some tests

         if Kind (Entity) = K_Subcomponent then
906
            Corresponding_Component := Get_Corresponding_Component (Entity);
907 908

         elsif Kind (Entity) = K_Subprogram_Call then
909
            Corresponding_Component := Get_Corresponding_Subprogram (Entity);
910 911 912 913 914 915 916 917 918 919

         else
            Corresponding_Component := Entity;
         end if;

         --  Get the category of the entity

         if Kind (Entity) = K_Component_Type
           or else Kind (Entity) = K_Component_Implementation
         then
920
            Category_Of_Component := Get_Category_Of_Component (Entity);
921 922 923 924 925 926 927 928 929 930

         elsif Kind (Entity) = K_Subprogram_Spec
           and then not Is_Server (Entity)
         then
            Category_Of_Component := CC_Subprogram;

         elsif Kind (Entity) = K_Subprogram_Call then
            Category_Of_Component := CC_Subprogram;

         elsif Kind (Entity) = K_Subcomponent then
931
            Category_Of_Component := Get_Category_Of_Subcomponent (Entity);
932 933

         elsif Kind (Entity) = K_Subcomponent_Access then
934 935
            Category_Of_Component :=
              Component_Category'Val (Subcomponent_Category (Entity));
936 937
         end if;

938 939 940
         if AADL_Version = AADL_V1 then
            --  AADLV1 only: access property names can be applied to
            --  subcomponent accesses.
941

942 943 944 945
            if Kind (Entity) = K_Subcomponent_Access
              and then not Is_Access (Property_Name)
            then
               --  No need to go further, it cannot apply
946

947 948
               return False;
            end if;
949 950 951 952
         end if;

         --  Check if the property can be applied to the entity

953 954
         List_Node :=
           First_Node (Owner_Categories (Applies_To (Property_Name)));
955 956 957 958

         while List_Node /= No_Node and then not Success loop
            case Category_Of_Property_Owner is
               when PO_Component_Category =>
959
                  --  Here, we check that the component (or an access
960
                  --  to this component) matches one of the
961 962 963
                  --  corresponding component types or meta model
                  --  elements in the 'applies to'

964
                  Can_Apply :=
965
                    ((Named_Element'Val (Category (List_Node)) =
966 967 968 969 970 971 972
                      PO_Component_Category
                      or else
                        Named_Element'Val (Category (List_Node)) =
                        PO_Component_Access)
                     and then
                     (Category_Of_Component =
                      Component_Category'Val (Component_Cat (List_Node))))
973
                    or else Category_Of_Component = CC_Abstract;
974

975 976 977 978
                  --  if not, then we check that the kind of entity
                  --  matches one particular meta-model element

                  if not Can_Apply then
979 980 981 982 983
                     Can_Apply :=
                       Kind (Entity) = K_Subcomponent
                       and then
                         Named_Element'Val (Category (List_Node)) =
                         PO_Subcomponent;
984 985
                  end if;

986 987 988
                  --  XXX dubious, here we erase the previously
                  --  computed value of Can_Apply, to be investigated.

989 990
                  if Present (Classifier_Ref (List_Node)) then
                     if Present (Corresponding_Component) then
991 992 993 994 995 996
                        Can_Apply :=
                          Is_An_Extension
                            (Component => Corresponding_Component,
                             Ancestor  =>
                               Get_Referenced_Entity
                                 (Classifier_Ref (List_Node)));
997 998 999 1000 1001
                     else
                        Can_Apply := False;
                     end if;
                  end if;

1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
               when PO_Event_Data_Port |
                 PO_Event_Port         |
                 PO_Data_Port          |
                 PO_Port               =>
                  Can_Apply :=
                    Named_Element'Val (Category (List_Node)) =
                    Category_Of_Property_Owner
                    or else Named_Element'Val (Category (List_Node)) = PO_Port;

               when PO_Data_Port_Connections    |
                 PO_Event_Port_Connections      |
                 PO_Event_Data_Port_Connections =>

                  Can_Apply :=
                    Named_Element'Val (Category (List_Node)) =
                    Category_Of_Property_Owner
                    or else
                      Named_Element'Val (Category (List_Node)) =
                      PO_Port_Connections
                    or else
                      Named_Element'Val (Category (List_Node)) =
                      PO_Connection
                    or else
                      Named_Element'Val (Category (List_Node)) =
                      PO_Connections;

               when PO_Parameter_Connections |
                 PO_Feature_Group_Connection |
                 PO_Port_Group_Connections   |
                 PO_Access_Connection        |
                 PO_Connection               |
                 PO_Connections              =>
                  Can_Apply :=
                    Named_Element'Val (Category (List_Node)) =
                    Category_Of_Property_Owner
                    or else
                      Named_Element'Val (Category (List_Node)) =
                      PO_Connection
                    or else
                      Named_Element'Val (Category (List_Node)) =
                      PO_Connections;
1043 1044

               when others =>
1045 1046 1047
                  Can_Apply :=
                    Named_Element'Val (Category (List_Node)) =
                    Category_Of_Property_Owner;
1048 1049
            end case;

1050
            Success   := Success or else Can_Apply;
1051 1052 1053 1054 1055 1056 1057 1058
            List_Node := Next_Node (List_Node);
         end loop;
      end if;

      return Success;
   end Property_Can_Apply_To_Entity;

end Ocarina.Analyzer.AADL.Queries;