ocarina-be_aadl-namespaces.adb 20 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--           O C A R I N A . B E _ A A D L . N A M E S P A C E S            --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                 Copyright (C) 2008, GET-Telecom Paris.                   --
--                                                                          --
-- Ocarina  is free software;  you  can  redistribute  it and/or  modify    --
-- it under terms of the GNU General Public License as published by the     --
-- Free Software Foundation; either version 2, or (at your option) any      --
-- later version. Ocarina is distributed  in  the  hope  that it will be    --
-- useful, but WITHOUT ANY WARRANTY;  without even the implied warranty of  --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General --
-- Public License for more details. You should have received  a copy of the --
-- GNU General Public License distributed with Ocarina; see file COPYING.   --
-- If not, write to the Free Software Foundation, 51 Franklin Street, Fifth --
-- Floor, Boston, MA 02111-1301, USA.                                       --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable to be   --
-- covered  by the  GNU  General  Public  License. This exception does not  --
-- however invalidate  any other reasons why the executable file might be   --
-- covered by the GNU Public License.                                       --
--                                                                          --
--                 Ocarina is maintained by the Ocarina team                --
--                       (ocarina-users@listes.enst.fr)                     --
--                                                                          --
------------------------------------------------------------------------------

with Output;

with Ocarina.ME_AADL;
with Ocarina.ME_AADL.AADL_Tree.Nodes;
with Ocarina.ME_AADL.AADL_Tree.Nutils;
with Ocarina.Property_Sets;
40
with Ocarina.BE_AADL.Annexes;
41 42
with Ocarina.BE_AADL.Components;
with Ocarina.BE_AADL.Identifiers;
43
with Ocarina.BE_AADL.Properties;
44 45 46 47 48 49 50 51

package body Ocarina.BE_AADL.Namespaces is

   use Output;
   use Ocarina.ME_AADL;
   use Ocarina.ME_AADL.AADL_Tree.Nodes;
   use Ocarina.ME_AADL.AADL_Tree.Nutils;
   use Ocarina.Property_Sets;
52
   use Ocarina.BE_AADL.Annexes;
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
   use Ocarina.BE_AADL.Components;
   use Ocarina.BE_AADL.Properties;
   use Ocarina.BE_AADL.Identifiers;

   procedure Print_Import_Declaration (Node : Node_Id);
   procedure Print_Alias_Declaration (Node : Node_Id);

   ------------------------------------
   -- Print_Constrained_Property_Set --
   ------------------------------------

   procedure Print_Constrained_Property_Set
     (Node      : Node_Id;
      Criterion : Node_Id)
   is
      Ident      : constant Node_Id := Identifier (Node);
      List_Node  : Node_Id;

      Someting_To_Print : Boolean := False;
   begin
      --  We do not print standard property sets.

      if not Is_User_Defined (Ident) then
         return;
      end if;

      --  First of all see whether the constraint let us some
      --  declarations to print.

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

         while Present (List_Node) loop

            Someting_To_Print := Is_Printable (List_Node, Criterion)
              or else Someting_To_Print;

            exit when Someting_To_Print;

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

      if not Someting_To_Print then
         return;
      end if;

      Print_Tokens ((T_Property, T_Set));
      Write_Space;
      Print_Identifier (Ident);
      Write_Space;
      Print_Token (T_Is);
      Write_Eol;

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

         while Present (List_Node) loop
            if List_Node /= First_Node (Imports_List (Node)) then
               Write_Eol;
            end if;

            Print_Import_Declaration (List_Node);

            List_Node := Next_Node (List_Node);
         end loop;

         Write_Eol;
         Write_Eol;
      end if;

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

         while Present (List_Node) loop
            if Is_Printable (List_Node, Criterion) then
               case Kind (List_Node) is
                  when K_Property_Definition_Declaration =>
                     Print_Property_Definition_Declaration (List_Node);

                  when K_Property_Type_Declaration =>
                     Print_Property_Type_Declaration (List_Node);

                  when K_Constant_Property_Declaration =>
                     Print_Constant_Property (List_Node);

                  when others =>
                     Node_Not_Handled (List_Node);
               end case;
            end if;

            List_Node := Next_Node (List_Node);
         end loop;

         Decrement_Indentation;
      end if;

      Write_Indentation;
      Print_Token (T_End);
      Write_Space;
      Print_Identifier (Ident);
      Print_Token (T_Semicolon);
      Write_Eol;
   end Print_Constrained_Property_Set;

   ------------------------
   -- Print_Property_Set --
   ------------------------

   procedure Print_Property_Set (Node : Node_Id) is
      procedure Internal_Print_Property_Set is new
        Print_Constrained_Property_Set (Always_Printable);
   begin
      Internal_Print_Property_Set (Node, No_Node);
   end Print_Property_Set;

   -------------------------------
   -- Print_Constrained_Package --
   -------------------------------

   procedure Print_Constrained_Package (Node : Node_Id; Criterion : Node_Id) is
      pragma Assert (Kind (Node) = K_Package_Specification);

      Pack_Identifier   : constant Node_Id := Identifier (Node);
      List_Node         : Node_Id;
      Someting_To_Print : Boolean := False;
      Has_Public        : Boolean := False;
      Has_Private       : Boolean := False;
   begin
      --  First of all see whether the constraint let us some
      --  declarations to print.

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

         while Present (List_Node) loop
            Someting_To_Print := Is_Printable (List_Node, Criterion)
              or else Someting_To_Print;

            Has_Public  := Has_Public  or else not Is_Private (List_Node);
            Has_Private := Has_Private or else     Is_Private (List_Node);

            List_Node := Next_Node (List_Node);
         end loop;
      else
         raise Program_Error;
      end if;

      if not Someting_To_Print then
         return;
      end if;

      Increment_Indentation;
      Print_Token (T_Package);
      Write_Space;
      Print_Identifier (Pack_Identifier);
      Write_Eol;

      --  Public part

      if Has_Public then
         Print_Token (T_Public);
         Write_Eol;

         List_Node := First_Node (Declarations (Node));

         while Present (List_Node) loop
            if not Is_Private (List_Node)
              and then Is_Printable (List_Node, Criterion)
            then
               case Kind (List_Node) is
                  when K_Component_Type =>
                     Print_Component_Type (List_Node);

                  when K_Component_Implementation =>
                     Print_Component_Implementation (List_Node);

                  when K_Feature_Group_Type =>
                     Print_Feature_Group_Type (List_Node);

                  when K_Name_Visibility_Declaration =>
                     Print_Name_Visibility_Declaration (List_Node);

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

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

         if not Is_Empty (Ocarina.ME_AADL.AADL_Tree.Nodes.Properties (Node))
           and then AADL_Version = AADL_V1
         then
            declare
               Number_Of_Properties : Integer := 0;
            begin
               List_Node := First_Node
                 (Ocarina.ME_AADL.AADL_Tree.Nodes.Properties (Node));

               while Present (List_Node) loop
                  if not Is_Private (List_Node)
                    and then Is_Printable (List_Node, Criterion)
                  then
                     Number_Of_Properties := Number_Of_Properties + 1;
                  end if;

                  List_Node := Next_Node (List_Node);
               end loop;

               if Number_Of_Properties > 0 then
                  Print_Token (T_Properties);
                  Write_Eol;
               end if;
            end;

            List_Node := First_Node
              (Ocarina.ME_AADL.AADL_Tree.Nodes.Properties (Node));

            while Present (List_Node) loop
               if not Is_Private (List_Node)
                 and then Is_Printable (List_Node, Criterion)
               then
                  Print_Property_Association (List_Node);
               end if;

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

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
      --  Annex subclauses

      if not Is_Empty (Ocarina.ME_AADL.AADL_Tree.Nodes.Annexes (Node)) then
         Increment_Indentation;
         List_Node := First_Node
           (Ocarina.ME_AADL.AADL_Tree.Nodes.Annexes (Node));

         while Present (List_Node) loop
            Print_Annex_Subclause (List_Node);
            List_Node := Next_Node (List_Node);
         end loop;

         Decrement_Indentation;
         Write_Eol;
      end if;

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
      --  private part

      if Has_Private then
         Print_Token (T_Private);
         Write_Eol;

         List_Node := First_Node (Declarations (Node));

         while Present (List_Node) loop
            if Is_Private (List_Node)
              and then Is_Printable (List_Node, Criterion)
            then
               case Kind (List_Node) is
                  when K_Component_Type =>
                     Print_Component_Type (List_Node);

                  when K_Component_Implementation =>
                     Print_Component_Implementation (List_Node);

                  when K_Feature_Group_Type =>
                     Print_Feature_Group_Type (List_Node);

                  when K_Name_Visibility_Declaration =>
                     Print_Name_Visibility_Declaration (List_Node);
                  when others =>
                     raise Program_Error;
               end case;

               Write_Eol;
            end if;

            List_Node := Next_Node (List_Node);
         end loop;

         if not Is_Empty (Ocarina.ME_AADL.AADL_Tree.Nodes.Properties (Node))
           and then AADL_Version = AADL_V1
         then
            declare
               Number_Of_Properties : Integer := 0;
            begin
               List_Node := First_Node
                 (Ocarina.ME_AADL.AADL_Tree.Nodes.Properties (Node));

               while Present (List_Node) loop
                  if Is_Private (List_Node)
                    and then Is_Printable (List_Node, Criterion)
                  then
                     Number_Of_Properties := Number_Of_Properties + 1;
                  end if;

                  List_Node := Next_Node (List_Node);
               end loop;

               if Number_Of_Properties > 0 then
                  Print_Token (T_Properties);
                  Write_Eol;
               end if;
            end;

            List_Node := First_Node
              (Ocarina.ME_AADL.AADL_Tree.Nodes.Properties (Node));

            while Present (List_Node) loop
               if Is_Private (List_Node)
                 and then Is_Printable (List_Node, Criterion)
               then
                  Print_Property_Association (List_Node);
               end if;

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

      if AADL_Version = AADL_V2 then
         if not Is_Empty (Ocarina.ME_AADL.AADL_Tree.Nodes.Properties
                            (Node)) then
            List_Node := First_Node
              (Ocarina.ME_AADL.AADL_Tree.Nodes.Properties (Node));

            while Present (List_Node) loop
               if Is_Printable (List_Node, Criterion) then
                  Print_Property_Association (List_Node);
               end if;

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

      Print_Token (T_End);
      Write_Space;
      Print_Identifier (Pack_Identifier);
      Print_Token (T_Semicolon);
      Write_Eol;
      Decrement_Indentation;
   end Print_Constrained_Package;

   -------------------
   -- Print_Package --
   -------------------

   procedure Print_Package (Node : Node_Id) is
      procedure Internal_Print_Package is new
        Print_Constrained_Package (Always_Printable);
   begin
      Internal_Print_Package (Node, No_Node);
   end Print_Package;

   ------------------------------------------
   -- Print_Constrained_AADL_Specification --
   ------------------------------------------

   procedure Print_Constrained_AADL_Specification
     (Node      : Node_Id;
      Criterion : Node_Id)
   is
      pragma Assert (Kind (Node) = K_AADL_Specification);

      --  Some internal procedures

      procedure Internal_Print_Package is new
        Print_Constrained_Package (Is_Printable);
      procedure Internal_Print_Property_Set is new
        Print_Constrained_Property_Set  (Is_Printable);

      List_Node : Node_Id;
   begin
      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_Component_Type =>
                  if Is_Printable (List_Node, Criterion) then
                     Print_Component_Type (List_Node);
                  end if;

               when K_Component_Implementation =>
                  if Is_Printable (List_Node, Criterion) then
                     Print_Component_Implementation (List_Node);
                  end if;

               when K_Feature_Group_Type =>
                  if Is_Printable (List_Node, Criterion) then
                     Print_Feature_Group_Type (List_Node);
                  end if;

               when K_Package_Specification =>
                  Internal_Print_Package (List_Node, Criterion);

               when K_Property_Set =>
                  Internal_Print_Property_Set (List_Node, Criterion);

               when others =>
                  raise Program_Error;
            end case;

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

   ------------------------------
   -- Print_AADL_Specification --
   ------------------------------

   procedure Print_AADL_Specification (Node : Node_Id) is
      procedure Internal_Print_AADL_Specification is new
        Print_Constrained_AADL_Specification (Always_Printable);
   begin
      Internal_Print_AADL_Specification (Node, No_Node);
   end Print_AADL_Specification;

   ---------------------------------------
   -- Print_Name_Visibility_Declaration --
   ---------------------------------------

   procedure Print_Name_Visibility_Declaration (Node : Node_Id) is
      List_Node : Node_Id;

   begin
      List_Node := First_Node (List_Items (Node));

      while Present (List_Node) loop
         Write_Indentation;
         case Kind (List_Node) is
            when K_Import_Declaration =>
               Print_Import_Declaration (List_Node);

            when K_Alias_Declaration =>
               Print_Alias_Declaration (List_Node);
            when others =>
               raise Program_Error;
         end case;

         Write_Eol;
         List_Node := Next_Node (List_Node);
      end loop;
   end Print_Name_Visibility_Declaration;

   ------------------------------
   -- Print_Import_Declaration --
   ------------------------------

   procedure Print_Import_Declaration (Node : Node_Id) is
      List_Node : Node_Id;
      Name_Node : Node_Id;

   begin
      List_Node := First_Node (List_Items (Node));

      Print_Token (T_With);
      Write_Space;

      while Present (List_Node) loop
         if List_Node /= First_Node (List_Items (Node)) then
            Print_Token (T_Comma);
            Write_Space;
         end if;

         case Kind (List_Node) is
            when K_Identifier =>
               Print_Identifier (List_Node);

            when K_Package_Name =>
               Name_Node := First_Node
                 (Ocarina.ME_AADL.AADL_Tree.Nodes.Identifiers (List_Node));
               while Present (Name_Node) loop
                  if Name_Node /= First_Node
                    (Ocarina.ME_AADL.AADL_Tree.Nodes.Identifiers (List_Node))
                  then
                     Print_Token (T_Colon_Colon);
                  end if;

                  Print_Identifier (Name_Node);
                  Name_Node := Next_Node (Name_Node);
               end loop;

            when others =>
               raise Program_Error;
         end case;

         List_Node := Next_Node (List_Node);
      end loop;

      Print_Token (T_Semicolon);
   end Print_Import_Declaration;

   -----------------------------
   -- Print_Alias_Declaration --
   -----------------------------

   procedure Print_Alias_Declaration (Node : Node_Id) is
      List_Node  : Node_Id;
      Entity_Cat : Ocarina.ME_AADL.Entity_Category;
      Is_All     : Boolean := False;

   begin
      Entity_Cat := Ocarina.ME_AADL.Entity_Category'Val
        (Ocarina.ME_AADL.AADL_Tree.Nodes.Entity_Category (Node));

      Is_All := Ocarina.ME_AADL.AADL_Tree.Nodes.Is_All (Node);

      case Entity_Cat is
         when EC_Package =>
            if Is_All then
               Print_Token (T_Renames);
            else
               Print_Identifier (Identifier (Node));
               Write_Space;
               Print_Tokens ((T_Renames, T_Package));
            end if;

            Write_Space;

            List_Node := First_Node
              (Ocarina.ME_AADL.AADL_Tree.Nodes.Identifiers
                 (Package_Name (Node)));
            while Present (List_Node) loop
               if List_Node /= First_Node
                 (Ocarina.ME_AADL.AADL_Tree.Nodes.Identifiers
                    (Package_Name (Node)))
               then
                  Print_Token (T_Colon_Colon);
               end if;

               Print_Identifier (List_Node);

               List_Node := Next_Node (List_Node);
            end loop;

            if Is_All then
               Print_Token (T_Colon_Colon);
               Print_Token (T_All);
            end if;

         when EC_Component =>
            if Identifier (Node) /= No_Node then
               Print_Identifier (Identifier (Node));
               Write_Space;
            end if;

            Print_Token (T_Renames);
            Write_Space;
            Print_Component_Category (Category (Node));
            Write_Space;
            Print_Entity_Reference (Reference (Node));

         when EC_Feature_Group_Type =>
            if Identifier (Node) /= No_Node then
               Print_Identifier (Identifier (Node));
               Write_Space;
            end if;

            Print_Tokens ((T_Renames, T_Feature, T_Group));
            Write_Space;
            Print_Entity_Reference (Reference (Node));

         when others =>
            raise Program_Error;
      end case;

      Print_Token (T_Semicolon);

   end Print_Alias_Declaration;

end Ocarina.BE_AADL.Namespaces;