ocarina-backends-po_hi_ada-marshallers.adb 39.6 KB
Newer Older
1 2 3 4 5 6 7 8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--                 OCARINA.BACKENDS.PO_HI_ADA.MARSHALLERS                   --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
--    Copyright (C) 2006-2009 Telecom ParisTech, 2010-2015 ESA & ISAE.      --
--                                                                          --
-- Ocarina  is free software; you can redistribute it and/or modify under   --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion. 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.                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
26
--                                                                          --
jhugues's avatar
jhugues committed
27 28
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
29 30 31
--                                                                          --
------------------------------------------------------------------------------

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

with Ocarina.ME_AADL;
with Ocarina.ME_AADL.AADL_Instances.Nodes;
with Ocarina.ME_AADL.AADL_Instances.Nutils;
with Ocarina.ME_AADL.AADL_Instances.Entities;

with Ocarina.Backends.Utils;
with Ocarina.Backends.Properties;
with Ocarina.Backends.Messages;
with Ocarina.Backends.Ada_Tree.Nutils;
with Ocarina.Backends.Ada_Tree.Nodes;
with Ocarina.Backends.PO_HI_Ada.Runtime;
with Ocarina.Backends.PO_HI_Ada.Mapping;

package body Ocarina.Backends.PO_HI_Ada.Marshallers is

49
   use Ocarina.Namet;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
   use Ocarina.ME_AADL;
   use Ocarina.ME_AADL.AADL_Instances.Nodes;
   use Ocarina.Backends.Utils;
   use Ocarina.Backends.Properties;
   use Ocarina.Backends.Messages;
   use Ocarina.ME_AADL.AADL_Instances.Entities;
   use Ocarina.Backends.Ada_Tree.Nutils;
   use Ocarina.Backends.PO_HI_Ada.Runtime;
   use Ocarina.Backends.PO_HI_Ada.Mapping;

   package ADN renames Ocarina.Backends.Ada_Tree.Nodes;
   package AAN renames Ocarina.ME_AADL.AADL_Instances.Nodes;
   package AAU renames Ocarina.ME_AADL.AADL_Instances.Nutils;

   function Get_Marshalled_Type (E : Node_Id) return Node_Id;
   --  Return depending on the category of component E, the type that
   --  should be used in procedure Marshall and Unmarshall.

   -------------------------
   -- Get_Marshalled_Type --
   -------------------------

   function Get_Marshalled_Type (E : Node_Id) return Node_Id is
      Category : constant Component_Category := Get_Category_Of_Component (E);
74
      T        : Node_Id;
75 76 77
   begin
      case Category is
         when CC_Thread =>
78 79 80
            T :=
              Extract_Designator
                (ADN.Port_Interface_Node (Backend_Node (Identifier (E))));
81 82

         when CC_Data =>
83 84 85
            T :=
              Extract_Designator
                (ADN.Type_Definition_Node (Backend_Node (Identifier (E))));
86 87

         when others =>
88 89 90 91
            raise Program_Error
              with "Cannot generate Marshall procedure" &
              " for a " &
              Component_Category'Image (Category);
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
      end case;

      return T;
   end Get_Marshalled_Type;

   ------------------
   -- Package_Spec --
   ------------------

   package body Package_Spec is

      procedure Visit_Architecture_Instance (E : Node_Id);
      procedure Visit_Component_Instance (E : Node_Id);
      procedure Visit_System_Instance (E : Node_Id);
      procedure Visit_Process_Instance (E : Node_Id);
      procedure Visit_Thread_Instance (E : Node_Id);
      procedure Visit_Subprogram_Instance (E : Node_Id);
      procedure Visit_Data_Instance (E : Node_Id);

      function Marshall_Spec (E : Node_Id) return Node_Id;
      --  Creates a spec for a Marshall procedure for a data type
      --  generated from an AADL data component, a Thread_Port type
      --  generated from an AADL thread component or a Port_Type type
      --  generated from an AADL process component.

      function Unmarshall_Spec (E : Node_Id) return Node_Id;
      --  Same as above but with an Unmarshall procedure

      -------------------
      -- Marshall_Spec --
      -------------------

      function Marshall_Spec (E : Node_Id) return Node_Id is
125 126
         N       : Node_Id;
         Profile : constant List_Id := New_List (ADN.K_Parameter_Profile);
127 128 129
      begin
         --  The 'Data' parameter

130 131 132 133 134
         N :=
           Make_Parameter_Specification
             (Make_Defining_Identifier (PN (P_Data)),
              Get_Marshalled_Type (E),
              Mode_In);
135 136 137 138
         Append_Node_To_List (N, Profile);

         --  The 'Message' parameter

139 140 141 142 143
         N :=
           Make_Parameter_Specification
             (Make_Defining_Identifier (PN (P_Message)),
              RE (RE_Message_Type),
              Mode_Inout);
144 145
         Append_Node_To_List (N, Profile);

146 147 148 149
         N :=
           Make_Subprogram_Specification
             (Make_Defining_Identifier (SN (S_Marshall)),
              Profile);
150 151 152 153 154 155 156 157 158

         return N;
      end Marshall_Spec;

      ---------------------
      -- Unmarshall_Spec --
      ---------------------

      function Unmarshall_Spec (E : Node_Id) return Node_Id is
159 160 161 162
         Category : constant Component_Category :=
           Get_Category_Of_Component (E);
         N       : Node_Id;
         Profile : constant List_Id := New_List (ADN.K_Parameter_Profile);
163 164 165 166 167 168
      begin
         --  If we deal with a thread, there is an extra parameter
         --  correspodning to the <T>_Ports enumerator useful for the
         --  marshalling.

         if Category = CC_Thread then
169 170 171 172 173 174
            N :=
              Make_Parameter_Specification
                (Make_Defining_Identifier (PN (P_Port)),
                 Extract_Designator
                   (ADN.Port_Enumeration_Node (Backend_Node (Identifier (E)))),
                 Mode_In);
175 176 177 178 179
            Append_Node_To_List (N, Profile);
         end if;

         --  The 'Data' parameter

180 181 182 183 184
         N :=
           Make_Parameter_Specification
             (Make_Defining_Identifier (PN (P_Data)),
              Get_Marshalled_Type (E),
              Mode_Out);
185 186 187 188
         Append_Node_To_List (N, Profile);

         --  The 'Message' parameter

189 190 191 192 193
         N :=
           Make_Parameter_Specification
             (Make_Defining_Identifier (PN (P_Message)),
              RE (RE_Message_Type),
              Mode_Inout);
194 195
         Append_Node_To_List (N, Profile);

196 197 198 199
         N :=
           Make_Subprogram_Specification
             (Make_Defining_Identifier (SN (S_Unmarshall)),
              Profile);
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

         return N;
      end Unmarshall_Spec;

      -----------
      -- Visit --
      -----------

      procedure Visit (E : Node_Id) is
      begin
         case Kind (E) is
            when K_Architecture_Instance =>
               Visit_Architecture_Instance (E);

            when K_Component_Instance =>
               Visit_Component_Instance (E);

            when others =>
               null;
         end case;
      end Visit;

      ---------------------------------
      -- Visit_Architecture_Instance --
      ---------------------------------

      procedure Visit_Architecture_Instance (E : Node_Id) is
      begin
         Visit (Root_System (E));
      end Visit_Architecture_Instance;

      ------------------------------
      -- Visit_Component_Instance --
      ------------------------------

      procedure Visit_Component_Instance (E : Node_Id) is
236 237
         Category : constant Component_Category :=
           Get_Category_Of_Component (E);
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
      begin
         case Category is
            when CC_System =>
               Visit_System_Instance (E);

            when CC_Process =>
               Visit_Process_Instance (E);

            when CC_Thread =>
               Visit_Thread_Instance (E);

            when CC_Data =>
               Visit_Data_Instance (E);

            when CC_Subprogram =>
               Visit_Subprogram_Instance (E);

            when others =>
               null;
         end case;
      end Visit_Component_Instance;

      -------------------------
      -- Visit_Data_Instance --
      -------------------------

      procedure Visit_Data_Instance (E : Node_Id) is
         N : Node_Id;
      begin
         --  Do not generate Marshallers more than once per node

         if No (Get_Handling (E, By_Name, H_Ada_Marshallers_Spec)) then
            --  Marshallers are generated only for types which can
            --  sent through data ports and event data ports.

            if Get_Data_Representation (E) /= Data_With_Accessors then

275 276 277 278
               N :=
                 Message_Comment
                   ("Marshallers for DATA type " &
                    Get_Name_String (Name (Identifier (E))));
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

               --  Marshall procedure

               N := Marshall_Spec (E);
               Bind_AADL_To_Marshall (Identifier (E), N);
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

               --  Unmarshall procedure

               N := Unmarshall_Spec (E);
               Bind_AADL_To_Unmarshall (Identifier (E), N);
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

               --  Mark the data type as being handled.

               Set_Handling
296 297 298 299
                 (E,
                  By_Name,
                  H_Ada_Marshallers_Spec,
                  Identifier (E));
300 301 302 303 304 305 306
            end if;
         else
            --  Do the tree bindings only

            Bind_AADL_To_Marshall
              (Identifier (E),
               ADN.Marshall_Node
307 308
                 (Backend_Node
                    (Get_Handling (E, By_Name, H_Ada_Marshallers_Spec))));
309 310 311 312

            Bind_AADL_To_Unmarshall
              (Identifier (E),
               ADN.Unmarshall_Node
313 314
                 (Backend_Node
                    (Get_Handling (E, By_Name, H_Ada_Marshallers_Spec))));
315 316 317 318 319 320 321 322
         end if;
      end Visit_Data_Instance;

      ----------------------------
      -- Visit_Process_Instance --
      ----------------------------

      procedure Visit_Process_Instance (E : Node_Id) is
323 324 325
         U : constant Node_Id :=
           ADN.Distributed_Application_Unit
             (ADN.Deployment_Node (Backend_Node (Identifier (E))));
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
         P : constant Node_Id := ADN.Entity (U);
         S : Node_Id;
      begin
         Push_Entity (P);
         Push_Entity (U);
         Set_Marshallers_Spec;

         --  Start recording the handling since they have to be reset
         --  for each node.

         Start_Recording_Handlings;

         --  Visit all the subcomponents of the process

         if not AAU.Is_Empty (Subcomponents (E)) then
            S := First_Node (Subcomponents (E));
            while Present (S) loop
               --  Visit the component instance corresponding to the
               --  subcomponent S.

               Visit (Corresponding_Instance (S));
               S := Next_Node (S);
            end loop;
         end if;

         --  Unmark all the marked types

         Reset_Handlings;

         Pop_Entity; -- U
         Pop_Entity; -- P
      end Visit_Process_Instance;

      -------------------------------
      -- Visit_Subprogram_Instance --
      -------------------------------

      procedure Visit_Subprogram_Instance (E : Node_Id) is
         F : Node_Id;
      begin
         --  Declare all necessary data types

         if not AAU.Is_Empty (Features (E)) then
            F := First_Node (Features (E));

            while Present (F) loop
               if Kind (F) = K_Port_Spec_Instance then
                  Display_Located_Error
                    (Loc (F),
                     "Port features in subprogram are not supported",
                     Fatal => True);
               end if;

               if Present (Corresponding_Instance (F)) then
                  Visit (Corresponding_Instance (F));
               end if;

               F := Next_Node (F);
            end loop;
         end if;
      end Visit_Subprogram_Instance;

      ---------------------------
      -- Visit_System_Instance --
      ---------------------------

      procedure Visit_System_Instance (E : Node_Id) is
         S : Node_Id;
      begin
         Push_Entity (Ada_Root);

         --  Visit all the subcomponents of the system

         if not AAU.Is_Empty (Subcomponents (E)) then
            S := First_Node (Subcomponents (E));
            while Present (S) loop
               --  Visit the component instance corresponding to the
               --  subcomponent S.

               Visit (Corresponding_Instance (S));
               S := Next_Node (S);
            end loop;
         end if;

         Pop_Entity; --  Ada_Root
      end Visit_System_Instance;

      ---------------------------
      -- Visit_Thread_Instance --
      ---------------------------

      procedure Visit_Thread_Instance (E : Node_Id) is
         N : Node_Id;
         F : Node_Id;
      begin
         if Has_Ports (E) then
            --  Generate marshallers for the Port_Type enumeration

424 425
            if No
                (Get_Handling
426 427 428 429
                   (Corresponding_Declaration (E),
                    By_Node,
                    H_Ada_Marshallers_Spec))
            then
430 431 432 433 434 435 436 437 438 439
               Set_Handling
                 (Corresponding_Declaration (E),
                  By_Node,
                  H_Ada_Marshallers_Spec,
                  E);

               N :=
                 Message_Comment
                   ("Marshallers for interface type of thread " &
                    Get_Name_String (Name (Identifier (E))));
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

               --  Marshall procedure

               N := Marshall_Spec (E);
               Bind_AADL_To_Marshall (Identifier (E), N);
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

               --  Unmarshall procedure

               N := Unmarshall_Spec (E);
               Bind_AADL_To_Unmarshall (Identifier (E), N);
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
            else
               declare
455 456 457 458 459 460
                  Found : constant Node_Id :=
                    Get_Handling
                      (Corresponding_Declaration (E),
                       By_Node,
                       H_Ada_Marshallers_Spec);
                  BE : constant Node_Id := Backend_Node (Identifier (Found));
461
               begin
462 463 464 465 466 467
                  Bind_AADL_To_Marshall
                    (Identifier (E),
                     ADN.Marshall_Node (BE));
                  Bind_AADL_To_Unmarshall
                    (Identifier (E),
                     ADN.Unmarshall_Node (BE));
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
               end;
            end if;

         end if;

         --  The only data that need to be marshalled or unmarshalled
         --  is the data that is meant to be sent between threads
         --  (locally or remotly). So we visit only thread features.

         if not AAU.Is_Empty (Features (E)) then
            F := First_Node (Features (E));

            while Present (F) loop
               if Kind (F) = K_Port_Spec_Instance and then AAN.Is_Data (F) then
                  Visit (Corresponding_Instance (F));
               end if;

               F := Next_Node (F);
            end loop;
         end if;
      end Visit_Thread_Instance;

   end Package_Spec;

   ------------------
   -- Package_Body --
   ------------------

   package body Package_Body is

      procedure Visit_Architecture_Instance (E : Node_Id);
      procedure Visit_Component_Instance (E : Node_Id);
      procedure Visit_System_Instance (E : Node_Id);
      procedure Visit_Process_Instance (E : Node_Id);
      procedure Visit_Thread_Instance (E : Node_Id);
      procedure Visit_Subprogram_Instance (E : Node_Id);
      procedure Visit_Data_Instance (E : Node_Id);

      function Marshall_Implementation (E : Node_Id) return Node_Id;
      --  Creates an implementation for a Marshall procedure

      function Unmarshall_Implementation (E : Node_Id) return Node_Id;
      --  Same as above but with an Unmarshall procedure

      function Marshallers_Instantiation (E : Node_Id) return Node_Id;
      --  Creates a generic instantiation for the Marshallers_G
      --  package corresponding to the node E.

      -----------------------------
      -- Marshall_Implementation --
      -----------------------------

      function Marshall_Implementation (E : Node_Id) return Node_Id is
521 522 523
         Spec : constant Node_Id :=
           ADN.Marshall_Node (Backend_Node (Identifier (E)));
         N : Node_Id;
524 525 526 527 528 529
      begin
         --  The marshallers for data component are simple renaming of
         --  intantiated ones. Fo thread components, the body is more
         --  complex.

         if not AAU.Is_Thread (E) then
530 531 532 533 534 535 536 537 538 539 540
            N :=
              Make_Selected_Component
                (Make_Defining_Identifier (Map_Marshallers_Name (E)),
                 Make_Defining_Identifier (SN (S_Marshall)));

            N :=
              Make_Subprogram_Specification
                (Defining_Identifier => ADN.Defining_Identifier (Spec),
                 Parameter_Profile   => ADN.Parameter_Profile (Spec),
                 Return_Type         => ADN.Return_Type (Spec),
                 Renamed_Subprogram  => N);
541 542 543 544 545 546
         else
            declare
               Alternatives : constant List_Id := New_List (ADN.K_List_Id);
               Statements   : List_Id;
               Declarations : List_Id;
               F            : Node_Id;
547
               Has_Data     : Boolean          := False;
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
            begin
               --  Check if the thread conrains at least one OUT DATA
               --  port, other wise, there is nothing to marshall

               F := First_Node (Features (E));

               while Present (F) loop
                  if Kind (F) = K_Port_Spec_Instance
                    and then Is_Out (F)
                    and then AAN.Is_Data (F)
                  then
                     Has_Data := True;
                     exit;
                  end if;

                  F := Next_Node (F);
               end loop;

               if Has_Data then
                  --  If we are at this point, we are sure that the
                  --  thread contains at least one data port. We must
                  --  also take in account the presence of pure event
                  --  ports, bu adding null case alternative for them

                  F := First_Node (Features (E));

                  while Present (F) loop
                     if Kind (F) = K_Port_Spec_Instance
                       and then Is_Out (F)
                     then
                        --  The statements (if any)

                        Statements := New_List (ADN.K_Statement_List);

                        if AAN.Is_Data (F) then
583 584 585 586 587 588 589 590 591 592 593 594 595
                           N :=
                             Make_Subprogram_Call
                               (Extract_Designator
                                  (ADN.Marshall_Node
                                     (Backend_Node
                                        (Identifier
                                           (Corresponding_Instance (F))))),
                                Make_List_Id
                                  (Make_Selected_Component
                                     (Make_Designator (PN (P_Data)),
                                      Make_Defining_Identifier
                                        (Map_Ada_Component_Name (F))),
                                   Make_Defining_Identifier (PN (P_Message))));
596
                           Append_Node_To_List (N, Statements);
597
                        else
598 599 600
                           Append_Node_To_List
                             (Make_Null_Statement,
                              Statements);
601 602
                        end if;

603 604 605 606 607 608 609 610 611
                        N :=
                          Make_Elsif_Statement
                            (Make_Expression
                               (Make_Selected_Component
                                  (Make_Designator (PN (P_Data)),
                                   Make_Designator (PN (P_Port))),
                                Op_Equal,
                                Extract_Enumerator (F, False)),
                             Statements);
612 613 614 615 616 617
                        Append_Node_To_List (N, Alternatives);
                     end if;

                     F := Next_Node (F);
                  end loop;

618
                  declare
619 620 621 622
                     Declarations : constant List_Id :=
                       New_List (ADN.K_Declaration_List);
                     Elsif_Statements : constant List_Id :=
                       New_List (ADN.K_List_Id);
623
                  begin
624 625 626
                     N :=
                       Make_Used_Package
                         (RU (RU_PolyORB_HI_Generated_Activity));
627
                     Append_Node_To_List (N, Declarations);
628

629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
                     ADN.Set_First_Node
                       (Elsif_Statements,
                        ADN.Next_Node (ADN.First_Node (Alternatives)));

                     N :=
                       Make_If_Statement
                         (Condition =>
                            ADN.Condition (ADN.First_Node (Alternatives)),
                          Then_Statements =>
                            ADN.Then_Statements
                              (ADN.First_Node (Alternatives)),
                          Elsif_Statements => Elsif_Statements);

                     N :=
                       Make_Subprogram_Implementation
                         (Spec,
                          Declarations,
                          Make_List_Id (N));
647
                  end;
648 649 650 651 652 653

               else
                  Declarations := New_List (ADN.K_Declaration_List);

                  --  Add a pragma unreferenced for parameters

654 655 656 657 658
                  N :=
                    Make_Pragma_Statement
                      (Pragma_Unreferenced,
                       Make_List_Id
                         (Make_Defining_Identifier (PN (P_Message))));
659 660
                  Append_Node_To_List (N, Declarations);

661 662 663 664
                  N :=
                    Make_Pragma_Statement
                      (Pragma_Unreferenced,
                       Make_List_Id (Make_Defining_Identifier (PN (P_Data))));
665 666
                  Append_Node_To_List (N, Declarations);

667 668 669 670 671
                  N :=
                    Make_Subprogram_Implementation
                      (Spec,
                       Declarations,
                       No_List);
672 673 674 675 676 677 678 679 680 681 682 683
               end if;
            end;
         end if;

         return N;
      end Marshall_Implementation;

      -------------------------------
      -- Unmarshall_Implementation --
      -------------------------------

      function Unmarshall_Implementation (E : Node_Id) return Node_Id is
684 685 686
         Spec : constant Node_Id :=
           ADN.Unmarshall_Node (Backend_Node (Identifier (E)));
         N : Node_Id;
687 688 689 690 691 692
      begin
         --  The marshallers for data component are simple renaming of
         --  intantiated ones. Fo thread components, the body is more
         --  complex.

         if not AAU.Is_Thread (E) then
693 694 695 696 697 698 699 700 701 702 703
            N :=
              Make_Selected_Component
                (Make_Defining_Identifier (Map_Marshallers_Name (E)),
                 Make_Defining_Identifier (SN (S_Unmarshall)));

            N :=
              Make_Subprogram_Specification
                (Defining_Identifier => ADN.Defining_Identifier (Spec),
                 Parameter_Profile   => ADN.Parameter_Profile (Spec),
                 Return_Type         => ADN.Return_Type (Spec),
                 Renamed_Subprogram  => N);
704 705 706
         else
            declare
               Alternatives : constant List_Id := New_List (ADN.K_List_Id);
707 708 709 710 711 712
               Declarations : constant List_Id :=
                 New_List (ADN.K_Declaration_List);
               Statements  : List_Id;
               Aggregates  : List_Id;
               Ref_Message : Boolean := False;
               F           : Node_Id;
713 714 715 716 717 718 719 720 721 722 723
            begin
               --  If the thread has not IN port, there is nothing to
               --  unmarshall

               if Has_In_Ports (E) then
                  --  If we are at this point, we are sure that the
                  --  thread contains at least one port

                  F := First_Node (Features (E));

                  while Present (F) loop
724
                     if Kind (F) = K_Port_Spec_Instance and then Is_In (F) then
725 726 727 728
                        --  The record aggregate

                        Aggregates := New_List (ADN.K_Statement_List);

729 730 731 732
                        N :=
                          Make_Component_Association
                            (Make_Defining_Identifier (PN (P_Port)),
                             Extract_Enumerator (F, False));
733 734 735 736 737 738 739 740 741
                        Append_Node_To_List (N, Aggregates);

                        --  The statements (if any)

                        Statements := New_List (ADN.K_Statement_List);

                        if AAN.Is_Data (F) then
                           --  Declare the temporary variable

742 743 744 745 746 747 748 749
                           N :=
                             Make_Object_Declaration
                               (Defining_Identifier =>
                                  Make_Defining_Identifier
                                    (Map_Ada_Component_Name (F)),
                                Object_Definition =>
                                  Map_Ada_Data_Type_Designator
                                    (Corresponding_Instance (F)));
750 751
                           Append_Node_To_List (N, Declarations);

752 753 754 755 756 757 758 759 760 761 762
                           N :=
                             Make_Subprogram_Call
                               (Extract_Designator
                                  (ADN.Unmarshall_Node
                                     (Backend_Node
                                        (Identifier
                                           (Corresponding_Instance (F))))),
                                Make_List_Id
                                  (Make_Defining_Identifier
                                     (Map_Ada_Component_Name (F)),
                                   Make_Defining_Identifier (PN (P_Message))));
763 764 765 766
                           Append_Node_To_List (N, Statements);

                           --  Append the extra aggregate

767 768 769 770 771 772
                           N :=
                             Make_Component_Association
                               (Make_Defining_Identifier
                                  (Map_Ada_Component_Name (F)),
                                Make_Defining_Identifier
                                  (Map_Ada_Component_Name (F)));
773 774 775 776 777 778 779 780 781 782
                           Append_Node_To_List (N, Aggregates);

                           --  Mark the message formal parameter as
                           --  being referenced.

                           Ref_Message := True;
                        end if;

                        --  Assign the port value

783 784 785 786 787 788 789 790
                        N :=
                          Make_Assignment_Statement
                            (Make_Defining_Identifier (PN (P_Data)),
                             Make_Qualified_Expression
                               (Extract_Designator
                                  (ADN.Port_Interface_Node
                                     (Backend_Node (Identifier (E)))),
                                Make_Record_Aggregate (Aggregates)));
791 792
                        Append_Node_To_List (N, Statements);

793 794 795 796 797 798 799
                        N :=
                          Make_Elsif_Statement
                            (Make_Expression
                               (Make_Defining_Identifier (PN (P_Port)),
                                Op_Equal,
                                Extract_Enumerator (F, False)),
                             Statements);
800 801 802 803 804 805 806 807 808
                        Append_Node_To_List (N, Alternatives);
                     end if;

                     F := Next_Node (F);
                  end loop;

                  if not Ref_Message then
                     --  Add a pragma unreferenced for 'Message'

809 810 811 812 813
                     N :=
                       Make_Pragma_Statement
                         (Pragma_Unreferenced,
                          Make_List_Id
                            (Make_Defining_Identifier (PN (P_Message))));
814 815 816
                     Append_Node_To_List (N, Declarations);
                  end if;

817 818 819
                  declare
                     --  Declarations : constant List_Id
                     --    := New_List (ADN.K_Declaration_List);
820 821
                     Elsif_Statements : constant List_Id :=
                       New_List (ADN.K_List_Id);
822
                  begin
823 824 825
                     N :=
                       Make_Used_Package
                         (RU (RU_PolyORB_HI_Generated_Activity));
826 827
                     Append_Node_To_List (N, Declarations);

828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
                     ADN.Set_First_Node
                       (Elsif_Statements,
                        ADN.Next_Node (ADN.First_Node (Alternatives)));

                     N :=
                       Make_If_Statement
                         (Condition =>
                            ADN.Condition (ADN.First_Node (Alternatives)),
                          Then_Statements =>
                            ADN.Then_Statements
                              (ADN.First_Node (Alternatives)),
                          Elsif_Statements => Elsif_Statements);

                     N :=
                       Make_Subprogram_Implementation
                         (Spec,
                          Declarations,
                          Make_List_Id (N));
846
                  end;
847 848 849 850

               else
                  --  Add a pragma unreferenced for parameters

851 852 853 854
                  N :=
                    Make_Pragma_Statement
                      (Pragma_Unreferenced,
                       Make_List_Id (Make_Defining_Identifier (PN (P_Port))));
855 856
                  Append_Node_To_List (N, Declarations);

857 858 859 860 861
                  N :=
                    Make_Pragma_Statement
                      (Pragma_Unreferenced,
                       Make_List_Id
                         (Make_Defining_Identifier (PN (P_Message))));
862 863
                  Append_Node_To_List (N, Declarations);

864 865 866 867
                  N :=
                    Make_Pragma_Statement
                      (Pragma_Unreferenced,
                       Make_List_Id (Make_Defining_Identifier (PN (P_Data))));
868 869
                  Append_Node_To_List (N, Declarations);

870 871 872 873 874
                  N :=
                    Make_Subprogram_Implementation
                      (Spec,
                       Declarations,
                       No_List);
875 876 877 878 879 880 881 882 883 884 885 886 887
               end if;
            end;
         end if;

         return N;
      end Unmarshall_Implementation;

      -------------------------------
      -- Marshallers_Instantiation --
      -------------------------------

      function Marshallers_Instantiation (E : Node_Id) return Node_Id is
      begin
888
         return Make_Package_Instantiation
yoogx's avatar
yoogx committed
889 890 891 892 893
             (Defining_Identifier =>
                Make_Defining_Identifier (Map_Marshallers_Name (E)),
              Generic_Package =>
                RU (RU_PolyORB_HI_Marshallers_G, Elaborated => True),
              Parameter_List => Make_List_Id (Get_Marshalled_Type (E)));
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
      end Marshallers_Instantiation;

      -----------
      -- Visit --
      -----------

      procedure Visit (E : Node_Id) is
      begin
         case Kind (E) is
            when K_Architecture_Instance =>
               Visit_Architecture_Instance (E);

            when K_Component_Instance =>
               Visit_Component_Instance (E);

            when others =>
               null;
         end case;
      end Visit;

      ---------------------------------
      -- Visit_Architecture_Instance --
      ---------------------------------

      procedure Visit_Architecture_Instance (E : Node_Id) is
      begin
         Visit (Root_System (E));
      end Visit_Architecture_Instance;

      ------------------------------
      -- Visit_Component_Instance --
      ------------------------------

      procedure Visit_Component_Instance (E : Node_Id) is
928 929
         Category : constant Component_Category :=
           Get_Category_Of_Component (E);
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
      begin
         case Category is
            when CC_System =>
               Visit_System_Instance (E);

            when CC_Process =>
               Visit_Process_Instance (E);

            when CC_Thread =>
               Visit_Thread_Instance (E);

            when CC_Data =>
               Visit_Data_Instance (E);

            when CC_Subprogram =>
               Visit_Subprogram_Instance (E);

            when others =>
               null;
         end case;
      end Visit_Component_Instance;

      -------------------------
      -- Visit_Data_Instance --
      -------------------------

      procedure Visit_Data_Instance (E : Node_Id) is
         N : Node_Id;
      begin
         --  Do not generate Marshallers more than once per node

         if No (Get_Handling (E, By_Name, H_Ada_Marshallers_Body)) then
            --  Marshallers are generated only for types which can
            --  sent through data ports and event data ports.

            if Get_Data_Representation (E) /= Data_With_Accessors then

967 968 969 970
               N :=
                 Message_Comment
                   ("Marshallers for DATA type " &
                    Get_Name_String (Name (Identifier (E))));
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
               Append_Node_To_List (N, ADN.Statements (Current_Package));

               --  Package instantiation

               N := Marshallers_Instantiation (E);
               Append_Node_To_List (N, ADN.Statements (Current_Package));

               --  Marshall procedure

               N := Marshall_Implementation (E);
               Append_Node_To_List (N, ADN.Statements (Current_Package));

               --  Unmarshall procedure

               N := Unmarshall_Implementation (E);
               Append_Node_To_List (N, ADN.Statements (Current_Package));

               --  Mark the data type as being handled.

               Set_Handling
991 992 993 994
                 (E,
                  By_Name,
                  H_Ada_Marshallers_Body,
                  Identifier (E));
995 996 997 998 999 1000 1001 1002 1003
            end if;
         end if;
      end Visit_Data_Instance;

      ----------------------------
      -- Visit_Process_Instance --
      ----------------------------

      procedure Visit_Process_Instance (E : Node_Id) is
1004 1005 1006
         U : constant Node_Id :=
           ADN.Distributed_Application_Unit
             (ADN.Deployment_Node (Backend_Node (Identifier (E))));
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 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
         P : constant Node_Id := ADN.Entity (U);
         S : Node_Id;
      begin
         Push_Entity (P);
         Push_Entity (U);
         Set_Marshallers_Body;

         --  Start recording the handling since they have to be reset
         --  for each node.

         Start_Recording_Handlings;

         --  Visit all the subcomponents of the process

         if not AAU.Is_Empty (Subcomponents (E)) then
            S := First_Node (Subcomponents (E));
            while Present (S) loop
               --  Visit the component instance corresponding to the
               --  subcomponent S.

               Visit (Corresponding_Instance (S));
               S := Next_Node (S);
            end loop;
         end if;

         --  Unmark all the marked types

         Reset_Handlings;

         Pop_Entity; -- U
         Pop_Entity; -- P
      end Visit_Process_Instance;

      -------------------------------
      -- Visit_Subprogram_Instance --
      -------------------------------

      procedure Visit_Subprogram_Instance (E : Node_Id) is
         F : Node_Id;
      begin
         --  Declare all necessary data types

         if not AAU.Is_Empty (Features (E)) then
            F := First_Node (Features (E));

            while Present (F) loop
               if Kind (F) = K_Port_Spec_Instance then
                  Display_Located_Error
                    (Loc (F),
                     "Port features in subprogram are not supported",
                     Fatal => True);
               end if;

               if Present (Corresponding_Instance (F)) then
                  Visit (Corresponding_Instance (F));
               end if;

               F := Next_Node (F);
            end loop;
         end if;
      end Visit_Subprogram_Instance;

      ---------------------------
      -- Visit_System_Instance --
      ---------------------------

      procedure Visit_System_Instance (E : Node_Id) is
         S : Node_Id;
      begin
         Push_Entity (Ada_Root);

         --  Visit all the subcomponents of the system

         if not AAU.Is_Empty (Subcomponents (E)) then
            S := First_Node (Subcomponents (E));

            while Present (S) loop
               --  Visit the component instance corresponding to the
               --  subcomponent S.

               Visit (Corresponding_Instance (S));
               S := Next_Node (S);
            end loop;
         end if;

         Pop_Entity; --  Ada_Root
      end Visit_System_Instance;

      ---------------------------
      -- Visit_Thread_Instance --
      ---------------------------

      procedure Visit_Thread_Instance (E : Node_Id) is
         N : Node_Id;
         F : Node_Id;
      begin
         if Has_Ports (E) then
1104 1105
            if No
                (Get_Handling
1106 1107 1108 1109
                   (Corresponding_Declaration (E),
                    By_Node,
                    H_Ada_Marshallers_Body))
            then
1110 1111 1112 1113 1114
               Set_Handling
                 (Corresponding_Declaration (E),
                  By_Node,
                  H_Ada_Marshallers_Body,
                  E);
1115 1116 1117

               --  Generate marshallers for the Port_Type enumeration

1118 1119 1120 1121
               N :=
                 Message_Comment
                   ("Marshallers for interface type of thread " &
                    Get_Name_String (Name (Identifier (E))));
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
               Append_Node_To_List (N, ADN.Statements (Current_Package));

               --  Marshall procedure

               N := Marshall_Implementation (E);
               Append_Node_To_List (N, ADN.Statements (Current_Package));

               --  Unmarshall procedure

               N := Unmarshall_Implementation (E);
               Append_Node_To_List (N, ADN.Statements (Current_Package));
            end if;
         end if;

         --  The only data that need to be marshalled or unmarshalled
         --  is the data that is meant to be sent between threads
         --  (locally or remotly). So we visit only thread features.

         if not AAU.Is_Empty (Features (E)) then
            F := First_Node (Features (E));

            while Present (F) loop
               if Kind (F) = K_Port_Spec_Instance and then AAN.Is_Data (F) then
                  Visit (Corresponding_Instance (F));
               end if;

               F := Next_Node (F);
            end loop;
         end if;
      end Visit_Thread_Instance;

   end Package_Body;

end Ocarina.Backends.PO_HI_Ada.Marshallers;