ocarina-backends-c_common-mapping.adb 146 KB
Newer Older
1
2
3
4
5
6
7
8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--    O C A R I N A . B A C K E N D S . C _ C O M M O N . M A P P I N G     --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--    Copyright (C) 2008-2009 Telecom ParisTech, 2010-2019 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
49
50
51
52
53
54
with Utils; use Utils;

with Ocarina.Backends.Messages;
with Ocarina.Backends.Utils;
with Ocarina.Backends.C_Values;
with Ocarina.Backends.C_Tree.Nodes;
with Ocarina.Backends.C_Tree.Nutils;
with Ocarina.Backends.PO_HI_C.Runtime;
with Ocarina.Backends.POK_C.Runtime;
with Ocarina.Backends.POK_C;

with Ocarina.Instances.Queries;

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

55
56
with Ocarina.Backends.C_Common.BA;

57
58
package body Ocarina.Backends.C_Common.Mapping is

59
   use Ocarina.Namet;
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
   use Ocarina.Backends.Messages;
   use Ocarina.Backends.Utils;
   use Ocarina.Backends.C_Values;
   use Ocarina.Backends.C_Tree.Nodes;
   use Ocarina.Backends.C_Tree.Nutils;
   use Ocarina.Backends.PO_HI_C.Runtime;
   use Ocarina.Backends.POK_C.Runtime;
   use Ocarina.Backends.POK_C;
   use Ocarina.Instances.Queries;

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

   package AAN renames Ocarina.ME_AADL.AADL_Tree.Nodes;
   package ATU renames Ocarina.ME_AADL.AADL_Tree.Nutils;
--   package ATE renames Ocarina.ME_AADL.AADL_Tree.Entities;
   package AIN renames Ocarina.ME_AADL.AADL_Instances.Nodes;
   package AINU renames Ocarina.ME_AADL.AADL_Instances.Nutils;
   package CTN renames Ocarina.Backends.C_Tree.Nodes;
   package CTU renames Ocarina.Backends.C_Tree.Nutils;
   package PKR renames Ocarina.Backends.POK_C.Runtime;
   package PHR renames Ocarina.Backends.PO_HI_C.Runtime;
84
   package CCBA renames Ocarina.Backends.C_Common.BA;
85
86
87
88
89
90

   ---------------------------
   -- Call_Remote_Functions --
   ---------------------------

   procedure Call_Remote_Functions
91
92
93
94
     (Caller_Thread : Node_Id;
      Spg_Call      : Node_Id;
      Declarations  : List_Id;
      Statements    : List_Id)
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
   is
      procedure Check_Connection_Consistency (F : Node_Id);
      --  Verifies that the feature F is connected to at least one
      --  destination.

      procedure Call_Remote_Functions
        (Root_F         : Node_Id;
         Intermediate_F : Node_Id);
      --  This recursive procedure will unwind recursively all the
      --  destinations of the Root_F feature until it finds a
      --  subprogram. If the remote subprogram is found, add a call
      --  to the subprogram call table (if necessary) and add a
      --  parameter association corresponding to Root_F the the
      --  call to this subprogram. At the top level call to
      --  Call_Remote_Subprograms, Intermediate_F = Root_F.

      procedure Update_Remote_Calls
        (Remote_Spg  : Node_Id;
         Param_Value : Node_Id);
      --  If the call to Spg has not been added yet, create
      --  it. Adds a parameter association Param_Name =>
      --  Param_Value to the call profile.

      Call_List : constant List_Id := New_List (CTN.K_Statement_List);
      --  List that contains the calls to all remote subprogram
      --  connected to Spg.

      ----------------------------------
      -- Check_Connection_Consistency --
      ----------------------------------

      procedure Check_Connection_Consistency (F : Node_Id) is
      begin
         if AINU.Length (Destinations (F)) = 0 then
            Display_Located_Error
              (AIN.Loc (F),
               "This feature does not have any destinations",
               Fatal => True);
         end if;
      end Check_Connection_Consistency;

      ---------------------------
      -- Call_Remote_Functions --
      ---------------------------

      procedure Call_Remote_Functions
        (Root_F         : Node_Id;
         Intermediate_F : Node_Id)
      is
         Destination_F : Node_Id;
         C             : Node_Id;
      begin
         --  Root F has to be a parameter instance

         pragma Assert (Kind (Root_F) = K_Parameter_Instance);

         --  The container of the subprogram containing F has to be
         --  necessarily a Thread.

154
155
156
157
158
         pragma Assert
           (AINU.Is_Thread
              (Parent_Component
                 (Parent_Sequence
                    (Parent_Subcomponent (Parent_Component (Root_F))))));
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225

         --  Check the feature consistency

         Check_Connection_Consistency (Intermediate_F);

         Destination_F := AIN.First_Node (Destinations (Intermediate_F));

         while Present (Destination_F) loop
            C := Parent_Component (Item (Destination_F));

            --  If C is also the parent component of Root_F (which
            --  is necessarily a subprogram). Then display an error
            --  and exit.

            if C = Parent_Component (Root_F) then
               Display_Located_Error
                 (AIN.Loc (C),
                  "This subprogram is involved in a connection cycle",
                  Fatal => True);
            end if;

            if AINU.Is_Subprogram (C) then
               --  If C is a subprogram, then append it to the call
               --  list (if necessary, and add a parameter
               --  association corresponding to Root_F and
               --  Destination_F.

               Update_Remote_Calls (C, Root_F);
            else
               --  Otherwise, keep unwinding the destinations

               Call_Remote_Functions (Root_F, Item (Destination_F));
            end if;

            Destination_F := AIN.Next_Node (Destination_F);
         end loop;
      end Call_Remote_Functions;

      -------------------------
      -- Update_Remote_Calls --
      -------------------------

      procedure Update_Remote_Calls
        (Remote_Spg  : Node_Id;
         Param_Value : Node_Id)
      is
         R_Name  : Name_Id;
         N       : Node_Id;
         Profile : List_Id;
      begin
         --  Check whether a call corresponding to Spg has already
         --  been added to Call_List (we use name table infos
         --  instead of looping on Call list, which is a very fast
         --  way.

         Set_Nat_To_Name_Buffer (Nat (Remote_Spg));
         Add_Str_To_Name_Buffer ("%RemoteCall%");
         Add_Nat_To_Name_Buffer (Nat (Spg_Call));
         R_Name := Name_Find;

         if Get_Name_Table_Info (R_Name) = 0 then
            N := Message_Comment ("Call stub ");
            Append_Node_To_List (N, Statements);

            Profile := New_List (CTN.K_List_Id);

            --  Add the FROM argument (first argument)
226
227
228
229
230
            N :=
              Make_Defining_Identifier
                (Map_C_Enumerator_Name
                   (Parent_Subcomponent (Caller_Thread),
                    Entity => True));
231
232
233
234
            Append_Node_To_List (N, Profile);

            --  Add the TO argument (second argument)

235
236
237
238
239
            N :=
              Make_Defining_Identifier
                (Map_C_Enumerator_Name
                   (Parent_Subcomponent (Get_Container_Thread (Remote_Spg)),
                    Entity => True));
240
241
242
243

            Append_Node_To_List (N, Profile);

            --  Add the message argument (third argument)
244
245
246
            N :=
              Make_Variable_Address
                (Make_Defining_Identifier (PN (P_Message)));
247
248
249
250
            Append_Node_To_List (N, Profile);

            --  Create the subprogram call

251
            N := Make_Call_Profile (Map_Stub_Identifier (Remote_Spg), Profile);
252
253
254
255
256
257
258
259
260
261
262
            Append_Node_To_List (N, Statements);

            --  Mark the call as being added. The info we associate
            --  to the name is the value of the profile list to be
            --  able to get it to add parameter associations.

            Set_Name_Table_Info (R_Name, Nat (Profile));
         else
            Profile := List_Id (Get_Name_Table_Info (R_Name));
         end if;

263
264
265
         N :=
           Make_Defining_Identifier
             (Map_C_Full_Parameter_Name (Spg_Call, Param_Value));
266
267
268
269
         Append_Node_To_List (N, Profile);

      end Update_Remote_Calls;

270
271
      Spg : constant Node_Id := Corresponding_Instance (Spg_Call);
      F   : Node_Id;
272
273
274
275
276
277
   begin
      pragma Assert (AINU.Is_Thread (Caller_Thread));

      --  The lists have to be created

      if Declarations = No_List or else Statements = No_List then
278
279
280
         raise Program_Error
           with "Lists have to be created before any " &
           "call to Get_Remote_Subprogram";
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
306
307
308
309
310
311
312
313
314
315
316
317
      end if;

      if not AINU.Is_Empty (Features (Spg)) then
         F := AIN.First_Node (Features (Spg));

         while Present (F) loop
            if Kind (F) = K_Parameter_Instance and then Is_Out (F) then
               --  Call all the remote subprograms connected the
               --  feature F.

               Call_Remote_Functions (F, F);
            end if;

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

      --  Append the calls to the Statements list

      if not CTU.Is_Empty (Call_List) then
         Append_Node_To_List (CTN.First_Node (Call_List), Statements);
      end if;
   end Call_Remote_Functions;

   ---------------------------------
   -- Map_Distributed_Application --
   ---------------------------------

   function Map_Distributed_Application (E : Node_Id) return Node_Id is
      D : constant Node_Id := New_Node (CTN.K_HI_Distributed_Application);
   begin
      pragma Assert (AINU.Is_System (E) or else AINU.Is_Processor (E));

      --  Update the global variable to be able to fetch the root of
      --  the distributed application and generate the source files.

      if Get_Current_Backend_Kind = PolyORB_Kernel_C then
318
319
320
321
322
         CTN.Set_Name
           (D,
            To_C_Name
              (AIN.Name (AIN.Identifier (Parent_Subcomponent (E))),
               Keyword_Check => False));
323
      else
hugues.jerome's avatar
hugues.jerome committed
324
         CTN.Set_Name (D, To_C_Name (AIN.Name (AIN.Identifier (E))));
325
      end if;
hugues.jerome's avatar
hugues.jerome committed
326

327
328
329
330
331
332
333
334
335
336
      CTN.Set_Units (D, New_List (CTN.K_List_Id));
      CTN.Set_HI_Nodes (D, New_List (CTN.K_List_Id));

      return D;
   end Map_Distributed_Application;

   -----------------
   -- Map_HI_Node --
   -----------------

337
338
339
340
   function Map_HI_Node
     (E      : Node_Id;
      Kernel : Boolean := False) return Node_Id
   is
341
342
343
      N : constant Node_Id := New_Node (CTN.K_HI_Node);
   begin
      pragma Assert
344
        (AINU.Is_Process_Or_Device (E)
345
346
347
348
349
350
351
352
353
354
355
         or else AINU.Is_System (E)
         or else AINU.Is_Processor (E));

      --  The name of the node is not the name of the process
      --  component instance, but the name of the process subcomponent
      --  corresponding to this instance.

      if Kernel then
         Set_Str_To_Name_Buffer ("kernel");
         CTN.Set_Name (N, Name_Find);
      else
356
357
358
359
360
         CTN.Set_Name
           (N,
            To_C_Name
              (AIN.Name (AIN.Identifier (AIN.Parent_Subcomponent (E))),
               Keyword_Check => False));
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
      end if;

      Set_Units (N, New_List (K_List_Id));

      --  Append the partition N to the node list of the PolyORB-HI
      --  distributed application. We are sure that the top of the
      --  entity stack contains the C distributed application node.

      Append_Node_To_List (N, HI_Nodes (Current_Entity));
      Set_Distributed_Application (N, Current_Entity);

      return N;
   end Map_HI_Node;

   -----------------
   -- Map_HI_Unit --
   -----------------

   function Map_HI_Unit (E : Node_Id) return Node_Id is
380
381
382
383
384
      U : Node_Id;
      S : List_Id;
      H : List_Id;
      N : Node_Id;
      P : Node_Id;
385
   begin
386
387
388
389
      pragma Assert
        (AINU.Is_System (E)
         or else AINU.Is_Process_Or_Device (E)
         or else AINU.Is_Processor (E));
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581

      U := New_Node (CTN.K_HI_Unit, AIN.Identifier (E));
      S := New_List (K_Sources);
      H := New_List (K_Headers);

      --  Packages that are common to all nodes

      if AINU.Is_Process_Or_Device (E) then
         Set_Str_To_Name_Buffer ("subprograms");
         N := Make_Defining_Identifier (Name_Find);
         P := Make_Source_File (N);
         Set_Distributed_Application_Unit (P, U);
         CTN.Set_Subprograms_Source (U, P);
         Append_Node_To_List (P, S);

         if Get_Current_Backend_Kind = PolyORB_Kernel_C then
            Set_Str_To_Name_Buffer ("gtypes");
         elsif Get_Current_Backend_Kind = PolyORB_HI_C then
            Set_Str_To_Name_Buffer ("types");
         end if;
         N := Make_Defining_Identifier (Name_Find);
         P := Make_Source_File (N);
         Set_Distributed_Application_Unit (P, U);
         CTN.Set_Types_Source (U, P);
         Append_Node_To_List (P, S);

         Set_Str_To_Name_Buffer ("marshallers");
         N := Make_Defining_Identifier (Name_Find);
         P := Make_Source_File (N);
         Set_Distributed_Application_Unit (P, U);
         CTN.Set_Marshallers_Source (U, P);
         Append_Node_To_List (P, S);

         Set_Str_To_Name_Buffer ("request");
         N := Make_Defining_Identifier (Name_Find);
         P := Make_Source_File (N);
         Set_Distributed_Application_Unit (P, U);
         CTN.Set_Request_Source (U, P);
         Append_Node_To_List (P, S);

         Set_Str_To_Name_Buffer ("activity");
         N := Make_Defining_Identifier (Name_Find);
         P := Make_Source_File (N);
         Set_Distributed_Application_Unit (P, U);
         CTN.Set_Activity_Source (U, P);
         Append_Node_To_List (P, S);
         Bind_AADL_To_Activity (Identifier (E), P);

         Set_Str_To_Name_Buffer ("activity");
         N := Make_Defining_Identifier (Name_Find);
         P := Make_Header_File (N);
         Set_Distributed_Application_Unit (P, U);
         CTN.Set_Activity_Header (U, P);
         Append_Node_To_List (P, H);

         Set_Str_To_Name_Buffer ("main");
         N := Make_Defining_Identifier (Name_Find);
         P := Make_Source_File (N);
         Set_Distributed_Application_Unit (P, U);
         CTN.Set_Main_Source (U, P);
         Append_Node_To_List (P, S);
         Bind_AADL_To_Main (Identifier (E), P);

         Set_Str_To_Name_Buffer ("request");
         N := Make_Defining_Identifier (Name_Find);
         P := Make_Header_File (N);
         Set_Distributed_Application_Unit (P, U);
         CTN.Set_Request_Header (U, P);
         Append_Node_To_List (P, H);

         if Get_Current_Backend_Kind = PolyORB_Kernel_C then
            Set_Str_To_Name_Buffer ("gtypes");
         elsif Get_Current_Backend_Kind = PolyORB_HI_C then
            Set_Str_To_Name_Buffer ("types");
         end if;
         N := Make_Defining_Identifier (Name_Find);
         P := Make_Header_File (N);
         Set_Distributed_Application_Unit (P, U);
         CTN.Set_Types_Header (U, P);
         Append_Node_To_List (P, H);

         Set_Str_To_Name_Buffer ("marshallers");
         N := Make_Defining_Identifier (Name_Find);
         P := Make_Header_File (N);
         Set_Distributed_Application_Unit (P, U);
         CTN.Set_Marshallers_Header (U, P);
         Append_Node_To_List (P, H);

         Set_Str_To_Name_Buffer ("subprograms");
         N := Make_Defining_Identifier (Name_Find);
         P := Make_Header_File (N);
         Set_Distributed_Application_Unit (P, U);
         CTN.Set_Subprograms_Header (U, P);
         Append_Node_To_List (P, H);

      end if;

      Set_Str_To_Name_Buffer ("deployment");
      N := Make_Defining_Identifier (Name_Find);
      P := Make_Source_File (N);
      Set_Distributed_Application_Unit (P, U);
      CTN.Set_Deployment_Source (U, P);
      Append_Node_To_List (P, S);

      Set_Str_To_Name_Buffer ("deployment");
      N := Make_Defining_Identifier (Name_Find);
      P := Make_Header_File (N);
      Set_Distributed_Application_Unit (P, U);
      CTN.Set_Deployment_Header (U, P);
      Append_Node_To_List (P, H);

      Set_Str_To_Name_Buffer ("naming");
      N := Make_Defining_Identifier (Name_Find);
      P := Make_Header_File (N);
      Set_Distributed_Application_Unit (P, U);
      CTN.Set_Naming_Header (U, P);
      Append_Node_To_List (P, H);

      Set_Str_To_Name_Buffer ("naming");
      N := Make_Defining_Identifier (Name_Find);
      P := Make_Source_File (N);
      Set_Distributed_Application_Unit (P, U);
      CTN.Set_Naming_Source (U, P);
      Append_Node_To_List (P, S);
      Bind_AADL_To_Naming (Identifier (E), P);

      --  Append the Unit to the units list of the current C
      --  partition.

      CTN.Set_Sources (U, S);
      CTN.Set_Headers (U, H);

      Append_Node_To_List (U, Units (Current_Entity));
      CTN.Set_Entity (U, Current_Entity);

      return U;
   end Map_HI_Unit;

   -------------------------------------
   -- Bind_AADL_To_Feature_Subprogram --
   -------------------------------------

   procedure Bind_AADL_To_Feature_Subprogram (G : Node_Id; A : Node_Id) is
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);
      end if;

      CTN.Set_Feature_Subprogram_Node (N, A);
      CTN.Set_Frontend_Node (A, G);
   end Bind_AADL_To_Feature_Subprogram;

   -----------------------------
   -- Bind_AADL_To_Deployment --
   -----------------------------

   procedure Bind_AADL_To_Deployment (G : Node_Id; A : Node_Id) is
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);
      end if;

      CTN.Set_Deployment_Node (N, A);
      CTN.Set_Frontend_Node (A, G);
   end Bind_AADL_To_Deployment;

   ------------------------------
   -- Bind_AADL_To_Global_Port --
   ------------------------------

   procedure Bind_AADL_To_Global_Port (G : Node_Id; A : Node_Id) is
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);
      end if;

      CTN.Set_Global_Port_Node (N, A);
      CTN.Set_Frontend_Node (A, G);
   end Bind_AADL_To_Global_Port;

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

   procedure Bind_AADL_To_Global_Names (G : Node_Id; A : Node_Id) is
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);
      end if;

      CTN.Set_Global_Names_Node (N, A);
      CTN.Set_Frontend_Node (A, G);
   end Bind_AADL_To_Global_Names;

   -------------------------------------
   -- Bind_AADL_To_Global_Model_Names --
   -------------------------------------

   procedure Bind_AADL_To_Global_Model_Names (G : Node_Id; A : Node_Id) is
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);
      end if;

      CTN.Set_Global_Model_Names_Node (N, A);
      CTN.Set_Frontend_Node (A, G);
   end Bind_AADL_To_Global_Model_Names;

618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
   -----------------------------
   -- Bind_AADL_To_Local_Port --
   -----------------------------

   procedure Bind_AADL_To_Local_Port (G : Node_Id; A : Node_Id) is
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);
      end if;

      CTN.Set_Local_Port_Node (N, A);
      CTN.Set_Frontend_Node (A, G);
   end Bind_AADL_To_Local_Port;

   --------------------------
   -- Bind_AADL_To_Request --
   --------------------------

   procedure Bind_AADL_To_Request (G : Node_Id; A : Node_Id) is
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);
      end if;

      CTN.Set_Request_Node (N, A);
      CTN.Set_Frontend_Node (A, G);
   end Bind_AADL_To_Request;

   ---------------------------
   -- Bind_AADL_To_Entities --
   ---------------------------

   procedure Bind_AADL_To_Entities (G : Node_Id; A : Node_Id) is
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);
      end if;

      CTN.Set_Entities_Node (N, A);
      CTN.Set_Frontend_Node (A, G);
   end Bind_AADL_To_Entities;

   --------------------------
   -- Bind_AADL_To_Servers --
   --------------------------

   procedure Bind_AADL_To_Servers (G : Node_Id; A : Node_Id) is
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);
      end if;

      CTN.Set_Servers_Node (N, A);
      CTN.Set_Frontend_Node (A, G);
   end Bind_AADL_To_Servers;

   -----------------------------
   -- Bind_AADL_To_Subprogram --
   -----------------------------

   procedure Bind_AADL_To_Subprogram (G : Node_Id; A : Node_Id) is
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);
      end if;

      CTN.Set_Subprogram_Node (N, A);
      CTN.Set_Frontend_Node (A, G);
   end Bind_AADL_To_Subprogram;

   ---------------------------
   -- Bind_AADL_To_Activity --
   ---------------------------

   procedure Bind_AADL_To_Activity (G : Node_Id; A : Node_Id) is
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);
      end if;

      CTN.Set_Activity_Node (N, A);
      CTN.Set_Frontend_Node (A, G);
   end Bind_AADL_To_Activity;

   -------------------------
   -- Bind_AADL_To_Naming --
   -------------------------

   procedure Bind_AADL_To_Naming (G : Node_Id; A : Node_Id) is
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);
      end if;

      CTN.Set_Naming_Node (N, A);
      CTN.Set_Frontend_Node (A, G);
   end Bind_AADL_To_Naming;

   ----------------------
   -- Bind_AADL_To_Job --
   ----------------------

   procedure Bind_AADL_To_Job (G : Node_Id; A : Node_Id) is
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);
      end if;

      CTN.Set_Job_Node (N, A);
      CTN.Set_Frontend_Node (A, G);
   end Bind_AADL_To_Job;

   -----------------------------
   -- Bind_AADL_To_Enumerator --
   -----------------------------

   procedure Bind_AADL_To_Enumerator (G : Node_Id; A : Node_Id) is
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);
      end if;

      CTN.Set_Enumerator_Node (N, A);
      CTN.Set_Frontend_Node (A, G);
   end Bind_AADL_To_Enumerator;

   ----------------------------------
   -- Bind_AADL_To_Type_Definition --
   ----------------------------------

   procedure Bind_AADL_To_Type_Definition (G : Node_Id; A : Node_Id) is
785
786
      pragma Assert (Present (A));

787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);
      end if;

      CTN.Set_Type_Definition_Node (N, A);
      CTN.Set_Frontend_Node (A, G);
   end Bind_AADL_To_Type_Definition;

   ----------------------------------
   -- Bind_AADL_To_Process_Request --
   ----------------------------------

   procedure Bind_AADL_To_Process_Request (G : Node_Id; A : Node_Id) is
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);
      end if;

      CTN.Set_Process_Request_Node (N, A);
      CTN.Set_Frontend_Node (A, G);
   end Bind_AADL_To_Process_Request;

   -------------------------------
   -- Bind_AADL_To_Request_Type --
   -------------------------------

   procedure Bind_AADL_To_Request_Type (G : Node_Id; A : Node_Id) is
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);
      end if;

      CTN.Set_Request_Type_Node (N, A);
      CTN.Set_Frontend_Node (A, G);
   end Bind_AADL_To_Request_Type;

   -----------------------
   -- Bind_AADL_To_Main --
   -----------------------

   procedure Bind_AADL_To_Main (G : Node_Id; A : Node_Id) is
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);
      end if;

      CTN.Set_Main_Node (N, A);
      CTN.Set_Frontend_Node (A, G);
   end Bind_AADL_To_Main;

   -----------------------------
   -- Map_Task_Job_Identifier --
   -----------------------------

jdelange's avatar
jdelange committed
858
   function Map_Task_Job_Identifier
859
860
861
     (E                : Node_Id;
      Prefix_Component : Node_Id := No_Node) return Node_Id
   is
862
      Name : Name_Id;
jdelange's avatar
jdelange committed
863
864
      C1   : Name_Id := No_Name;
      C2   : Name_Id := No_Name;
865
   begin
jdelange's avatar
jdelange committed
866
867
      if Prefix_Component /= No_Node then
         Get_Name_String
868
           (To_C_Name (AIN.Display_Name (Identifier (Prefix_Component))));
jdelange's avatar
jdelange committed
869
870
         C1 := Name_Find;
      end if;
871
      Get_Name_String (To_C_Name (AIN.Display_Name (Identifier (E))));
jdelange's avatar
jdelange committed
872
873
874
875
876
877
878
879
880
881
      C2 := Name_Find;

      Set_Str_To_Name_Buffer ("");

      if C1 /= No_Name then
         Get_Name_String (C1);
         Add_Str_To_Name_Buffer ("_");
      end if;

      Get_Name_String_And_Append (C2);
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
      Add_Str_To_Name_Buffer ("_job");
      Name := Name_Find;
      Name := To_Lower (Name);
      return Make_Defining_Identifier (Name);
   end Map_Task_Job_Identifier;

   ---------------------------------
   -- Map_Task_Deliver_Identifier --
   ---------------------------------

   function Map_Task_Deliver_Identifier (E : Node_Id) return Node_Id is
      Name : Name_Id;
   begin
      Get_Name_String (To_C_Name (AIN.Display_Name (Identifier (E))));
      Add_Str_To_Name_Buffer ("_deliver");
      Name := Name_Find;
      Name := To_Lower (Name);
      return Make_Defining_Identifier (Name);
   end Map_Task_Deliver_Identifier;

   ---------------------
   -- Map_C_Enum_Name --
   ---------------------

906
907
908
   function Map_C_Enum_Name
     (E          : Node_Id;
      Enumerator : Name_Id) return Name_Id
909
910
911
912
913
914
915
   is
      Enum_Name : Name_Id;
   begin
      --  Note: we need to adapt the name of the enumerator to avoid
      --  collision of enumerators, as C does not allow twice the same
      --  enumerators in two different enum types.

916
      Enum_Name := To_C_Name (Display_Name (Identifier (E)));
917
918
919
920
921
922
923
924
925
926
927
928

      Get_Name_String_And_Append (Enum_Name);
      Add_Str_To_Name_Buffer ("_");
      Get_Name_String_And_Append (Enumerator);
      return Name_Find;
   end Map_C_Enum_Name;

   ---------------------------
   -- Map_C_Enumerator_Name --
   ---------------------------

   function Map_C_Enumerator_Name
929
930
931
932
933
934
     (E             : Node_Id;
      Custom_Parent : Node_Id := No_Node;
      Entity        : Boolean := False;
      Server        : Boolean := False;
      Port_Type     : Boolean := False;
      Local_Port    : Boolean := False) return Name_Id
935
936
937
938
939
   is
      C_Name_1 : Name_Id;
      C_Name_2 : Name_Id;
   begin
      if Kind (E) = K_Port_Spec_Instance then
940
         C_Name_1 := CTU.To_C_Name (Display_Name (Identifier (E)));
941
942
943
944

         Get_Name_String
           (CTU.To_C_Name
              (Display_Name
945
                 (Identifier (Parent_Subcomponent (Parent_Component (E))))));
946
947
948
949
950
951
952
         if Local_Port then
            Add_Str_To_Name_Buffer ("_local_");
            Get_Name_String_And_Append (C_Name_1);
         else
            Add_Str_To_Name_Buffer ("_global_");
            Get_Name_String_And_Append (C_Name_1);
         end if;
953
954
955
      elsif AINU.Is_Bus (E) then
         Set_Str_To_Name_Buffer ("bus_");
         Get_Name_String_And_Append
956
           (AIN.Name (AIN.Identifier (Parent_Subcomponent (E))));
957
958
959
960
      elsif AINU.Is_Virtual_Bus (E) then
         Set_Str_To_Name_Buffer ("virtual_bus_");
         if Parent_Subcomponent (E) /= No_Node then
            Get_Name_String_And_Append
961
              (AIN.Name (AIN.Identifier (Parent_Subcomponent (E))));
962
         else
963
            Get_Name_String_And_Append (AIN.Name (AIN.Identifier (E)));
964
         end if;
965
      elsif AINU.Is_Subprogram (E) then
966
967
968
969
970
         --  For subprograms and processes, the enemerator name is
         --  mapped from the entity name.

         Get_Name_String (CTU.To_C_Name (Display_Name (Identifier (E))));
         Add_Str_To_Name_Buffer ("_k");
971
972
973
      elsif Get_Current_Backend_Kind = PolyORB_HI_C
        and then AINU.Is_Device (E)
      then
974
         Get_Name_String
975
976
           (CTU.To_C_Name
              (Display_Name (Identifier (Parent_Subcomponent (E)))));
977
         Add_Str_To_Name_Buffer ("_device_id");
978
979
980
981
      elsif
        (Present (Corresponding_Instance (E))
         and then AINU.Is_Process_Or_Device (Corresponding_Instance (E)))
      then
982

983
984
         Get_Name_String (CTU.To_C_Name (Display_Name (Identifier (E))));
         Add_Str_To_Name_Buffer ("_k");
985
986
987
      elsif AINU.Is_Thread (Corresponding_Instance (E)) then
         --  For threads, the enumerator name is mapped from the
         --  containing process name and the thread subcomponent name.
988
         if Custom_Parent /= No_Node then
989
990
991
992
            C_Name_1 :=
              CTU.To_C_Name
                (Display_Name
                   (Identifier (Parent_Subcomponent (Custom_Parent))));
993
         else
994
995
996
997
            C_Name_1 :=
              CTU.To_C_Name
                (Display_Name
                   (Identifier (Parent_Subcomponent (Parent_Component (E)))));
998
999
         end if;

1000
         C_Name_2 := CTU.To_C_Name (Display_Name (Identifier (E)));
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020

         if Port_Type then
            Set_Str_To_Name_Buffer ("__po_hi_");
            Get_Name_String_And_Append (C_Name_2);
            Add_Str_To_Name_Buffer ("_t");
         else
            Get_Name_String (C_Name_1);
            Add_Char_To_Name_Buffer ('_');
            Get_Name_String_And_Append (C_Name_2);
            Add_Str_To_Name_Buffer ("_k");
         end if;

         if Server then
            Add_Str_To_Name_Buffer ("_server");
         end if;

         if Entity then
            Add_Str_To_Name_Buffer ("_entity");
         end if;
      else
1021
         raise Program_Error with "Wrong node kind for Map_C_Enumerator_Name";
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
      end if;

      C_Name_1 := Name_Find;
      C_Name_1 := To_Lower (C_Name_1);

      return C_Name_1;
   end Map_C_Enumerator_Name;

   -----------------------
   -- Map_C_Define_Name --
   -----------------------

   function Map_C_Define_Name
1035
1036
1037
1038
1039
1040
     (E                                             : Node_Id;
      Nb_Ports                                      : Boolean := False;
      Nb_States                                     : Boolean := False;
      Max_Dispatch_Transitions_Per_Complete_State   : Boolean := False;
      Max_Dispatch_Triggers_Per_Dispatch_Transition : Boolean := False)
      return Name_Id
1041
1042
1043
1044
1045
1046
1047
   is
      C_Name : Name_Id;
   begin
      if AINU.Is_Thread (Corresponding_Instance (E)) then
         --  For threads, the enumerator name is mapped from the
         --  containing process name and the thread subcomponent name.

1048
         C_Name := Display_Name (Identifier (E));
1049
1050
1051
1052
1053
1054

         Set_Str_To_Name_Buffer ("__PO_HI_");

         Get_Name_String_And_Append (C_Name);
         if Nb_Ports then
            Add_Str_To_Name_Buffer ("_NB_PORTS");
1055
1056
         elsif Nb_States then
            Add_Str_To_Name_Buffer ("_nb_states");
1057
1058
1059
1060
1061
1062
         elsif Max_Dispatch_Transitions_Per_Complete_State then
            Add_Str_To_Name_Buffer
              ("_max_dispatch_transitions_per_complete_state");
         elsif Max_Dispatch_Triggers_Per_Dispatch_Transition then
            Add_Str_To_Name_Buffer
              ("_max_dispatch_triggers_per_dispatch_transition");
1063
1064
         end if;
      else
1065
         raise Program_Error with "Wrong node kind for Map_C_Enumerator_Name";
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
      end if;

      C_Name := Name_Find;
      C_Name := To_Upper (C_Name);

      return C_Name;
   end Map_C_Define_Name;

   -------------------------
   -- Map_C_Variable_Name --
   -------------------------

   function Map_C_Variable_Name
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
     (E                   : Node_Id;
      Port_Variable       : Boolean := False;
      Port_History        : Boolean := False;
      Port_Woffsets       : Boolean := False;
      Port_Empties        : Boolean := False;
      Port_First          : Boolean := False;
      Port_Queue          : Boolean := False;
      Port_Recent         : Boolean := False;
      Port_Fifo_Size      : Boolean := False;
      Port_Offsets        : Boolean := False;
      Port_Used_Size      : Boolean := False;
      Port_N_Dest         : Boolean := False;
      Port_Local_Dest     : Boolean := False;
      Port_Destinations   : Boolean := False;
      Port_Total_Fifo     : Boolean := False;
      Port_Request        : Boolean := False;
      Request_Variable    : Boolean := False;
      State_Name_T        : Boolean := False;
      State_T             : Boolean := False;
      States_Array        : Boolean := False;
      Current_State       : Boolean := False) return Name_Id
1100
1101
1102
1103
1104
1105
   is
      C_Name : Name_Id;
   begin

      C_Name := To_C_Name (Display_Name (Identifier (E)));

1106
      if not Port_Request and then not Request_Variable then
1107
1108
1109
1110
1111
         Set_Str_To_Name_Buffer ("__po_hi_");

         if Port_Local_Dest then
            Get_Name_String_And_Append
              (AIN.Name
1112
                 (Identifier (Parent_Subcomponent (Parent_Component (E)))));
1113
1114
1115
1116
1117
1118
1119
1120
1121
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
            Add_Str_To_Name_Buffer ("_");
         end if;

         Get_Name_String_And_Append (C_Name);
      else
         Get_Name_String (C_Name);
      end if;

      if Port_Variable then
         Add_Str_To_Name_Buffer ("_local_to_global");
      elsif Port_History then
         Add_Str_To_Name_Buffer ("_history");
      elsif Port_Woffsets then
         Add_Str_To_Name_Buffer ("_woffsets");
      elsif Port_Empties then
         Add_Str_To_Name_Buffer ("_empties");
      elsif Port_First then
         Add_Str_To_Name_Buffer ("_first");
      elsif Port_Queue then
         Add_Str_To_Name_Buffer ("_queue");
      elsif Port_Recent then
         Add_Str_To_Name_Buffer ("_recent");
      elsif Port_Fifo_Size then
         Add_Str_To_Name_Buffer ("_fifo_size");
      elsif Port_Offsets then
         Add_Str_To_Name_Buffer ("_offsets");
      elsif Port_Used_Size then
         Add_Str_To_Name_Buffer ("_used_size");
      elsif Port_N_Dest then
         Add_Str_To_Name_Buffer ("_n_dest");
      elsif Port_Local_Dest then
         Add_Str_To_Name_Buffer ("_local_destinations");
      elsif Port_Destinations then
         Add_Str_To_Name_Buffer ("_destinations");
      elsif Port_Total_Fifo then
         Add_Str_To_Name_Buffer ("_total_fifo_size");
      elsif Port_Request then
         Add_Str_To_Name_Buffer ("_request");
      elsif Request_Variable then
         Add_Str_To_Name_Buffer ("_request_var");
1153
1154
1155
1156
1157
1158
1159
1160
      elsif State_Name_T then
         Add_Str_To_Name_Buffer ("_state_name_t");
      elsif State_T then
         Add_Str_To_Name_Buffer ("_state_t");
      elsif States_Array then
         Add_Str_To_Name_Buffer ("_states_array");
      elsif Current_State then
         Add_Str_To_Name_Buffer ("_current_state");
1161
1162
1163
1164
1165
1166
1167
      end if;

      C_Name := Name_Find;

      return C_Name;
   end Map_C_Variable_Name;

1168
1169
1170
1171
1172
   ------------------------------------
   -- Map_C_BA_Related_Function_Name --
   ------------------------------------

   function Map_C_BA_Related_Function_Name
1173
1174
1175
1176
1177
     (E                          : Node_Id;
      BA_Body                    : Boolean := False;
      States_Initialization      : Boolean := False;
      BA_Initialization          : Boolean := False;
      Update_Next_Complete_State : Boolean := False) return Name_Id
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
   is
      C_Name : Name_Id;
   begin

      C_Name := To_C_Name (Display_Name (Identifier (E)));

      Get_Name_String (C_Name);

      if BA_Body then
         Add_Str_To_Name_Buffer ("_ba_body");
      elsif States_Initialization then
1189
         Add_Str_To_Name_Buffer ("_states_and_current_state_initialization");
1190
1191
      elsif BA_Initialization then
         Add_Str_To_Name_Buffer ("_ba_initialization");
1192
1193
      elsif Update_Next_Complete_State then
         Add_Str_To_Name_Buffer ("_update_next_complete_state");
1194
1195
1196
1197
1198
1199
1200
      end if;

      C_Name := Name_Find;

      return C_Name;
   end Map_C_BA_Related_Function_Name;

1201
1202
1203
1204
   --------------------------
   -- Map_C_Operation_Name --
   --------------------------

1205
   function Map_C_Operation_Name (E : Node_Id) return Name_Id is
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
      C_Name : Name_Id;
   begin
      Get_Name_String (CTU.To_C_Name (Display_Name (Identifier (E))));
      C_Name := Name_Find;
      return C_Name;
   end Map_C_Operation_Name;

   -------------------------------
   -- Map_C_Full_Parameter_Name --
   -------------------------------

   function Map_C_Full_Parameter_Name
     (Spg    : Node_Id;
      P      : Node_Id;
1220
      Suffix : Character := ASCII.NUL) return Name_Id
1221
1222
1223
1224
1225
   is
   begin
      pragma Assert (Kind (P) = K_Parameter_Instance);

      if Kind (Spg) = K_Component_Instance
1226
1227
        and then AINU.Is_Subprogram (Spg)
      then
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
         Get_Name_String (AINU.Compute_Full_Name_Of_Instance (Spg, True));
      elsif Kind (Spg) = K_Call_Instance then
         Get_Name_String (Display_Name (Identifier (Spg)));
      else
         raise Program_Error with "Wrong subprogram kind";
      end if;

      Add_Char_To_Name_Buffer ('_');
      Get_Name_String_And_Append (Display_Name (Identifier (P)));

      Get_Name_String (CTU.To_C_Name (Name_Find));

      if Suffix /= ASCII.NUL then
         Add_Str_To_Name_Buffer ('_' & Suffix);
      end if;

      return Name_Find;
   end Map_C_Full_Parameter_Name;

   --------------------------
   -- Map_C_Port_Data_Name --
   --------------------------

1251
   function Map_C_Port_Data_Name (E : Node_Id; P : Node_Id) return Name_Id is
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
   begin
      Get_Name_String (AINU.Compute_Full_Name_Of_Instance (E, True));

      Add_Char_To_Name_Buffer ('_');
      Get_Name_String_And_Append (Display_Name (Identifier (P)));

      Get_Name_String (CTU.To_C_Name (Name_Find));

      return Name_Find;
   end Map_C_Port_Data_Name;

   --------------------------------
   -- Map_C_Data_Type_Designator --
   --------------------------------

   function Map_C_Data_Type_Designator (E : Node_Id) return Node_Id is
      N : Node_Id;
   begin
      if Get_Current_Backend_Kind = PolyORB_Kernel_C then
         Add_Include (PKR.RH (RH_Gtypes));
1272

1273
1274
1275
1276
1277
      elsif Get_Current_Backend_Kind = PolyORB_HI_C then
         Add_Include (PHR.RH (RH_Types));
      end if;

      if Present (Backend_Node (Identifier (E)))
1278
1279
        and then Present
          (CTN.Type_Definition_Node (Backend_Node (Identifier (E))))
1280
      then
1281
1282
1283
         N :=
           Type_Name
             (CTN.Type_Definition_Node (Backend_Node (Identifier (E))));
1284
1285
1286

         if Kind (N) = K_Defining_Identifier then
            return N;
1287

1288
1289
1290
1291
         elsif Kind (N) = K_Array_Declaration then
            return Defining_Identifier (N);
         end if;
      else
1292
1293
         --  XXX why do we need this hack?
         return Map_C_Defining_Identifier (E);
1294
      end if;
1295
1296

      return No_Node;
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
   end Map_C_Data_Type_Designator;

   ---------------------------------
   -- Map_C_Subprogram_Identifier --
   ---------------------------------

   function Map_C_Subprogram_Identifier (E : Node_Id) return Node_Id is
      Result   : Node_Id;
      Spg_Name : Name_Id;
   begin
1307
1308
1309
1310
      pragma Assert
        (AINU.Is_Thread (E)
         or else AINU.Is_Subprogram (E)
         or else Kind (E) = K_Port_Spec_Instance);
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339

      if AINU.Is_Subprogram (E)
        and then Get_Source_Language (E) /= Language_C
      then
         Display_Error ("This is not a C function", Fatal => True);
      end if;

      --  Get the subprogram name

      if AINU.Is_Subprogram (E) then
         Spg_Name := Get_Source_Name (E);
      elsif AINU.Is_Thread (E) then
         Spg_Name := Get_Thread_Compute_Entrypoint (E);
      else
         Spg_Name := Get_Port_Compute_Entrypoint (E);
      end if;

      --  Get the full implementation name

      Get_Name_String (Spg_Name);
      Result := Make_Defining_Identifier (Name_Find);
      return Result;
   end Map_C_Subprogram_Identifier;

   -------------------------------
   -- Map_C_Defining_Identifier --
   -------------------------------

   function Map_C_Defining_Identifier
1340
1341
1342
1343
1344
     (A          : Node_Id;
      Is_Pointer : Boolean := False) return Node_Id
   is
      I, J      : Node_Id;
      Result    : Node_Id;
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
      Name_List : List_Id;

   begin
      if Kind (A) /= K_Identifier then
         I := Identifier (A);
      end if;

      if Kind (A) = K_Component_Instance
        and then Display_Name (Identifier (Namespace (A))) /= No_Name
      then
         --  If this is a component instance, use both namespace and
         --  identifier to build the C defining identifier, to avoid
         --  collisions in the C namespace.

         Name_List := AINU.Split_Name (Namespace (A));

         J := AIN.First_Node (Name_List);

         if Present (J) then
            Get_Name_String (To_C_Name (Display_Name (J)));
            J := AIN.Next_Node (J);

            while Present (J) loop
1368
1369
               Add_Str_To_Name_Buffer
                 ("__" & Get_Name_String (Display_Name (J)));
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
               J := AIN.Next_Node (J);
            end loop;
         end if;

         Add_Str_To_Name_Buffer ("__" & Get_Name_String (Display_Name (I)));

      else
         Get_Name_String (Display_Name (I));
      end if;

1380
1381
      Result :=
        CTU.Make_Defining_Identifier (To_C_Name (Name_Find), True, Is_Pointer);
1382
1383
1384
1385
1386
1387
1388
1389

      return Result;
   end Map_C_Defining_Identifier;

   ------------------------
   -- Map_Scade_Function --
   ------------------------

1390
   function Map_Scade_Function_Name (Subprogram : Node_Id) return Node_Id is
1391
1392
1393
   begin
      Get_Name_String (Get_Source_Name (Subprogram));

1394
      return CTU.Make_Defining_Identifier (Name_Find, C_Conversion => False);
1395
1396
1397
1398
1399
1400
   end Map_Scade_Function_Name;

   -------------------------
   -- Map_Scade_Struct_In --
   -------------------------

1401
   function Map_Scade_Struct_In (Subprogram : Node_Id) return Node_Id is
1402
1403
1404
1405
   begin
      Set_Str_To_Name_Buffer ("inC_");
      Get_Name_String_And_Append (Get_Source_Name (Subprogram));

1406
      return CTU.Make_Defining_Identifier (Name_Find, C_Conversion => False);
1407
1408
1409
1410
1411
1412
   end Map_Scade_Struct_In;

   --------------------------
   -- Map_Scade_Struct_Out --
   --------------------------

1413
   function Map_Scade_Struct_Out (Subprogram : Node_Id) return Node_Id is
1414
1415
1416
1417
   begin
      Set_Str_To_Name_Buffer ("outC_");
      Get_Name_String_And_Append (Get_Source_Name (Subprogram));

1418
      return CTU.Make_Defining_Identifier (Name_Find, C_Conversion => False);
1419
1420
1421
1422
1423
1424
   end Map_Scade_Struct_Out;

   -------------------------
   -- Map_Scade_Parameter --
   -------------------------

1425
   function Map_Scade_Parameter (Parameter : Node_Id) return Node_Id is
1426
      Scade_Name : Name_Id;
1427
   begin
1428
      Scade_Name := Get_Scade_Signal (Parameter);
1429

1430
1431
1432
1433
1434
1435
      if Scade_Name = No_Name then
         Scade_Name := Get_Source_Name (Parameter);
      end if;

      if Scade_Name = No_Name then
         Display_Located_Error
yoogx's avatar
yoogx committed
1436
1437
1438
           (AIN.Loc (Parameter),
            "The Parameter does not specify a SCADE mapping",
            Fatal => True);
1439
1440
1441
      end if;

      return CTU.Make_Defining_Identifier (Scade_Name, C_Conversion => False);
1442
1443
1444
1445
1446
1447
   end Map_Scade_Parameter;

   ----------------------
   -- Map_Simulink_Var --
   ----------------------

1448
1449
1450
   function Map_Simulink_Var
     (Corresponding_Feature : Node_Id) return Node_Id
   is
1451
1452
1453
1454
1455
1456
      Variable_Name : Name_Id;
      Simulink_Var  : Node_Id;
   begin
      Set_Str_To_Name_Buffer ("");

      Get_Name_String
1457
        (Get_Source_Name (Parent_Component (Corresponding_Feature)));
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468

      if Is_In (Corresponding_Feature) then
         Add_Str_To_Name_Buffer ("_U");
      end if;

      if Is_Out (Corresponding_Feature) then
         Add_Str_To_Name_Buffer ("_Y");
      end if;

      Variable_Name := Name_Find;

1469
1470
1471
1472
1473
1474
1475
      Simulink_Var :=
        Make_Member_Designator
          (Make_Defining_Identifier
             (Get_Source_Name (Corresponding_Feature),
              False),
           Make_Defining_Identifier (Variable_Name, False),
           False);
1476
1477
1478
1479
1480
1481
1482
      return Simulink_Var;
   end Map_Simulink_Var;

   ----------------------------
   -- Map_Simulink_Init_Func --
   ----------------------------

1483
   function Map_Simulink_Init_Func (Subprogram : Node_Id) return Node_Id is
1484
1485
1486
1487
   begin
      Get_Name_String (Get_Source_Name (Subprogram));
      Add_Str_To_Name_Buffer ("_InitializeDataMapInfo");

1488
      return CTU.Make_Defining_Identifier (Name_Find, C_Conversion => False);
1489
1490
1491
1492
1493
1494
   end Map_Simulink_Init_Func;

   -----------------------------
   -- Map_Simulink_Model_Type --
   -----------------------------

1495
   function Map_Simulink_Model_Type (Subprogram : Node_Id) return Node_Id is
1496
1497
1498
1499
1500
   begin

      Set_Str_To_Name_Buffer ("RT_MODEL_");
      Get_Name_String_And_Append (Get_Source_Name (Subprogram));

1501
      return CTU.Make_Defining_Identifier (Name_Find, C_Conversion => False);
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
   end Map_Simulink_Model_Type;

   -------------------------
   -- Map_Simulink_Header --
   -------------------------

   function Map_Simulink_Header (Subprogram : Node_Id) return Node_Id is
      N : Name_Id;
   begin

      N := Get_Source_Name (Subprogram);

      if N = No_Name then
         return No_Node;
      end if;

      Get_Name_String (N);
      Add_Str_To_Name_Buffer ("_capi");

      return CTU.Make_Defining_Identifier (Name_Find);
   end Map_Simulink_Header;

   ---------------------
   -- Map_Source_Name --
   ---------------------

1528
   function Map_Source_Name (Subprogram : Node_Id) return Node_Id is
1529
1530
1531
1532
   begin
      Set_Str_To_Name_Buffer ("");
      Get_Name_String_And_Append (Get_Source_Name (Subprogram));

1533
      return CTU.Make_Defining_Identifier (Name_Find, C_Conversion => False);
1534
1535
1536
1537
1538
1539
   end Map_Source_Name;

   -----------------------------
   -- Map_Lustre_Context_Name --
   -----------------------------

1540
   function Map_Lustre_Context_Name (Subprogram : Node_Id) return Node_Id is
1541
1542
1543
1544
1545
   begin
      Set_Str_To_Name_Buffer ("");
      Get_Name_String_And_Append (Get_Source_Name (Subprogram));
      Add_Str_To_Name_Buffer ("_context");

1546
      return CTU.Make_Defining_Identifier (Name_Find, C_Conversion => False);
1547
1548
1549
1550
1551
1552
   end Map_Lustre_Context_Name;

   ------------------------------
   -- Map_Lustre_Context_Reset --
   ------------------------------

1553
   function Map_Lustre_Context_Reset (Subprogram : Node_Id) return Node_Id is
1554
1555
1556
1557
1558
   begin
      Set_Str_To_Name_Buffer ("");
      Get_Name_String_And_Append (Get_Source_Name (Subprogram));
      Add_Str_To_Name_Buffer ("_reset");

1559
      return CTU.Make_Defining_Identifier (Name_Find, C_Conversion => False);
1560
1561
1562
1563
1564
1565
   end Map_Lustre_Context_Reset;

   -----------------------------
   -- Map_Lustre_Context_Init --
   -----------------------------

1566
   function Map_Lustre_Context_Init (Subprogram : Node_Id) return Node_Id is
1567
1568
1569
1570
1571
   begin
      Set_Str_To_Name_Buffer ("");
      Get_Name_String_And_Append (Get_Source_Name (Subprogram));
      Add_Str_To_Name_Buffer ("_new_ctx");

1572
      return CTU.Make_Defining_Identifier (Name_Find, C_Conversion => False);
1573
1574
1575
1576
1577
1578
   end Map_Lustre_Context_Init;

   -----------------------------
   -- Map_Lustre_Context_Type --
   -----------------------------

1579
   function Map_Lustre_Context_Type (Subprogram : Node_Id) return Node_Id is
1580
1581
1582
1583
1584
   begin
      Set_Str_To_Name_Buffer ("struct ");
      Get_Name_String_And_Append (Get_Source_Name (Subprogram));
      Add_Str_To_Name_Buffer ("_ctx");

1585
      return CTU.Make_Defining_Identifier (Name_Find, C_Conversion => False);
1586
1587
1588
1589
1590
1591
   end Map_Lustre_Context_Type;

   --------------------------
   -- Map_Lustre_Step_Name --
   --------------------------

1592
   function Map_Lustre_Step_Name (Subprogram : Node_Id) return Node_Id is
1593
1594
1595
1596
1597
   begin
      Set_Str_To_Name_Buffer ("");
      Get_Name_String_And_Append (Get_Source_Name (Subprogram));
      Add_Str_To_Name_Buffer ("_step");

1598
      return CTU.Make_Defining_Identifier (Name_Find, C_Conversion => False);
1599
1600
1601
1602
1603
1604
1605
   end Map_Lustre_Step_Name;

   ------------------------------------
   -- Map_Lustre_Input_Function_Name --
   ------------------------------------

   function Map_Lustre_Input_Function_Name
1606
1607
1608
     (Subprogram : Node_Id;
      Port       : Node_Id) return Node_Id
   is
1609
1610
1611
1612
1613
1614
   begin
      Set_Str_To_Name_Buffer ("");
      Get_Name_String_And_Append (Get_Source_Name (Subprogram));
      Add_Str_To_Name_Buffer ("_I_");
      Get_Name_String_And_Append (Get_Source_Name (Port));

1615
      return CTU.Make_Defining_Identifier (Name_Find, C_Conversion => False);
1616
1617
1618
1619
1620
1621
1622
   end Map_Lustre_Input_Function_Name;

   -------------------------
   -- Map_Lustre_Temp_Var --
   -------------------------

   function Map_Lustre_Temp_Var
1623
1624
1625
     (Subprogram : Node_Id;
      Port       : Node_Id) return Node_Id
   is
1626
1627
1628
1629
1630
1631
1632
   begin
      Set_Str_To_Name_Buffer ("");
      Get_Name_String_And_Append (Get_Source_Name (Subprogram));
      Add_Str_To_Name_Buffer ("_");
      Get_Name_String_And_Append (Get_Source_Name (Port));
      Add_Str_To_Name_Buffer ("_lustre_tmpvar");

1633
      return CTU.Make_Defining_Identifier (Name_Find, C_Conversion => False);
1634
1635
1636
1637
1638
1639
1640
   end Map_Lustre_Temp_Var;

   -------------------------------------
   -- Map_Lustre_Output_Function_Name --
   -------------------------------------

   function Map_Lustre_Output_Function_Name
1641
1642
1643
     (Subprogram : Node_Id;
      Port       : Node_Id) return Node_Id
   is
1644
1645
1646
1647
1648
1649
   begin
      Set_Str_To_Name_Buffer ("");
      Get_Name_String_And_Append (Get_Source_Name (Subprogram));
      Add_Str_To_Name_Buffer ("_O_");
      Get_Name_String_And_Append (Get_Source_Name (Port));

1650
      return CTU.Make_Defining_Identifier (Name_Find, C_Conversion => False);
1651
1652
1653
1654
1655
1656
   end Map_Lustre_Output_Function_Name;

   ------------------------
   -- Map_Esterel_Header --
   ------------------------

1657
   function Map_Esterel_Header (Subprogram : Node_Id) return Node_Id is
1658
1659
1660
1661
1662
1663
   begin
      Set_Str_To_Name_Buffer ("");
      Get_Name_String_And_Append (Get_Source_Location (Subprogram));
      Set_Str_To_Name_Buffer (Name_Buffer (1 .. Name_Len - 2));
      Add_Str_To_Name_Buffer ("_strl");

1664
      return CTU.Make_Defining_Identifier (Name_Find, C_Conversion => False);
1665
1666
1667
1668
1669
1670
   end Map_Esterel_Header;

   --------------------------------
   -- Map_Esterel_Reset_Function --
   --------------------------------

1671
   function Map_Esterel_Reset_Function (Subprogram : Node_Id) return Node_Id is
1672
1673
1674
1675
1676
   begin
      Set_Str_To_Name_Buffer ("");
      Get_Name_String_And_Append (Get_Source_Name (Subprogram));
      Add_Str_To_Name_Buffer ("_reset");

1677
      return CTU.Make_Defining_Identifier (Name_Find, C_Conversion => False);
1678
1679
1680
1681
1682
1683
1684
   end Map_Esterel_Reset_Function;

   --------------------------------
   -- Map_Esterel_Input_Function --
   --------------------------------

   function Map_Esterel_Input_Function
1685
1686
1687
     (Subprogram : Node_Id;
      Feature    : Node_Id) return Node_Id
   is
1688
1689
1690
1691
1692
1693
   begin
      Set_Str_To_Name_Buffer ("");
      Get_Name_String_And_Append (Get_Source_Name (Subprogram));
      Add_Str_To_Name_Buffer ("_I_");
      Get_Name_String_And_Append (Get_Source_Name (Feature));

1694
      return CTU.Make_Defining_Identifier (Name_Find, C_Conversion => False);
1695
1696
1697
1698
1699
1700
1701
   end Map_Esterel_Input_Function;

   ---------------------------------
   -- Map_Esterel_Output_Function --
   ---------------------------------

   function Map_Esterel_Output_Function
1702
1703
1704
     (Subprogram : Node_Id;
      Feature    : Node_Id) return Node_Id
   is
1705
1706
1707
1708
1709
1710
   begin
      Set_Str_To_Name_Buffer ("");
      Get_Name_String_And_Append (Get_Source_Name (Subprogram));
      Add_Str_To_Name_Buffer ("_O_");
      Get_Name_String_And_Append (Get_Source_Name (Feature));

1711
      return CTU.Make_Defining_Identifier (Name_Find, C_Conversion => False);
1712
1713
1714
1715
1716
1717
1718
   end Map_Esterel_Output_Function;

   --------------------------
   -- Map_Esterel_Temp_Var --
   --------------------------

   function Map_Esterel_Temp_Var
1719
1720
1721
     (Subprogram : Node_Id;
      Port       : Node_Id) return Node_Id
   is
1722
1723
1724
1725
1726
1727
1728
   begin
      Set_Str_To_Name_Buffer ("");
      Get_Name_String_And_Append (Get_Source_Name (Subprogram));
      Add_Str_To_Name_Buffer ("_");
      Get_Name_String_And_Append (Get_Source_Name (Port));
      Add_Str_To_Name_Buffer ("_esterel_tmpvar");

1729
      return CTU.Make_Defining_Identifier (Name_Find, C_Conversion => False);
1730
1731
1732
1733
1734
1735
1736
1737
   end Map_Esterel_Temp_Var;

   ------------------------------
   -- Map_C_Feature_Subprogram --
   ------------------------------

   function Map_C_Feature_Subprogram
     (A     : Node_Id;
1738
      Owner : Node_Id := No_Node) return Node_Id
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
   is
      I      : Node_Id;
      Result : Node_Id;
      M      : Name_Id;
      N      : Name_Id;
   begin
      if Kind (A) /= K_Identifier then
         I := Identifier (A);
      end if;

      if Present (Owner) then
         M := CTU.To_C_Name (Display_Name (Identifier (Owner)));
      else
         M := CTU.To_C_Name (Display_Name (Identifier (Parent_Component (A))));
      end if;

      N := CTU.To_C_Name (Display_Name (I));

      Get_Name_String (M);
      Add_Char_To_Name_Buffer ('_');
      Get_Name_String_And_Append (N);

1761
      N      := Name_Find;
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
      Result := CTU.Make_Defining_Identifier (N);

      return Result;
   end Map_C_Feature_Subprogram;

   ---------------------------------
   -- Map_C_Marshaller_Subprogram --
   ---------------------------------

   function Map_C_Marshaller_Subprogram
     (A             : Node_Id;
      Is_Unmarshall : Boolean := False;
1774
1775
1776
1777
1778
1779
      Is_Request    : Boolean := False) return Node_Id
   is
      I      : Node_Id;
      Result : Node_Id;
      N      : Name_Id;
      C_Name : Name_Id;
1780
1781
1782
1783
   begin
      I := Identifier (A);

      if Kind (A) = K_Port_Spec_Instance then
1784
1785
1786
1787
         C_Name :=
           CTU.To_C_Name
             (Display_Name
                (Identifier (Parent_Subcomponent (Parent_Component (A)))));
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
      end if;

      N := CTU.To_C_Name (Display_Name (I));

      if Is_Request then
         if Is_Unmarshall then
            Set_Str_To_Name_Buffer ("__po_hi_unmarshall_request_");
         else
            Set_Str_To_Name_Buffer ("__po_hi_marshall_request_");
         end if;
      else
         if Is_Unmarshall then
            Set_Str_To_Name_Buffer ("__po_hi_unmarshall_type_");
         else
            Set_Str_To_Name_Buffer ("__po_hi_marshall_type_");
         end if;
      end if;

      if Kind (A) = K_Port_Spec_Instance then
         Get_Name_String_And_Append (C_Name);
         Add_Str_To_Name_Buffer ("_");
      end if;

      Get_Name_String_And_Append (N);

1813
1814
      N      := Name_Find;
      Result := CTU.Make_Defining_Identifier (N);
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
      return Result;
   end Map_C_Marshaller_Subprogram;

   -----------------------
   -- Bind_AADL_To_Stub --
   -----------------------

   procedure Bind_AADL_To_Stub (G : Node_Id; A : Node_Id) is
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);
      end if;

      CTN.Set_Stub_Node (N, A);
      CTN.Set_Frontend_Node (A, G);
   end Bind_AADL_To_Stub;

   ------------------------
   -- Bind_AADL_To_Types --
   ------------------------

   procedure Bind_AADL_To_Types (G : Node_Id; A : Node_Id) is
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);
      end if;

      CTN.Set_Types_Node (N, A);
      CTN.Set_Frontend_Node (A, G);
   end Bind_AADL_To_Types;

   --------------------------------
   -- Bind_AADL_To_Default_Value --
   --------------------------------

   procedure Bind_AADL_To_Default_Value (G : Node_Id; A : Node_Id) is
      N : Node_Id;
   begin
      N := AIN.Backend_Node (G);

      if No (N) then
         N := New_Node (CTN.K_HI_Tree_Bindings);
         AIN.Set_Backend_Node (G, N);