ocarina-backends-po_hi_ada-marshallers.adb 38.4 KB
Newer Older
1 2 3 4 5 6 7 8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--                 OCARINA.BACKENDS.PO_HI_ADA.MARSHALLERS                   --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--    Copyright (C) 2006-2009 Telecom ParisTech, 2010-2017 ESA & ISAE.      --
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
--                                                                          --
-- 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
      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);
110
      procedure Visit_Subcomponents_Of is new Visit_Subcomponents_Of_G (Visit);
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

      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
126 127
         N       : Node_Id;
         Profile : constant List_Id := New_List (ADN.K_Parameter_Profile);
128 129 130
      begin
         --  The 'Data' parameter

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

         --  The 'Message' parameter

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

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

         return N;
      end Marshall_Spec;

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

      function Unmarshall_Spec (E : Node_Id) return Node_Id is
160 161 162 163
         Category : constant Component_Category :=
           Get_Category_Of_Component (E);
         N       : Node_Id;
         Profile : constant List_Id := New_List (ADN.K_Parameter_Profile);
164 165 166 167 168 169
      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
170 171 172 173 174 175
            N :=
              Make_Parameter_Specification
                (Make_Defining_Identifier (PN (P_Port)),
                 Extract_Designator
                   (ADN.Port_Enumeration_Node (Backend_Node (Identifier (E)))),
                 Mode_In);
176 177 178 179 180
            Append_Node_To_List (N, Profile);
         end if;

         --  The 'Data' parameter

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

         --  The 'Message' parameter

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

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

         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
237 238
         Category : constant Component_Category :=
           Get_Category_Of_Component (E);
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
      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

276 277 278 279
               N :=
                 Message_Comment
                   ("Marshallers for DATA type " &
                    Get_Name_String (Name (Identifier (E))));
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
               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
297 298 299 300
                 (E,
                  By_Name,
                  H_Ada_Marshallers_Spec,
                  Identifier (E));
301 302 303 304 305 306 307
            end if;
         else
            --  Do the tree bindings only

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

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

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

      procedure Visit_Process_Instance (E : Node_Id) is
324 325 326
         U : constant Node_Id :=
           ADN.Distributed_Application_Unit
             (ADN.Deployment_Node (Backend_Node (Identifier (E))));
327 328 329 330 331 332 333 334 335 336 337 338 339
         P : constant Node_Id := ADN.Entity (U);
      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

340
         Visit_Subcomponents_Of (E);
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

         --  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
      begin
         Push_Entity (Ada_Root);

         --  Visit all the subcomponents of the system

389
         Visit_Subcomponents_Of (E);
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

         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

405 406
            if No
                (Get_Handling
407 408 409 410
                   (Corresponding_Declaration (E),
                    By_Node,
                    H_Ada_Marshallers_Spec))
            then
411 412 413 414 415 416 417 418 419 420
               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))));
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
               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
436 437 438 439 440 441
                  Found : constant Node_Id :=
                    Get_Handling
                      (Corresponding_Declaration (E),
                       By_Node,
                       H_Ada_Marshallers_Spec);
                  BE : constant Node_Id := Backend_Node (Identifier (Found));
442
               begin
443 444 445 446 447 448
                  Bind_AADL_To_Marshall
                    (Identifier (E),
                     ADN.Marshall_Node (BE));
                  Bind_AADL_To_Unmarshall
                    (Identifier (E),
                     ADN.Unmarshall_Node (BE));
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
               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);
486
      procedure Visit_Subcomponents_Of is new Visit_Subcomponents_Of_G (Visit);
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502

      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
503 504 505
         Spec : constant Node_Id :=
           ADN.Marshall_Node (Backend_Node (Identifier (E)));
         N : Node_Id;
506 507 508 509 510 511
      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
512 513 514 515 516 517 518 519 520 521 522
            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);
523 524 525 526 527 528
         else
            declare
               Alternatives : constant List_Id := New_List (ADN.K_List_Id);
               Statements   : List_Id;
               Declarations : List_Id;
               F            : Node_Id;
529
               Has_Data     : Boolean          := False;
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
            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
565 566 567 568 569 570 571 572 573 574 575 576 577
                           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))));
578
                           Append_Node_To_List (N, Statements);
579
                        else
580 581 582
                           Append_Node_To_List
                             (Make_Null_Statement,
                              Statements);
583 584
                        end if;

585 586 587 588 589 590 591 592 593
                        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);
594 595 596 597 598 599
                        Append_Node_To_List (N, Alternatives);
                     end if;

                     F := Next_Node (F);
                  end loop;

600
                  declare
601 602 603 604
                     Declarations : constant List_Id :=
                       New_List (ADN.K_Declaration_List);
                     Elsif_Statements : constant List_Id :=
                       New_List (ADN.K_List_Id);
605
                  begin
606 607 608
                     N :=
                       Make_Used_Package
                         (RU (RU_PolyORB_HI_Generated_Activity));
609
                     Append_Node_To_List (N, Declarations);
610

611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
                     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));
629
                  end;
630 631 632 633 634 635

               else
                  Declarations := New_List (ADN.K_Declaration_List);

                  --  Add a pragma unreferenced for parameters

636 637 638 639 640
                  N :=
                    Make_Pragma_Statement
                      (Pragma_Unreferenced,
                       Make_List_Id
                         (Make_Defining_Identifier (PN (P_Message))));
641 642
                  Append_Node_To_List (N, Declarations);

643 644 645 646
                  N :=
                    Make_Pragma_Statement
                      (Pragma_Unreferenced,
                       Make_List_Id (Make_Defining_Identifier (PN (P_Data))));
647 648
                  Append_Node_To_List (N, Declarations);

649 650 651 652 653
                  N :=
                    Make_Subprogram_Implementation
                      (Spec,
                       Declarations,
                       No_List);
654 655 656 657 658 659 660 661 662 663 664 665
               end if;
            end;
         end if;

         return N;
      end Marshall_Implementation;

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

      function Unmarshall_Implementation (E : Node_Id) return Node_Id is
666 667 668
         Spec : constant Node_Id :=
           ADN.Unmarshall_Node (Backend_Node (Identifier (E)));
         N : Node_Id;
669 670 671 672 673 674
      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
675 676 677 678 679 680 681 682 683 684 685
            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);
686 687 688
         else
            declare
               Alternatives : constant List_Id := New_List (ADN.K_List_Id);
689 690 691 692 693 694
               Declarations : constant List_Id :=
                 New_List (ADN.K_Declaration_List);
               Statements  : List_Id;
               Aggregates  : List_Id;
               Ref_Message : Boolean := False;
               F           : Node_Id;
695 696 697 698 699 700 701 702 703 704 705
            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
706
                     if Kind (F) = K_Port_Spec_Instance and then Is_In (F) then
707 708 709 710
                        --  The record aggregate

                        Aggregates := New_List (ADN.K_Statement_List);

711 712 713 714
                        N :=
                          Make_Component_Association
                            (Make_Defining_Identifier (PN (P_Port)),
                             Extract_Enumerator (F, False));
715 716 717 718 719 720 721 722 723
                        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

724 725 726 727 728 729 730 731
                           N :=
                             Make_Object_Declaration
                               (Defining_Identifier =>
                                  Make_Defining_Identifier
                                    (Map_Ada_Component_Name (F)),
                                Object_Definition =>
                                  Map_Ada_Data_Type_Designator
                                    (Corresponding_Instance (F)));
732 733
                           Append_Node_To_List (N, Declarations);

734 735 736 737 738 739 740 741 742 743 744
                           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))));
745 746 747 748
                           Append_Node_To_List (N, Statements);

                           --  Append the extra aggregate

749 750 751 752 753 754
                           N :=
                             Make_Component_Association
                               (Make_Defining_Identifier
                                  (Map_Ada_Component_Name (F)),
                                Make_Defining_Identifier
                                  (Map_Ada_Component_Name (F)));
755 756 757 758 759 760 761 762 763 764
                           Append_Node_To_List (N, Aggregates);

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

                           Ref_Message := True;
                        end if;

                        --  Assign the port value

765 766 767 768 769 770 771 772
                        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)));
773 774
                        Append_Node_To_List (N, Statements);

775 776 777 778 779 780 781
                        N :=
                          Make_Elsif_Statement
                            (Make_Expression
                               (Make_Defining_Identifier (PN (P_Port)),
                                Op_Equal,
                                Extract_Enumerator (F, False)),
                             Statements);
782 783 784 785 786 787 788 789 790
                        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'

791 792 793 794 795
                     N :=
                       Make_Pragma_Statement
                         (Pragma_Unreferenced,
                          Make_List_Id
                            (Make_Defining_Identifier (PN (P_Message))));
796 797 798
                     Append_Node_To_List (N, Declarations);
                  end if;

799 800 801
                  declare
                     --  Declarations : constant List_Id
                     --    := New_List (ADN.K_Declaration_List);
802 803
                     Elsif_Statements : constant List_Id :=
                       New_List (ADN.K_List_Id);
804
                  begin
805 806 807
                     N :=
                       Make_Used_Package
                         (RU (RU_PolyORB_HI_Generated_Activity));
808 809
                     Append_Node_To_List (N, Declarations);

810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
                     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));
828
                  end;
829 830 831 832

               else
                  --  Add a pragma unreferenced for parameters

833 834 835 836
                  N :=
                    Make_Pragma_Statement
                      (Pragma_Unreferenced,
                       Make_List_Id (Make_Defining_Identifier (PN (P_Port))));
837 838
                  Append_Node_To_List (N, Declarations);

839 840 841 842 843
                  N :=
                    Make_Pragma_Statement
                      (Pragma_Unreferenced,
                       Make_List_Id
                         (Make_Defining_Identifier (PN (P_Message))));
844 845
                  Append_Node_To_List (N, Declarations);

846 847 848 849
                  N :=
                    Make_Pragma_Statement
                      (Pragma_Unreferenced,
                       Make_List_Id (Make_Defining_Identifier (PN (P_Data))));
850 851
                  Append_Node_To_List (N, Declarations);

852 853 854 855 856
                  N :=
                    Make_Subprogram_Implementation
                      (Spec,
                       Declarations,
                       No_List);
857 858 859 860 861 862 863 864 865 866 867 868 869
               end if;
            end;
         end if;

         return N;
      end Unmarshall_Implementation;

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

      function Marshallers_Instantiation (E : Node_Id) return Node_Id is
      begin
870
         return Make_Package_Instantiation
yoogx's avatar
yoogx committed
871 872 873 874 875
             (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)));
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
      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
910 911
         Category : constant Component_Category :=
           Get_Category_Of_Component (E);
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
      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

949 950 951 952
               N :=
                 Message_Comment
                   ("Marshallers for DATA type " &
                    Get_Name_String (Name (Identifier (E))));
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
               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
973 974 975 976
                 (E,
                  By_Name,
                  H_Ada_Marshallers_Body,
                  Identifier (E));
977 978 979 980 981 982 983 984 985
            end if;
         end if;
      end Visit_Data_Instance;

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

      procedure Visit_Process_Instance (E : Node_Id) is
986 987 988
         U : constant Node_Id :=
           ADN.Distributed_Application_Unit
             (ADN.Deployment_Node (Backend_Node (Identifier (E))));
989 990 991 992 993 994 995 996 997 998 999 1000 1001
         P : constant Node_Id := ADN.Entity (U);
      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

1002
         Visit_Subcomponents_Of (E);
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 1043 1044 1045 1046 1047 1048 1049 1050

         --  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
      begin
         Push_Entity (Ada_Root);

         --  Visit all the subcomponents of the system

1051
         Visit_Subcomponents_Of (E);
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064

         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
1065 1066
            if No
                (Get_Handling
1067 1068 1069 1070
                   (Corresponding_Declaration (E),
                    By_Node,
                    H_Ada_Marshallers_Body))
            then
1071 1072 1073 1074 1075
               Set_Handling
                 (Corresponding_Declaration (E),
                  By_Node,
                  H_Ada_Marshallers_Body,
                  E);
1076 1077 1078

               --  Generate marshallers for the Port_Type enumeration

1079 1080 1081 1082
               N :=
                 Message_Comment
                   ("Marshallers for interface type of thread " &
                    Get_Name_String (Name (Identifier (E))));
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
               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;