ocarina-backends-po_hi_c-marshallers.adb 72.2 KB
Newer Older
1
2
3
4
5
6
7
8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--                  OCARINA.BACKENDS.PO_HI_C.MARSHALLERS                    --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--    Copyright (C) 2008-2009 Telecom ParisTech, 2010-2014 ESA & ISAE.      --
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
--                                                                          --
-- Ocarina  is free software;  you  can  redistribute  it and/or  modify    --
-- it under terms of the GNU General Public License as published by the     --
-- Free Software Foundation; either version 2, or (at your option) any      --
-- later version. Ocarina is distributed  in  the  hope  that it will be    --
-- useful, but WITHOUT ANY WARRANTY;  without even the implied warranty of  --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General --
-- Public License for more details. You should have received  a copy of the --
-- GNU General Public License distributed with Ocarina; see file COPYING.   --
-- If not, write to the Free Software Foundation, 51 Franklin Street, Fifth --
-- Floor, Boston, MA 02111-1301, USA.                                       --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable to be   --
-- covered  by the  GNU  General  Public  License. This exception does not  --
-- however invalidate  any other reasons why the executable file might be   --
-- covered by the GNU Public License.                                       --
--                                                                          --
jhugues's avatar
jhugues committed
29
30
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
31
32
33
--                                                                          --
------------------------------------------------------------------------------

34
35
with Namet; use Namet;

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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.C_Tree.Nutils;
with Ocarina.Backends.C_Tree.Nodes;
with Ocarina.Backends.C_Values;
with Ocarina.Backends.C_Common.Mapping;
with Ocarina.Backends.PO_HI_C.Runtime;

package body Ocarina.Backends.PO_HI_C.Marshallers is

   use Ocarina.ME_AADL;
   use Ocarina.ME_AADL.AADL_Instances.Nodes;
   use Ocarina.ME_AADL.AADL_Instances.Entities;
   use Ocarina.Backends.Utils;
   use Ocarina.Backends.Properties;
   use Ocarina.Backends.Messages;
   use Ocarina.Backends.C_Tree.Nutils;
   use Ocarina.Backends.C_Common.Mapping;
   use Ocarina.Backends.PO_HI_C.Runtime;

   package AAU renames Ocarina.ME_AADL.AADL_Instances.Nutils;
   package CTN renames Ocarina.Backends.C_Tree.Nodes;
   package CTU renames Ocarina.Backends.C_Tree.Nutils;
   package CV renames Ocarina.Backends.C_Values;

   Marshaller_Request_Spec   : Node_Id;
   Unmarshaller_Request_Spec : Node_Id;

70
71
   Marshaller_Asn1_Request_Spec   : Node_Id;
   Unmarshaller_Asn1_Request_Spec : Node_Id;
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
   -----------------
   -- Header_File --
   -----------------

   package body Header_File 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_Type_Spec (E : Node_Id) return Node_Id;
      function Unmarshall_Type_Spec (E : Node_Id) return Node_Id;
      function Marshall_Request_Spec (E : Node_Id) return Node_Id;
      function Unmarshall_Request_Spec (E : Node_Id) return Node_Id;

      ---------------------------
      -- Marshall_Request_Spec --
      ---------------------------

      function Marshall_Request_Spec (E : Node_Id) return Node_Id is
         N          : Node_Id;
         Parameters : constant List_Id := New_List (CTN.K_Parameter_List);
      begin
         Append_Node_To_List
           (Make_Parameter_Specification
101
102
103
              (Defining_Identifier =>
                 Make_Defining_Identifier (PN (P_Request)),
               Parameter_Type => Make_Pointer_Type (RE (RE_Request_T))),
104
105
106
107
            Parameters);

         Append_Node_To_List
           (Make_Parameter_Specification
108
109
110
              (Defining_Identifier =>
                 Make_Defining_Identifier (PN (P_Message)),
               Parameter_Type => Make_Pointer_Type (RE (RE_Msg_T))),
111
112
113
114
            Parameters);

         Append_Node_To_List
           (Make_Parameter_Specification
115
116
              (Defining_Identifier => Make_Defining_Identifier (PN (P_Offset)),
               Parameter_Type      => Make_Pointer_Type (RE (RE_Uint32_T))),
117
118
            Parameters);

119
120
121
122
123
124
         N :=
           Make_Function_Specification
             (Defining_Identifier =>
                Map_C_Marshaller_Subprogram (E, Is_Request => True),
              Parameters  => Parameters,
              Return_Type => New_Node (CTN.K_Void));
125
126
127
128
129
130
131
132
133
134
135
136
137
         return N;
      end Marshall_Request_Spec;

      -----------------------------
      -- Unmarshall_Request_Spec --
      -----------------------------

      function Unmarshall_Request_Spec (E : Node_Id) return Node_Id is
         N          : Node_Id;
         Parameters : constant List_Id := New_List (CTN.K_Parameter_List);
      begin
         Append_Node_To_List
           (Make_Parameter_Specification
138
139
140
              (Defining_Identifier =>
                 Make_Defining_Identifier (PN (P_Request)),
               Parameter_Type => Make_Pointer_Type (RE (RE_Request_T))),
141
142
143
144
            Parameters);

         Append_Node_To_List
           (Make_Parameter_Specification
145
146
147
              (Defining_Identifier =>
                 Make_Defining_Identifier (PN (P_Message)),
               Parameter_Type => Make_Pointer_Type (RE (RE_Msg_T))),
148
149
150
151
            Parameters);

         Append_Node_To_List
           (Make_Parameter_Specification
152
153
              (Defining_Identifier => Make_Defining_Identifier (PN (P_Offset)),
               Parameter_Type      => Make_Pointer_Type (RE (RE_Uint32_T))),
154
155
            Parameters);

156
157
158
159
160
161
162
163
164
         N :=
           Make_Function_Specification
             (Defining_Identifier =>
                Map_C_Marshaller_Subprogram
                  (E,
                   Is_Request    => True,
                   Is_Unmarshall => True),
              Parameters  => Parameters,
              Return_Type => New_Node (CTN.K_Void));
165
166
167
168
169
170
171
172
173
174
175
176
177
178
         return N;
      end Unmarshall_Request_Spec;

      ------------------------
      -- Marshall_Type_Spec --
      ------------------------

      function Marshall_Type_Spec (E : Node_Id) return Node_Id is
         N          : Node_Id;
         Parameters : constant List_Id := New_List (CTN.K_Parameter_List);
      begin

         Append_Node_To_List
           (Make_Parameter_Specification
179
180
              (Defining_Identifier => Make_Defining_Identifier (PN (P_Value)),
               Parameter_Type      => Map_C_Defining_Identifier (E)),
181
182
183
184
            Parameters);

         Append_Node_To_List
           (Make_Parameter_Specification
185
186
187
              (Defining_Identifier =>
                 Make_Defining_Identifier (PN (P_Message)),
               Parameter_Type => Make_Pointer_Type (RE (RE_Msg_T))),
188
189
190
191
            Parameters);

         Append_Node_To_List
           (Make_Parameter_Specification
192
193
              (Defining_Identifier => Make_Defining_Identifier (PN (P_Offset)),
               Parameter_Type      => Make_Pointer_Type (RE (RE_Uint32_T))),
194
195
            Parameters);

196
197
198
199
200
         N :=
           Make_Function_Specification
             (Defining_Identifier => Map_C_Marshaller_Subprogram (E),
              Parameters          => Parameters,
              Return_Type         => New_Node (CTN.K_Void));
201
202
203
204
205
206
207
208
209
210
211
212
213
214
         return N;
      end Marshall_Type_Spec;

      --------------------------
      -- Unmarshall_Type_Spec --
      --------------------------

      function Unmarshall_Type_Spec (E : Node_Id) return Node_Id is
         N          : Node_Id;
         Parameters : constant List_Id := New_List (CTN.K_Parameter_List);
      begin

         Append_Node_To_List
           (Make_Parameter_Specification
215
216
217
              (Defining_Identifier => Make_Defining_Identifier (PN (P_Value)),
               Parameter_Type      =>
                 Make_Pointer_Type (Map_C_Defining_Identifier (E))),
218
219
220
221
            Parameters);

         Append_Node_To_List
           (Make_Parameter_Specification
222
223
224
              (Defining_Identifier =>
                 Make_Defining_Identifier (PN (P_Message)),
               Parameter_Type => Make_Pointer_Type (RE (RE_Msg_T))),
225
226
227
228
            Parameters);

         Append_Node_To_List
           (Make_Parameter_Specification
229
230
              (Defining_Identifier => Make_Defining_Identifier (PN (P_Offset)),
               Parameter_Type      => Make_Pointer_Type (RE (RE_Uint32_T))),
231
232
            Parameters);

233
234
235
236
237
238
         N :=
           Make_Function_Specification
             (Defining_Identifier =>
                Map_C_Marshaller_Subprogram (E, Is_Unmarshall => True),
              Parameters  => Parameters,
              Return_Type => New_Node (CTN.K_Void));
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
         return N;
      end Unmarshall_Type_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
274
275
         Category : constant Component_Category :=
           Get_Category_Of_Component (E);
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
      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;
         S : Node_Id;
      begin
julien.delange's avatar
julien.delange committed
306
307
         Add_Include (RH (RH_Types));

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
         if No (Get_Handling (E, By_Name, H_C_Marshall_Spec)) then
            N := Marshall_Type_Spec (E);
            Set_Handling (E, By_Name, H_C_Marshall_Spec, N);
            Append_Node_To_List (N, CTN.Declarations (Current_File));
         end if;

         if No (Get_Handling (E, By_Name, H_C_Unmarshall_Spec)) then
            N := Unmarshall_Type_Spec (E);
            Set_Handling (E, By_Name, H_C_Unmarshall_Spec, N);
            Append_Node_To_List (N, CTN.Declarations (Current_File));
         end if;

         Bind_AADL_To_Marshaller
           (Identifier (E),
            Get_Handling (E, By_Name, H_C_Marshall_Spec));

         Bind_AADL_To_Unmarshaller
           (Identifier (E),
            Get_Handling (E, By_Name, H_C_Unmarshall_Spec));

         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.

334
335
336
               if AAU.Is_Data (Corresponding_Instance (S)) then
                  Visit (Corresponding_Instance (S));
               end if;
337
338
339
340
341
342
343
344
345
346
               S := Next_Node (S);
            end loop;
         end if;
      end Visit_Data_Instance;

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

      procedure Visit_Process_Instance (E : Node_Id) is
347
348
349
350
351
352
         U : constant Node_Id :=
           CTN.Distributed_Application_Unit
             (CTN.Naming_Node (Backend_Node (Identifier (E))));
         P          : constant Node_Id := CTN.Entity (U);
         S          : Node_Id;
         Parameters : List_Id;
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
      begin
         Push_Entity (P);
         Push_Entity (U);
         Set_Marshallers_Header;

         --  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;

         --  Make the main marshall_request function

         Parameters := New_List (CTN.K_Parameter_List);
         Append_Node_To_List
           (Make_Parameter_Specification
381
382
383
              (Defining_Identifier =>
                 Make_Defining_Identifier (PN (P_Request)),
               Parameter_Type => Make_Pointer_Type (RE (RE_Request_T))),
384
385
386
387
            Parameters);

         Append_Node_To_List
           (Make_Parameter_Specification
388
389
390
              (Defining_Identifier =>
                 Make_Defining_Identifier (PN (P_Message)),
               Parameter_Type => Make_Pointer_Type (RE (RE_Msg_T))),
391
392
            Parameters);

393
394
395
396
397
398
399
400
         Marshaller_Request_Spec :=
           Make_Function_Specification
             (Defining_Identifier => RE (RE_Marshall_Request),
              Parameters          => Parameters,
              Return_Type         => New_Node (CTN.K_Void));
         Append_Node_To_List
           (Marshaller_Request_Spec,
            CTN.Declarations (Current_File));
401
402
403
404
405
406

         --  Make the main unmarshall_request function

         Parameters := New_List (CTN.K_Parameter_List);
         Append_Node_To_List
           (Make_Parameter_Specification
407
408
409
              (Defining_Identifier =>
                 Make_Defining_Identifier (PN (P_Request)),
               Parameter_Type => Make_Pointer_Type (RE (RE_Request_T))),
410
411
412
413
            Parameters);

         Append_Node_To_List
           (Make_Parameter_Specification
414
415
416
              (Defining_Identifier =>
                 Make_Defining_Identifier (PN (P_Message)),
               Parameter_Type => Make_Pointer_Type (RE (RE_Msg_T))),
417
418
            Parameters);

419
420
421
422
423
424
425
426
         Unmarshaller_Request_Spec :=
           Make_Function_Specification
             (Defining_Identifier => RE (RE_Unmarshall_Request),
              Parameters          => Parameters,
              Return_Type         => New_Node (CTN.K_Void));
         Append_Node_To_List
           (Unmarshaller_Request_Spec,
            CTN.Declarations (Current_File));
427

428
         if Use_ASN1_Deployment then
429
            --  Make the main marshall_asn1_request function
430
431
432

            Parameters := New_List (CTN.K_Parameter_List);
            Append_Node_To_List
433
434
435
436
437
              (Make_Parameter_Specification
                 (Defining_Identifier =>
                    Make_Defining_Identifier (PN (P_Request)),
                  Parameter_Type => Make_Pointer_Type (RE (RE_Request_T))),
               Parameters);
438
439

            Append_Node_To_List
440
441
442
443
444
445
446
447
448
449
450
451
452
              (Make_Parameter_Specification
                 (Defining_Identifier => Make_Defining_Identifier (PN (P_Pkt)),
                  Parameter_Type => Make_Pointer_Type (RE (RE_Asn1_Pkt_T))),
               Parameters);

            Marshaller_Asn1_Request_Spec :=
              Make_Function_Specification
                (Defining_Identifier => RE (RE_Marshall_Asn1_Request),
                 Parameters          => Parameters,
                 Return_Type         => New_Node (CTN.K_Void));
            Append_Node_To_List
              (Marshaller_Asn1_Request_Spec,
               CTN.Declarations (Current_File));
453
454
455
456
457

            --  Make the main unmarshall_request function

            Parameters := New_List (CTN.K_Parameter_List);
            Append_Node_To_List
458
459
460
461
462
              (Make_Parameter_Specification
                 (Defining_Identifier =>
                    Make_Defining_Identifier (PN (P_Request)),
                  Parameter_Type => Make_Pointer_Type (RE (RE_Request_T))),
               Parameters);
463
464

            Append_Node_To_List
465
466
467
468
469
470
471
472
473
474
475
476
477
              (Make_Parameter_Specification
                 (Defining_Identifier => Make_Defining_Identifier (PN (P_Pkt)),
                  Parameter_Type => Make_Pointer_Type (RE (RE_Asn1_Pkt_T))),
               Parameters);

            Unmarshaller_Asn1_Request_Spec :=
              Make_Function_Specification
                (Defining_Identifier => RE (RE_Unmarshall_Asn1_Request),
                 Parameters          => Parameters,
                 Return_Type         => New_Node (CTN.K_Void));
            Append_Node_To_List
              (Unmarshaller_Asn1_Request_Spec,
               CTN.Declarations (Current_File));
478
479
         end if;

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
         --  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 (C_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; --  C_Root
      end Visit_System_Instance;

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

      procedure Visit_Thread_Instance (E : Node_Id) is
         Call_Seq : Node_Id;
         Spg_Call : Node_Id;
         F        : Node_Id;
         N        : Node_Id;
      begin
         if not AAU.Is_Empty (Features (E)) then
            F := First_Node (Features (E));

            while Present (F) loop
557
558
559
               if Kind (F) = K_Port_Spec_Instance
                 and then Ocarina.ME_AADL.AADL_Instances.Nodes.Is_Data (F)
               then
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
631
632
633
                  Visit (Corresponding_Instance (F));
               end if;

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

         --  Visit all the call sequences of the thread

         if not AAU.Is_Empty (Calls (E)) then
            Call_Seq := First_Node (Calls (E));

            while Present (Call_Seq) loop
               --  For each call sequence visit all the called
               --  subprograms.

               if not AAU.Is_Empty (Subprogram_Calls (Call_Seq)) then
                  Spg_Call := First_Node (Subprogram_Calls (Call_Seq));

                  while Present (Spg_Call) loop
                     Visit (Corresponding_Instance (Spg_Call));

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

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

         if Has_Ports (E) then
            F := First_Node (Features (E));
            while Present (F) loop
               if Kind (F) = K_Port_Spec_Instance then
                  N := Marshall_Request_Spec (F);
                  Set_Handling (F, By_Name, H_C_Marshall_Spec, N);
                  Append_Node_To_List (N, CTN.Declarations (Current_File));

                  N := Unmarshall_Request_Spec (F);
                  Set_Handling (F, By_Name, H_C_Unmarshall_Spec, N);
                  Append_Node_To_List (N, CTN.Declarations (Current_File));

                  Bind_AADL_To_Marshaller
                    (Identifier (F),
                     Get_Handling (F, By_Name, H_C_Marshall_Spec));

                  Bind_AADL_To_Unmarshaller
                    (Identifier (F),
                     Get_Handling (F, By_Name, H_C_Unmarshall_Spec));
               end if;
               F := Next_Node (F);
            end loop;
         end if;

      end Visit_Thread_Instance;

   end Header_File;

   -----------------
   -- Source_File --
   -----------------

   package body Source_File 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_Data_Instance (E : Node_Id);
      procedure Visit_Subprogram_Instance (E : Node_Id);
      function Get_Marshall_Function_Name (E : Node_Id) return Node_Id;
      function Get_Unmarshall_Function_Name (E : Node_Id) return Node_Id;

634
635
636
637
      Asn1_Marshall_Alternatives   : List_Id;
      Asn1_Unmarshall_Alternatives : List_Id;
      Marshall_Alternatives        : List_Id;
      Unmarshall_Alternatives      : List_Id;
638
639
640
641
642
643
644
645
646
647
648
649

      -----------------------------
      -- Make_Marshall_Type_Body --
      -----------------------------

      function Make_Marshall_Type_Body (E : Node_Id) return Node_Id is
         Data_Representation : Supported_Data_Representation;
         C                   : Node_Id;
         N                   : Node_Id;
         Declarations        : List_Id;
         Statements          : List_Id;
         Parameters          : List_Id;
650
651
         Marshall_Spec       : constant Node_Id :=
           CTN.Marshaller_Node (Backend_Node (Identifier (E)));
652
653
654
655
      begin
         --  Create the marshall function for this type

         Declarations := New_List (CTN.K_Declaration_List);
656
         Statements   := New_List (CTN.K_Statement_List);
657
658
659
660

         Data_Representation := Get_Data_Representation (E);

         case Data_Representation is
661
            when Data_With_Accessors | Data_Struct =>
662
663
664
665
666
667
               --  For structures, the marshaller calls the
               --  marshallers of each enclosed member.

               C := First_Node (Subcomponents (E));

               while Present (C) loop
668
669
                  if AAU.Is_Data (Corresponding_Instance (C)) then
                     Visit (Corresponding_Instance (C));
670

671
672
673
                     Parameters := New_List (CTN.K_Parameter_List);
                     Append_Node_To_List
                       (Make_Member_Designator
674
675
                          (Aggregate_Name =>
                             Make_Defining_Identifier (PN (P_Value)),
676
677
678
679
680
681
682
683
684
685
                           Defining_Identifier =>
                             Map_C_Defining_Identifier (C),
                           Is_Pointer => False),
                        Parameters);
                     Append_Node_To_List
                       (Make_Defining_Identifier (PN (P_Message)),
                        Parameters);
                     Append_Node_To_List
                       (Make_Defining_Identifier (PN (P_Offset)),
                        Parameters);
686
687
688
689
690
                     N :=
                       Make_Call_Profile
                         (Get_Marshall_Function_Name
                            (Corresponding_Instance (C)),
                          Parameters);
691
692
                     Append_Node_To_List (N, Statements);
                  end if;
693
694
695
                  C := Next_Node (C);
               end loop;

696
            when Data_Array | Data_String =>
697
698
699
700
701
702
703
               Parameters := New_List (CTN.K_Parameter_List);
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Value)),
                  Parameters);
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Message)),
                  Parameters);
704
               Append_Node_To_List (CTU.Get_Data_Size (E), Parameters);
705
706
707
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Offset)),
                  Parameters);
708
               N := Make_Call_Profile (RE (RE_Marshall_Array), Parameters);
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
               Append_Node_To_List (N, Statements);

            when Data_Enum =>
               --  In C, an enumeration constant is equivalent to an
               --  int constant, therefore we marshall the value as an
               --  __po_hi_int32 parameter.

               Parameters := New_List (CTN.K_Parameter_List);
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Value)),
                  Parameters);
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Message)),
                  Parameters);
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Offset)),
                  Parameters);
726
               N := Make_Call_Profile (RE (RE_Marshall_Int32), Parameters);
727
728
729
730
731
732
733
734
735
736
737
               Append_Node_To_List (N, Statements);

            when Data_None =>
               Parameters := New_List (CTN.K_Parameter_List);
               Append_Node_To_List
                 (Make_Variable_Address
                    (Make_Defining_Identifier (PN (P_Value))),
                  Parameters);
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Message)),
                  Parameters);
738
               Append_Node_To_List (CTU.Get_Data_Size (E), Parameters);
739
740
741
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Offset)),
                  Parameters);
742
               N := Make_Call_Profile (RE (RE_Marshall_Array), Parameters);
743
744
745
746
747
748
749
750
751
752
753
754
755
               Append_Node_To_List (N, Statements);

            when others =>
               Parameters := New_List (CTN.K_Parameter_List);
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Value)),
                  Parameters);
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Message)),
                  Parameters);
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Offset)),
                  Parameters);
756
757
758
759
               N :=
                 Make_Call_Profile
                   (Get_Marshall_Function_Name (E),
                    Parameters);
760
761
762
               Append_Node_To_List (N, Statements);
         end case;

763
764
765
766
767
         N :=
           Make_Function_Implementation
             (Marshall_Spec,
              Declarations,
              Statements);
768
769
770
771
772
773
774
775
         return N;
      end Make_Marshall_Type_Body;

      -------------------------------
      -- Make_Unmarshall_Type_Body --
      -------------------------------

      function Make_Unmarshall_Type_Body (E : Node_Id) return Node_Id is
776
777
778
779
780
781
782
783
         Data_Representation : Supported_Data_Representation;
         N                   : Node_Id;
         C                   : Node_Id;
         Declarations        : List_Id;
         Statements          : List_Id;
         Parameters          : List_Id;
         Unmarshall_Spec     : constant Node_Id :=
           CTN.Unmarshaller_Node (Backend_Node (Identifier (E)));
784
785
786
787
      begin
         --  Create the unmarshall function for this type

         Declarations := New_List (CTN.K_Declaration_List);
788
         Statements   := New_List (CTN.K_Statement_List);
789
790
791
792

         Data_Representation := Get_Data_Representation (E);

         case Data_Representation is
793
            when Data_With_Accessors | Data_Struct =>
794
795
796
797

               C := First_Node (Subcomponents (E));

               while Present (C) loop
798
799
800
801
802
803
804
                  if AAU.Is_Data (Corresponding_Instance (C)) then
                     Visit (Corresponding_Instance (C));

                     Parameters := New_List (CTN.K_Parameter_List);
                     Append_Node_To_List
                       (Make_Variable_Address
                          (Make_Member_Designator
805
806
                             (Aggregate_Name =>
                                Make_Defining_Identifier (PN (P_Value)),
807
808
809
810
811
812
813
814
815
816
                              Defining_Identifier =>
                                Map_C_Defining_Identifier (C),
                              Is_Pointer => True)),
                        Parameters);
                     Append_Node_To_List
                       (Make_Defining_Identifier (PN (P_Message)),
                        Parameters);
                     Append_Node_To_List
                       (Make_Defining_Identifier (PN (P_Offset)),
                        Parameters);
817
818
819
820
821
                     N :=
                       Make_Call_Profile
                         (Get_Unmarshall_Function_Name
                            (Corresponding_Instance (C)),
                          Parameters);
822
823
                     Append_Node_To_List (N, Statements);
                  end if;
824
825
826
                  C := Next_Node (C);
               end loop;

827
            when Data_Array | Data_String =>
828
829
830
831
832
833
834
               Parameters := New_List (CTN.K_Parameter_List);
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Value)),
                  Parameters);
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Message)),
                  Parameters);
835
               Append_Node_To_List (CTU.Get_Data_Size (E), Parameters);
836
837
838
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Offset)),
                  Parameters);
839
               N := Make_Call_Profile (RE (RE_Unmarshall_Array), Parameters);
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
               Append_Node_To_List (N, Statements);

            when Data_Enum =>
               --  In C, an enumeration constant is equivalent to an
               --  int constant, therefore we unmarshall the value as
               --  an __po_hi_int32 parameter.

               Parameters := New_List (CTN.K_Parameter_List);
               Append_Node_To_List
                 (Make_Type_Conversion
                    (Make_Pointer_Type (RE (RE_Int32_T)),
                     Make_Defining_Identifier (PN (P_Value))),
                  Parameters);
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Message)),
                  Parameters);
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Offset)),
                  Parameters);
859
               N := Make_Call_Profile (RE (RE_Unmarshall_Int32), Parameters);
860
861
862
863
864
               Append_Node_To_List (N, Statements);

            when Data_None =>
               Parameters := New_List (CTN.K_Parameter_List);
               Append_Node_To_List
865
                 (Make_Defining_Identifier (PN (P_Value)),
866
867
868
869
                  Parameters);
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Message)),
                  Parameters);
870
               Append_Node_To_List (CTU.Get_Data_Size (E), Parameters);
871
872
873
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Offset)),
                  Parameters);
874
               N := Make_Call_Profile (RE (RE_Unmarshall_Array), Parameters);
875
876
877
878
879
880
881
882
883
884
885
886
887
               Append_Node_To_List (N, Statements);

            when others =>
               Parameters := New_List (CTN.K_Parameter_List);
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Value)),
                  Parameters);
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Message)),
                  Parameters);
               Append_Node_To_List
                 (Make_Defining_Identifier (PN (P_Offset)),
                  Parameters);
888
889
890
891
               N :=
                 Make_Call_Profile
                   (Get_Unmarshall_Function_Name (E),
                    Parameters);
892
893
894
               Append_Node_To_List (N, Statements);
         end case;

895
896
897
898
899
         N :=
           Make_Function_Implementation
             (Unmarshall_Spec,
              Declarations,
              Statements);
900
901
902
903
904
905
906
907
         return N;
      end Make_Unmarshall_Type_Body;

      --------------------------------
      -- Get_Marshall_Function_Name --
      --------------------------------

      function Get_Marshall_Function_Name (E : Node_Id) return Node_Id is
908
909
910
911
912
913
914
915
916
         Data_Representation : constant Supported_Data_Representation :=
           Get_Data_Representation (E);
         Data_Size        : constant Size_Type          := Get_Data_Size (E);
         Actual_Data_Size : constant Unsigned_Long_Long :=
           To_Bytes (Data_Size);
         Number_Representation : constant Supported_Number_Representation :=
           Get_Number_Representation (E);
         Is_Signed : constant Boolean :=
           (Number_Representation = Representation_Signed);
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983

      begin
         case Data_Representation is
            when Data_Boolean =>
               return (RE (RE_Marshall_Bool));

            when Data_Character =>
               return (RE (RE_Marshall_Char));

            when Data_Integer =>
               if Data_Size.S = 0 then
                  return (RE (RE_Marshall_Int));

               elsif Actual_Data_Size = 1 and then Is_Signed then
                  return (RE (RE_Marshall_Int8));

               elsif Actual_Data_Size = 1 and then not Is_Signed then
                  return (RE (RE_Marshall_Uint8));

               elsif Actual_Data_Size = 2 and then Is_Signed then
                  return (RE (RE_Marshall_Int16));

               elsif Actual_Data_Size = 2 and then not Is_Signed then
                  return (RE (RE_Marshall_Uint16));

               elsif Actual_Data_Size = 4 and then Is_Signed then
                  return (RE (RE_Marshall_Int32));

               elsif Actual_Data_Size = 4 and then not Is_Signed then
                  return (RE (RE_Marshall_Uint32));

               elsif Actual_Data_Size = 8 and then Is_Signed then
                  return (RE (RE_Marshall_Int64));

               elsif Actual_Data_Size = 8 and then not Is_Signed then
                  return (RE (RE_Marshall_Uint64));

               else
                  Display_Error ("Unsupported data size", Fatal => True);
                  return (No_Node);
               end if;

            when Data_Float | Data_Fixed =>
               if Data_Size.S = 0 then
                  return (RE (RE_Marshall_Float));

               elsif Actual_Data_Size = 4 then
                  return (RE (RE_Marshall_Float32));

               elsif Actual_Data_Size = 8 then
                  return (RE (RE_Marshall_Float64));

               else
                  Display_Error ("Unsupported data size", Fatal => True);
                  return (No_Node);
               end if;

            when others =>
               return (Map_C_Marshaller_Subprogram (E));
         end case;
      end Get_Marshall_Function_Name;

      ----------------------------------
      -- Get_Unmarshall_Function_Name --
      ----------------------------------

      function Get_Unmarshall_Function_Name (E : Node_Id) return Node_Id is
984
985
986
987
988
989
990
991
992
         Data_Representation : constant Supported_Data_Representation :=
           Get_Data_Representation (E);
         Data_Size        : constant Size_Type          := Get_Data_Size (E);
         Actual_Data_Size : constant Unsigned_Long_Long :=
           To_Bytes (Data_Size);
         Number_Representation : constant Supported_Number_Representation :=
           Get_Number_Representation (E);
         Is_Signed : constant Boolean :=
           (Number_Representation = Representation_Signed);
993
994
995
996
997
998
999
1000

      begin
         case Data_Representation is
            when Data_Boolean =>
               return (RE (RE_Unmarshall_Bool));

            when Data_Character =>
               return (RE (RE_Unmarshall_Char));