ocarina-backends-c_tree-nutils.adb 77.6 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 _ T R E E . N U T I L S        --
--                                                                          --
--                                 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
36
37
38
--                                                                          --
------------------------------------------------------------------------------

with GNAT.Table;
with GNAT.Case_Util;

with Charset;   use Charset;
with Locations; use Locations;
39
with Ocarina.Namet;     use Ocarina.Namet;
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
70
71
72
with Utils;     use Utils;

with Ocarina.Backends;
with Ocarina.Backends.C_Common.Mapping;
with Ocarina.Backends.PO_HI_C.Runtime;
with Ocarina.Backends.POK_C;
with Ocarina.Backends.POK_C.Runtime;
with Ocarina.Backends.Utils;
with Ocarina.Backends.Messages;
with Ocarina.Backends.C_Tree.Nutils;
with Ocarina.Backends.C_Values;
with Ocarina.Backends.Properties;

with Ocarina.ME_AADL.AADL_Instances.Nodes;
with Ocarina.ME_AADL.AADL_Tree.Nodes;
with Ocarina.ME_AADL.AADL_Instances.Nutils;
with Ocarina.Instances.Queries;

use Ocarina.ME_AADL.AADL_Instances.Nodes;
use Ocarina.Backends;
use Ocarina.Backends.Utils;
use Ocarina.Backends.Messages;
use Ocarina.Backends.Properties;
use Ocarina.Backends.C_Common.Mapping;
use Ocarina.Backends.C_Values;

use Ocarina.Instances.Queries;

package body Ocarina.Backends.C_Tree.Nutils is

   package AINU renames Ocarina.ME_AADL.AADL_Instances.Nutils;
   package AIN renames Ocarina.ME_AADL.AADL_Instances.Nodes;
   package ATN renames Ocarina.ME_AADL.AADL_Tree.Nodes;
73
   package CV renames Ocarina.Backends.C_Values;
74
75
76
77
78
79
80
81
82
   package CTU renames Ocarina.Backends.C_Tree.Nutils;
   package CTN renames Ocarina.Backends.C_Tree.Nodes;
   package PHCR renames Ocarina.Backends.PO_HI_C.Runtime;
   package PKR renames Ocarina.Backends.POK_C.Runtime;

   Keyword_Suffix : constant String := "%C";
   --  Used to mark C keywords and avoid collision with other languages

   type Entity_Stack_Entry is record
83
84
      Current_File   : Node_Id;
      Current_Entity : Node_Id;
85
86
87
   end record;

   No_Depth : constant Int := -1;
88
89
90
91
92
93
   package Entity_Stack is new GNAT.Table
     (Entity_Stack_Entry,
      Int,
      No_Depth + 1,
      10,
      10);
94
95
96

   use Entity_Stack;

97
   procedure New_Operator (O : Operator_Type; I : String := "");
98
99
100
101
102
103
104

   ------------------------
   -- Add_Prefix_To_Name --
   ------------------------

   function Add_Prefix_To_Name
     (Prefix : String;
105
      Name   : Name_Id) return Name_Id
106
107
108
109
110
111
112
113
114
115
116
117
118
   is
   begin
      Set_Str_To_Name_Buffer (Prefix);
      Get_Name_String_And_Append (Name);
      return Name_Find;
   end Add_Prefix_To_Name;

   ------------------------
   -- Add_Suffix_To_Name --
   ------------------------

   function Add_Suffix_To_Name
     (Suffix : String;
119
      Name   : Name_Id) return Name_Id
120
121
122
123
124
125
126
127
128
129
130
131
132
   is
   begin
      Get_Name_String (Name);
      Add_Str_To_Name_Buffer (Suffix);
      return Name_Find;
   end Add_Suffix_To_Name;

   -----------------------------
   -- Remove_Suffix_From_Name --
   -----------------------------

   function Remove_Suffix_From_Name
     (Suffix : String;
133
      Name   : Name_Id) return Name_Id
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
   is
      Length   : Natural;
      Temp_Str : String (1 .. Suffix'Length);
   begin
      Set_Str_To_Name_Buffer (Suffix);
      Length := Name_Len;
      Get_Name_String (Name);
      if Name_Len > Length then
         Temp_Str := Name_Buffer (Name_Len - Length + 1 .. Name_Len);
         if Suffix = Temp_Str then
            Set_Str_To_Name_Buffer (Name_Buffer (1 .. Name_Len - Length));
            return Name_Find;
         end if;
      end if;
      return Name;
   end Remove_Suffix_From_Name;

   -------------------------
   -- Append_Node_To_List --
   -------------------------

   procedure Append_Node_To_List (E : Node_Id; L : List_Id) is
      Last : Node_Id;

   begin
      Last := CTN.Last_Node (L);
      if No (Last) then
         CTN.Set_First_Node (L, E);
      else
         CTN.Set_Next_Node (Last, E);
      end if;
      Last := E;
      while Present (Last) loop
         CTN.Set_Last_Node (L, Last);
         Last := CTN.Next_Node (Last);
      end loop;
   end Append_Node_To_List;

   -----------------------
   -- Insert_After_Node --
   -----------------------

   procedure Insert_After_Node (E : Node_Id; N : Node_Id) is
      Next : constant Node_Id := CTN.Next_Node (N);
   begin
      CTN.Set_Next_Node (N, E);
      CTN.Set_Next_Node (E, Next);
   end Insert_After_Node;

   ------------------------
   -- Insert_Before_Node --
   ------------------------

   procedure Insert_Before_Node (E : Node_Id; N : Node_Id; L : List_Id) is
      Entity : Node_Id;
   begin
      Entity := CTN.First_Node (L);
      if Entity = N then
         CTN.Set_Next_Node (E, Entity);
         CTN.Set_First_Node (L, E);
      else
         while Present (Entity) loop
            exit when CTN.Next_Node (Entity) = N;
            Entity := CTN.Next_Node (Entity);
         end loop;

         Insert_After_Node (E, Entity);
      end if;
   end Insert_Before_Node;

   ---------------
   -- Copy_Node --
   ---------------

   function Copy_Node (N : Node_Id) return Node_Id is
      C : Node_Id;
   begin
      case CTN.Kind (N) is
         when K_Defining_Identifier =>
            C := New_Node (K_Defining_Identifier);
            CTN.Set_Name (C, CTN.Name (N));
            CTN.Set_Corresponding_Node (C, CTN.Corresponding_Node (N));

         when K_Function_Specification =>
            C := New_Node (K_Function_Specification);
            CTN.Set_Defining_Identifier
220
221
              (C,
               CTU.Copy_Node (Defining_Identifier (N)));
222
223
224
225
226
            CTN.Set_Parameters (C, CTN.Parameters (N));
            CTN.Set_Return_Type (C, CTN.Return_Type (N));

         when K_Include_Clause =>
            C := New_Node (K_Include_Clause);
227
            CTN.Set_Header_Name (C, CTU.Copy_Node (Header_Name (N)));
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
            CTN.Set_Is_Local (C, CTN.Is_Local (N));

         when K_Literal =>
            C := New_Node (K_Literal);
            CTN.Set_Value (C, CTN.Value (N));

         when K_Ifdef_Clause =>
            C := New_Node (K_Ifdef_Clause);
            CTN.Set_Negation (C, Negation (N));
            CTN.Set_Then_Statements (C, Then_Statements (N));
            CTN.Set_Else_Statements (C, Else_Statements (N));
            CTN.Set_Clause (C, Copy_Node (Clause (N)));

         when others =>
            raise Program_Error;
      end case;
      return C;
   end Copy_Node;

   ---------------------
   -- Message_Comment --
   ---------------------

   function Message_Comment (M : Name_Id) return Node_Id is
      C : Node_Id;
   begin
      C := Make_C_Comment (M);
      return C;
   end Message_Comment;

   ---------------------
   -- Message_Comment --
   ---------------------

   function Message_Comment (M : String) return Node_Id is
      C : Node_Id;
   begin
      Set_Str_To_Name_Buffer (M);
      C := Make_C_Comment (Name_Find);
      return C;
   end Message_Comment;

   -----------
   -- Image --
   -----------

   function Image (T : Token_Type) return String is
      S : String := Token_Type'Image (T);
   begin
      To_Lower (S);
      return S (5 .. S'Last);
   end Image;

   -----------
   -- Image --
   -----------

   function Image (O : Operator_Type) return String is
      S : String := Operator_Type'Image (O);
   begin
      To_Lower (S);
      for I in S'First .. S'Last loop
         if S (I) = '_' then
            S (I) := ' ';
         end if;
      end loop;
      return S (4 .. S'Last);
   end Image;

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize is
   begin
      --  Initialize Nutils only once

      if Initialized then
         return;
      end if;

      Initialized := True;

      --  Keywords.
      for I in Keyword_Type loop
         New_Token (I);
      end loop;

      --  Graphic Characters
      New_Token (Tok_And, "&&");
      New_Token (Tok_Xor, "^");
      New_Token (Tok_Sharp, "#");
      New_Token (Tok_Or, "||");
      New_Token (Tok_Left_Brace, "{");
      New_Token (Tok_Right_Brace, "}");
      New_Token (Tok_Mod, "%");
      New_Token (Tok_Not, "!");
      New_Token (Tok_Ampersand, "&");
      New_Token (Tok_Minus, "-");
      New_Token (Tok_Underscore, "_");
      New_Token (Tok_Plus, "+");
      New_Token (Tok_Asterisk, "*");
      New_Token (Tok_Slash, "/");
      New_Token (Tok_Dot, ".");
      New_Token (Tok_Apostrophe, "'");
      New_Token (Tok_Left_Paren, "(");
      New_Token (Tok_Right_Paren, ")");
      New_Token (Tok_Left_Hook, "[");
      New_Token (Tok_Right_Hook, "]");
      New_Token (Tok_Comma, ",");
      New_Token (Tok_Less, "<");
      New_Token (Tok_Equal, "=");
      New_Token (Tok_Equal_Equal, "==");
      New_Token (Tok_Greater, ">");
      New_Token (Tok_Not_Equal, "!=");
      New_Token (Tok_Greater_Equal, ">=");
      New_Token (Tok_Less_Equal, "<=");
      New_Token (Tok_Colon, ":");
      New_Token (Tok_Greater_Greater, ">>");
      New_Token (Tok_Less_Less, "<<");
      New_Token (Tok_Quote, """");
      New_Token (Tok_Semicolon, ";");
      New_Token (Tok_Arrow, "->");
      New_Token (Tok_Vertical_Bar, "|");

      for O in Op_And .. Op_Or_Else loop
         New_Operator (O);
      end loop;
      New_Operator (Op_And_Symbol, "&");
      New_Operator (Op_Double_Asterisk, "**");
      New_Operator (Op_Asterisk, "**");
      New_Operator (Op_Minus, "-");
      New_Operator (Op_Plus, "+");
      New_Operator (Op_Asterisk, "*");
      New_Operator (Op_Slash, "/");
      New_Operator (Op_Less, "<");
      New_Operator (Op_Equal, "=");
      New_Operator (Op_Equal_Equal, "==");
      New_Operator (Op_Greater, ">");
      New_Operator (Op_Not_Equal, "!=");
      New_Operator (Op_Greater_Equal, ">=");
      New_Operator (Op_Less_Equal, "<=");
      New_Operator (Op_Greater_Greater, ">>");
      New_Operator (Op_Less_Less, "<<");
      New_Operator (Op_Semicolon, ";");
      New_Operator (Op_Arrow, "=>");
      New_Operator (Op_Vertical_Bar, "|");

      for A in Attribute_Id loop
         Set_Str_To_Name_Buffer (Attribute_Id'Image (A));
         Set_Str_To_Name_Buffer (Name_Buffer (3 .. Name_Len));
         GNAT.Case_Util.To_Mixed (Name_Buffer (1 .. Name_Len));
         AN (A) := Name_Find;
      end loop;

      for C in Constant_Id loop
         Set_Str_To_Name_Buffer (Constant_Id'Image (C));
         Set_Str_To_Name_Buffer (Name_Buffer (3 .. Name_Len));
         CONST (C) := To_Upper (Name_Find);
      end loop;

      for P in Parameter_Id loop
         Set_Str_To_Name_Buffer (Parameter_Id'Image (P));
         Set_Str_To_Name_Buffer (Name_Buffer (3 .. Name_Len));
         GNAT.Case_Util.To_Mixed (Name_Buffer (1 .. Name_Len));
         PN (P) := Name_Find;
      end loop;

      for F in Function_Id loop
         Set_Str_To_Name_Buffer (Function_Id'Image (F));
         Set_Str_To_Name_Buffer (Name_Buffer (3 .. Name_Len));
         FN (F) := To_Lower (Name_Find);
      end loop;

      for T in Type_Id loop
         Set_Str_To_Name_Buffer (Type_Id'Image (T));
         Set_Str_To_Name_Buffer (Name_Buffer (3 .. Name_Len));
         TN (T) := To_Lower (Name_Find);
      end loop;

      for V in Variable_Id loop
         Set_Str_To_Name_Buffer (Variable_Id'Image (V));
         Set_Str_To_Name_Buffer (Name_Buffer (3 .. Name_Len));
         VN (V) := To_Lower (Name_Find);
      end loop;

      for V in Member_Id loop
         Set_Str_To_Name_Buffer (Member_Id'Image (V));
         Set_Str_To_Name_Buffer (Name_Buffer (3 .. Name_Len));

         MN (V) := To_Lower (Name_Find);
      end loop;
   end Initialize;

   -----------
   -- Reset --
   -----------

   procedure Reset is
   begin
      Entity_Stack.Init;

      Initialized := False;
   end Reset;

   --------------
   -- Is_Empty --
   --------------

   function Is_Empty (L : List_Id) return Boolean is
   begin
      return L = No_List or else No (CTN.First_Node (L));
   end Is_Empty;

   ------------
   -- Length --
   ------------

   function Length (L : List_Id) return Natural is
      N : Node_Id;
      C : Natural := 0;
   begin
      if not Is_Empty (L) then
         N := CTN.First_Node (L);

         while Present (N) loop
            C := C + 1;
            N := CTN.Next_Node (N);
         end loop;
      end if;

      return C;
   end Length;

   --------------------
   -- Make_C_Comment --
   --------------------

   function Make_C_Comment
     (N                 : Name_Id;
468
      Has_Header_Spaces : Boolean := True) return Node_Id
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
   is
      C : Node_Id;
   begin
      C := New_Node (K_C_Comment);
      Set_Defining_Identifier (C, New_Node (K_Defining_Identifier));
      CTN.Set_Name (Defining_Identifier (C), N);
      CTN.Set_Has_Header_Spaces (C, Has_Header_Spaces);
      return C;
   end Make_C_Comment;

   -------------------------------
   -- Make_Assignment_Statement --
   -------------------------------

   function Make_Assignment_Statement
     (Variable_Identifier : Node_Id;
485
      Expression          : Node_Id) return Node_Id
486
487
488
489
490
491
492
493
494
495
496
497
498
499
   is
      N : Node_Id;
   begin
      N := New_Node (K_Assignment_Statement);
      Set_Defining_Identifier (N, Variable_Identifier);
      Set_Expression (N, Expression);
      return N;
   end Make_Assignment_Statement;

   ------------------------------
   -- Make_Defining_Identifier --
   ------------------------------

   function Make_Defining_Identifier
500
501
     (Name           : Name_Id;
      C_Conversion   : Boolean := True;
502
      Ada_Conversion : Boolean := False;
503
      Pointer        : Boolean := False) return Node_Id
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
   is
      N : Node_Id;

   begin
      N := New_Node (K_Defining_Identifier);
      if C_Conversion then
         CTN.Set_Name (N, To_C_Name (Name, Ada_Conversion));
      else
         CTN.Set_Name (N, Name);
      end if;

      if Pointer then
         CTN.Set_Is_Pointer (N, True);
      end if;

      return N;
   end Make_Defining_Identifier;

   ---------------------
   -- Make_Expression --
   ---------------------

   function Make_Expression
     (Left_Expr  : Node_Id;
      Operator   : Operator_Type := Op_None;
529
      Right_Expr : Node_Id       := No_Node) return Node_Id
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
   is
      N : Node_Id;
   begin
      N := New_Node (K_Expression);
      Set_Left_Expression (N, Left_Expr);
      Set_Operator (N, Operator_Type'Pos (Operator));
      Set_Right_Expression (N, Right_Expr);
      return N;
   end Make_Expression;

   ------------------------
   -- Make_For_Statement --
   ------------------------

   function Make_For_Statement
     (Defining_Identifier : Node_Id;
      Pre_Cond            : Node_Id;
      Condition           : Node_Id;
      Post_Cond           : Node_Id;
549
      Statements          : List_Id) return Node_Id
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
   is
      N : Node_Id;
   begin
      N := New_Node (K_For_Statement);
      Set_Defining_Identifier (N, Defining_Identifier);
      Set_Pre_Cond (N, Pre_Cond);
      Set_Condition (N, Condition);
      Set_Post_Cond (N, Post_Cond);
      Set_Statements (N, Statements);
      return N;
   end Make_For_Statement;

   ------------------
   -- Make_Literal --
   ------------------

   function Make_Literal (Value : Value_Id) return Node_Id is
      N : Node_Id;
   begin
      N := New_Node (K_Literal);
      CTN.Set_Value (N, Value);
      return N;
   end Make_Literal;

   -------------------------
   -- Make_Loop_Statement --
   -------------------------

   function Make_While_Statement
579
580
     (Condition  : Node_Id;
      Statements : List_Id) return Node_Id
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
   is
      N : Node_Id;
   begin
      N := New_Node (K_While_Statement);
      Set_Condition (N, Condition);
      Set_Statements (N, Statements);
      return N;
   end Make_While_Statement;

   --------------------------------
   -- Make_Full_Type_Declaration --
   --------------------------------

   function Make_Full_Type_Declaration
     (Defining_Identifier : Node_Id;
596
      Type_Definition     : Node_Id) return Node_Id
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
   is
      N : Node_Id;

   begin
      N := New_Node (K_Full_Type_Declaration);
      Set_Type_Name (N, Defining_Identifier);
      Set_Type_Definition (N, Type_Definition);
      return N;
   end Make_Full_Type_Declaration;

   -----------------------
   -- Make_If_Statement --
   -----------------------

   function Make_If_Statement
612
613
614
     (Condition       : Node_Id;
      Statements      : List_Id;
      Else_Statements : List_Id := No_List) return Node_Id
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
   is
      N : Node_Id;
   begin
      N := New_Node (K_If_Statement);
      Set_Condition (N, Condition);
      Set_Statements (N, Statements);
      Set_Else_Statements (N, Else_Statements);
      return N;
   end Make_If_Statement;

   ------------------
   -- Make_List_Id --
   ------------------

   function Make_List_Id
     (N1 : Node_Id;
      N2 : Node_Id := No_Node;
632
      N3 : Node_Id := No_Node) return List_Id
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
   is
      L : List_Id;
   begin
      L := New_List (K_List_Id);
      Append_Node_To_List (N1, L);
      if Present (N2) then
         Append_Node_To_List (N2, L);

         if Present (N3) then
            Append_Node_To_List (N3, L);
         end if;
      end if;
      return L;
   end Make_List_Id;

   ----------------------------------
   -- Make_Parameter_Specification --
   ----------------------------------

   function Make_Parameter_Specification
     (Defining_Identifier : Node_Id;
654
      Parameter_Type      : Node_Id := No_Node) return Node_Id
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
   is
      P : Node_Id;

   begin
      P := New_Node (K_Parameter_Specification);
      Set_Defining_Identifier (P, Defining_Identifier);
      Set_Parameter_Type (P, Parameter_Type);
      return P;
   end Make_Parameter_Specification;

   ---------------------------
   -- Make_Return_Statement --
   ---------------------------

   function Make_Return_Statement
670
     (Expression : Node_Id := No_Node) return Node_Id
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
   is
      N : Node_Id;
   begin
      N := New_Node (K_Return_Statement);
      if Expression /= No_Node then
         Set_Expression (N, Expression);
      end if;
      return N;
   end Make_Return_Statement;

   ---------------------------------
   -- Make_Function_Specification --
   ---------------------------------

   function Make_Function_Specification
686
687
688
     (Defining_Identifier : Node_Id;
      Parameters          : List_Id := No_List;
      Return_Type         : Node_Id := No_Node) return Node_Id
689
690
691
   is
      N : Node_Id;
   begin
692
693
      N := New_Node (K_Function_Specification);
      Set_Parameters (N, Parameters);
694
      Set_Defining_Identifier (N, Defining_Identifier);
695
      Set_Return_Type (N, Return_Type);
696
697
698
699
700
701
702
703
704
705
      return N;
   end Make_Function_Specification;

   ----------------------------------
   -- Make_Function_Implementation --
   ----------------------------------

   function Make_Function_Implementation
     (Specification : Node_Id;
      Declarations  : List_Id;
706
      Statements    : List_Id) return Node_Id
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
   is
      N : Node_Id;

   begin
      N := New_Node (K_Function_Implementation);
      Set_Specification (N, Specification);
      CTN.Set_Declarations (N, Declarations);
      Set_Statements (N, Statements);
      return N;
   end Make_Function_Implementation;

   -----------------------------
   -- Make_Member_Declaration --
   -----------------------------

   function Make_Member_Declaration
     (Defining_Identifier : Node_Id;
724
      Used_Type           : Node_Id) return Node_Id
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
   is
      P : Node_Id;
   begin
      P := New_Node (K_Member_Declaration);
      Set_Defining_Identifier (P, Defining_Identifier);
      Set_Used_Type (P, Used_Type);
      return P;
   end Make_Member_Declaration;

   -------------------------------
   -- Make_Variable_Declaration --
   -------------------------------

   function Make_Variable_Declaration
     (Defining_Identifier : Node_Id;
740
      Used_Type           : Node_Id) return Node_Id
741
742
743
744
745
746
747
748
749
750
751
752
753
   is
      P : Node_Id;
   begin
      P := New_Node (K_Variable_Declaration);
      Set_Defining_Identifier (P, Defining_Identifier);
      Set_Used_Type (P, Used_Type);
      return P;
   end Make_Variable_Declaration;

   ---------------------------
   -- Make_Variable_Address --
   ---------------------------

754
   function Make_Variable_Address (Expression : Node_Id) return Node_Id is
755
756
757
758
759
760
761
762
763
764
765
      P : Node_Id;
   begin
      P := New_Node (K_Variable_Address);
      Set_Expression (P, Expression);
      return P;
   end Make_Variable_Address;

   ------------------------------------
   -- Make_Extern_Entity_Declaration --
   ------------------------------------

766
   function Make_Extern_Entity_Declaration (Entity : Node_Id) return Node_Id is
767
768
769
770
771
772
773
774
775
776
777
778
779
      P : Node_Id;
   begin
      P := New_Node (K_Extern_Entity_Declaration);
      CTN.Set_Entity (P, Entity);
      return P;
   end Make_Extern_Entity_Declaration;

   ---------------------------
   -- Make_Struct_Aggregate --
   ---------------------------

   function Make_Struct_Aggregate
     (Defining_Identifier : Node_Id := No_Node;
780
      Members             : List_Id) return Node_Id
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
   is
      N : Node_Id;
   begin
      N := New_Node (K_Struct_Aggregate);
      if Defining_Identifier /= No_Node then
         Set_Defining_Identifier (N, Defining_Identifier);
      end if;
      Set_Struct_Members (N, Members);
      return N;
   end Make_Struct_Aggregate;

   --------------------------
   -- Make_Union_Aggregate --
   --------------------------

   function Make_Union_Aggregate
     (Defining_Identifier : Node_Id := No_Node;
798
      Members             : List_Id) return Node_Id
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
   is
      N : Node_Id;
   begin
      N := New_Node (K_Union_Aggregate);
      if Defining_Identifier /= No_Node then
         Set_Defining_Identifier (N, Defining_Identifier);
      end if;
      Set_Union_Members (N, Members);
      return N;
   end Make_Union_Aggregate;

   -------------------------
   -- Make_Enum_Aggregate --
   -------------------------

814
   function Make_Enum_Aggregate (Members : List_Id) return Node_Id is
815
816
817
818
819
820
821
822
823
824
825
826
      N : Node_Id;
   begin
      N := New_Node (K_Enum_Aggregate);
      Set_Enum_Members (N, Members);
      return N;
   end Make_Enum_Aggregate;

   -----------------------
   -- Make_Call_Profile --
   -----------------------

   function Make_Call_Profile
827
828
     (Defining_Identifier : Node_Id;
      Parameters          : List_Id := No_List) return Node_Id
829
830
831
832
833
834
835
836
837
838
839
840
841
842
   is
      N : Node_Id;
   begin
      N := New_Node (K_Call_Profile);
      Set_Defining_Identifier (N, Defining_Identifier);
      Set_Parameters (N, Parameters);
      return N;
   end Make_Call_Profile;

   ---------------------
   -- Make_Macro_Call --
   ---------------------

   function Make_Macro_Call
843
844
     (Defining_Identifier : Node_Id;
      Parameters          : List_Id := No_List) return Node_Id
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
   is
      N : Node_Id;
   begin
      N := New_Node (K_Macro_Call);
      Set_Defining_Identifier (N, Defining_Identifier);
      Set_Parameters (N, Parameters);
      return N;
   end Make_Macro_Call;

   -------------------------
   -- Make_Type_Attribute --
   -------------------------

   function Make_Type_Attribute
     (Designator : Node_Id;
860
      Attribute  : Attribute_Id) return Node_Id
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
   is
      procedure Get_Scoped_Name_String (S : Node_Id);

      ----------------------------
      -- Get_Scoped_Name_String --
      ----------------------------

      procedure Get_Scoped_Name_String (S : Node_Id) is
      begin
         Get_Name_String_And_Append (CTN.Name (Defining_Identifier (S)));
      end Get_Scoped_Name_String;

   begin
      Name_Len := 0;
      Get_Scoped_Name_String (Designator);
      Add_Char_To_Name_Buffer (''');
      Get_Name_String_And_Append (AN (Attribute));
      return Make_Defining_Identifier (Name_Find);
   end Make_Type_Attribute;

   --------------------------
   -- Make_Type_Conversion --
   --------------------------

   function Make_Type_Conversion
     (Subtype_Mark : Node_Id;
887
      Expression   : Node_Id) return Node_Id
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
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
   is
      N : Node_Id;
   begin
      N := New_Node (K_Type_Conversion);
      Set_Subtype_Mark (N, Subtype_Mark);
      Set_Expression (N, Expression);
      return N;
   end Make_Type_Conversion;

   -------------------------
   -- Make_Comment_Header --
   -------------------------

   procedure Make_Comment_Header (Header : List_Id) is
      N : Node_Id;
   begin
      --  Appending the comment header lines to the file header

      Set_Str_To_Name_Buffer
        ("***************************************************");
      N := Make_C_Comment (Name_Find, False);
      Append_Node_To_List (N, Header);

      Set_Str_To_Name_Buffer
        ("This file was automatically generated by Ocarina ");
      N := Make_C_Comment (Name_Find);
      Append_Node_To_List (N, Header);

      Set_Str_To_Name_Buffer
        ("Do NOT hand-modify this file, as your            ");
      N := Make_C_Comment (Name_Find);
      Append_Node_To_List (N, Header);

      Set_Str_To_Name_Buffer
        ("changes will be lost when you re-run Ocarina     ");
      N := Make_C_Comment (Name_Find);
      Append_Node_To_List (N, Header);

      Set_Str_To_Name_Buffer
        ("***************************************************");
      N := Make_C_Comment (Name_Find, False);
      Append_Node_To_List (N, Header);

   end Make_Comment_Header;

   -----------------
   -- Next_N_Node --
   -----------------

   function Next_N_Node (N : Node_Id; Num : Natural) return Node_Id is
      Result : Node_Id := N;
   begin
      for I in 1 .. Num loop
         Result := CTN.Next_Node (Result);
      end loop;

      return Result;
   end Next_N_Node;

   --------------
   -- New_List --
   --------------

   function New_List
     (Kind : CTN.Node_Kind;
953
954
      From : Node_Id := No_Node) return List_Id
   is
955
956
957
958
      N : Node_Id;

   begin
      CTN.Entries.Increment_Last;
959
      N                     := CTN.Entries.Last;
960
961
962
      CTN.Entries.Table (N) := CTN.Default_Node;
      Set_Kind (N, Kind);
      if Present (From) then
963
         CTN.Set_Loc (N, CTN.Loc (From));
964
      else
965
         CTN.Set_Loc (N, No_Location);
966
967
968
969
970
971
972
973
974
975
      end if;
      return List_Id (N);
   end New_List;

   --------------
   -- New_Node --
   --------------

   function New_Node
     (Kind : CTN.Node_Kind;
976
      From : Node_Id := No_Node) return Node_Id
977
978
979
980
   is
      N : Node_Id;
   begin
      CTN.Entries.Increment_Last;
981
      N                     := CTN.Entries.Last;
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
      CTN.Entries.Table (N) := CTN.Default_Node;
      CTN.Set_Kind (N, Kind);

      if Present (From) then
         CTN.Set_Loc (N, AIN.Loc (From));
      else
         CTN.Set_Loc (N, No_Location);
      end if;

      return N;
   end New_Node;

   ---------------
   -- New_Token --
   ---------------

998
   procedure New_Token (T : Token_Type; I : String := "") is
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
      Name : Name_Id;
   begin
      if T in Keyword_Type then
         --  Marking the token image as a keyword for fas searching
         --  purpose, we add the prefix to avoir collision with other
         --  languages keywords

         Set_Str_To_Name_Buffer (Image (T));
         Name := Name_Find;
         Name := Add_Suffix_To_Name (Keyword_Suffix, Name);
yoogx's avatar
yoogx committed
1009
1010
         Set_Name_Table_Byte
           (Name, Ocarina.Types.Byte (Token_Type'Pos (T) + 1));
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022

         Set_Str_To_Name_Buffer (Image (T));
      else
         Set_Str_To_Name_Buffer (I);
      end if;
      Token_Image (T) := Name_Find;
   end New_Token;

   ------------------
   -- New_Operator --
   ------------------

1023
   procedure New_Operator (O : Operator_Type; I : String := "") is
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
   begin
      if O in Keyword_Operator then
         Set_Str_To_Name_Buffer (Image (O));
      else
         Set_Str_To_Name_Buffer (I);
      end if;
      Operator_Image (Operator_Type'Pos (O)) := Name_Find;
   end New_Operator;

   ----------------
   -- Pop_Entity --
   ----------------

   procedure Pop_Entity is
   begin
      if Last > No_Depth then
         Decrement_Last;
      end if;
   end Pop_Entity;

   -----------------
   -- Push_Entity --
   -----------------

   procedure Push_Entity (E : Node_Id) is
   begin
      Increment_Last;
      Table (Last).Current_Entity := E;
   end Push_Entity;

   ---------------------------
   -- Remove_Node_From_List --
   ---------------------------

   procedure Remove_Node_From_List (E : Node_Id; L : List_Id) is
      C : Node_Id;

   begin
      C := CTN.First_Node (L);
      if C = E then
         CTN.Set_First_Node (L, CTN.Next_Node (E));
         if CTN.Last_Node (L) = E then
            CTN.Set_Last_Node (L, No_Node);
         end if;
      else
         while Present (C) loop
            if CTN.Next_Node (C) = E then
               CTN.Set_Next_Node (C, CTN.Next_Node (E));
               if CTN.Last_Node (L) = E then
                  CTN.Set_Last_Node (L, C);
               end if;
               exit;
            end if;
            C := CTN.Next_Node (C);
         end loop;
      end if;
   end Remove_Node_From_List;

   ---------------------
   -- Set_Main_Source --
   ---------------------

   procedure Set_Main_Source (N : Node_Id := No_Node) is
      X : Node_Id := N;
   begin
      if No (X) then
         X := Table (Last).Current_Entity;
      end if;
      Table (Last).Current_File := Main_Source (X);
   end Set_Main_Source;

   ---------------------
   -- Set_Main_Header --
   ---------------------

   procedure Set_Main_Header (N : Node_Id := No_Node) is
      X : Node_Id := N;
   begin
      if No (X) then
         X := Table (Last).Current_Entity;
      end if;
      Table (Last).Current_File := Main_Header (X);
   end Set_Main_Header;

   ---------------
   -- To_C_Name --
   ---------------

1112
   function To_C_Name
1113
1114
1115
     (N             : Name_Id;
      Ada_Style     : Boolean := False;
      Keyword_Check : Boolean := True) return Name_Id
1116
   is
1117
1118
      Name      : Name_Id;
      Test_Name : Name_Id;
1119
      V         : Ocarina.Types.Byte;
1120
1121
1122
1123
   begin
      Get_Name_String (Normalize_Name (N, Ada_Style));
      Name := Name_Find;

1124
      if Keyword_Check then
1125

1126
1127
         --  If the identifier collides with a C reserved word insert
         --  "AADL_" string before the identifier.
1128

1129
         Test_Name := Add_Suffix_To_Name (Keyword_Suffix, Name);
1130
         V         := Get_Name_Table_Byte (Test_Name);
1131
1132
1133
1134
1135
1136
         if V > 0 then
            Set_Str_To_Name_Buffer ("AADL_");
            Get_Name_String_And_Append (Name);
            Name := Name_Find;
         end if;
      end if;
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
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
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
      return To_Lower (Name);
   end To_C_Name;

   ----------------------------
   -- Conventional_Base_Name --
   ----------------------------

   function Conventional_Base_Name (N : Name_Id) return Name_Id is
   begin
      Get_Name_String (N);

      for Index in 1 .. Name_Len loop
         Name_Buffer (Index) := To_Lower (Name_Buffer (Index));
      end loop;

      return Name_Find;
   end Conventional_Base_Name;

   -------------------------
   -- Set_Activity_Source --
   -------------------------

   procedure Set_Activity_Source (N : Node_Id := No_Node) is
      X : Node_Id := N;
   begin
      if No (X) then
         X := Table (Last).Current_Entity;
      end if;
      Table (Last).Current_File := Activity_Source (X);
   end Set_Activity_Source;

   ---------------------------
   -- Set_Deployment_Header --
   ---------------------------

   procedure Set_Deployment_Header (N : Node_Id := No_Node) is
      X : Node_Id := N;
   begin
      if No (X) then
         X := Table (Last).Current_Entity;
      end if;
      Table (Last).Current_File := Deployment_Header (X);
   end Set_Deployment_Header;

   ---------------------------
   -- Set_Deployment_Source --
   ---------------------------

   procedure Set_Deployment_Source (N : Node_Id := No_Node) is
      X : Node_Id := N;
   begin
      if No (X) then
         X := Table (Last).Current_Entity;
      end if;
      Table (Last).Current_File := Deployment_Source (X);
   end Set_Deployment_Source;

   -------------------------
   -- Set_Activity_Header --
   -------------------------

   procedure Set_Activity_Header (N : Node_Id := No_Node) is
      X : Node_Id := N;
   begin
      if No (X) then
         X := Table (Last).Current_Entity;
      end if;
      Table (Last).Current_File := Activity_Header (X);
   end Set_Activity_Header;

   ------------------------
   -- Set_Request_Header --
   ------------------------

   procedure Set_Request_Header (N : Node_Id := No_Node) is
      X : Node_Id := N;
   begin
      if No (X) then
         X := Table (Last).Current_Entity;
      end if;
      Table (Last).Current_File := Request_Header (X);
   end Set_Request_Header;

   ------------------------
   -- Set_Request_Source --
   ------------------------

   procedure Set_Request_Source (N : Node_Id := No_Node) is
      X : Node_Id := N;
   begin
      if No (X) then
         X := Table (Last).Current_Entity;
      end if;
      Table (Last).Current_File := Request_Source (X);
   end Set_Request_Source;

   ----------------------------
   -- Set_Marshallers_Source --
   ----------------------------
   procedure Set_Marshallers_Source (N : Node_Id := No_Node) is
      X : Node_Id := N;
   begin
      if No (X) then
         X := Table (Last).Current_Entity;
      end if;
      Table (Last).Current_File := Marshallers_Source (X);
   end Set_Marshallers_Source;

   ----------------------
   -- Set_Types_Header --
   ----------------------

   procedure Set_Types_Header (N : Node_Id := No_Node) is
      X : Node_Id := N;
   begin
      if No (X) then
         X := Table (Last).Current_Entity;
      end if;
      Table (Last).Current_File := Types_Header (X);
   end Set_Types_Header;

   ----------------------------
   -- Set_Marshallers_Header --
   ----------------------------

   procedure Set_Marshallers_Header (N : Node_Id := No_Node) is
      X : Node_Id := N;
   begin
      if No (X) then
         X := Table (Last).Current_Entity;
      end if;
      Table (Last).Current_File := Marshallers_Header (X);
   end Set_Marshallers_Header;

   ----------------------------
   -- Set_Subprograms_Header --
   ----------------------------

   procedure Set_Subprograms_Header (N : Node_Id := No_Node) is
      X : Node_Id := N;
   begin
      if No (X) then
         X := Table (Last).Current_Entity;
      end if;
      Table (Last).Current_File := Subprograms_Header (X);
   end Set_Subprograms_Header;

   -----------------------
   -- Set_Naming_Header --
   -----------------------

   procedure Set_Naming_Header (N : Node_Id := No_Node) is
      X : Node_Id := N;
   begin
      if No (X) then
         X := Table (Last).Current_Entity;
      end if;
      Table (Last).Current_File := Naming_Header (X);
   end Set_Naming_Header;

   -----------------------
   -- Set_Naming_Source --
   -----------------------

   procedure Set_Naming_Source (N : Node_Id := No_Node) is
      X : Node_Id := N;
   begin
      if No (X) then
         X := Table (Last).Current_Entity;
      end if;
      Table (Last).Current_File := Naming_Source (X);
   end Set_Naming_Source;

   ----------------------------
   -- Set_Subprograms_Source --
   ----------------------------

   procedure Set_Subprograms_Source (N : Node_Id := No_Node) is
      X : Node_Id := N;
   begin
      if No (X) then
         X := Table (Last).Current_Entity;
      end if;
      Table (Last).Current_File := Subprograms_Source (X);
   end Set_Subprograms_Source;

   ----------------------
   -- Set_Types_Source --
   ----------------------

   procedure Set_Types_Source (N : Node_Id := No_Node) is
      X : Node_Id := N;
   begin
      if No (X) then
         X := Table (Last).Current_Entity;
      end if;
      Table (Last).Current_File := Types_Source (X);
   end Set_Types_Source;

   --------------------
   -- Current_Entity --
   --------------------

   function Current_Entity return Node_Id is
   begin
      if Last = No_Depth then
         return No_Node;
      else
         return Table (Last).Current_Entity;
      end if;
   end Current_Entity;

   ------------------
   -- Current_File --
   ------------------

   function Current_File return Node_Id is
   begin
      if Last = No_Depth then
         return No_Node;
      else
         return Table (Last).Current_File;
      end if;
   end Current_File;

   ----------------------
   -- Make_Source_File --
   ----------------------

   function Make_Source_File (Identifier : Node_Id) return Node_Id is
      File : Node_Id;
   begin
      File := New_Node (K_Source_File);
      Set_Defining_Identifier (File, Identifier);
      Set_Corresponding_Node (Identifier, File);

      CTN.Set_Included_Headers (File, New_List (K_Header_List));
      CTN.Set_Declarations (File, New_List (CTN.K_Declaration_List));
      Make_Comment_Header (CTN.Declarations (File));

      return File;
   end Make_Source_File;

   ----------------------
   -- Make_Header_File --
   ----------------------

   function Make_Header_File (Identifier : Node_Id) return Node_Id is
      File : Node_Id;
   begin
      File := New_Node (K_Header_File);
      Set_Defining_Identifier (File, Identifier);
      Set_Corresponding_Node (Identifier, File);

      CTN.Set_Included_Headers (File, New_List (K_Header_List));
      CTN.Set_Declarations (File, New_List (CTN.K_Declaration_List));
      Make_Comment_Header (CTN.Declarations (File));

      return File;
   end Make_Header_File;

   -----------------
   -- Add_Include --
   -----------------

1402
1403
1404
1405
   procedure Add_Include (E : Node_Id; Preserve_Case : Boolean := False) is
      W                : Node_Id;
      N                : Name_Id;
      M                : Name_Id;
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
      Existing_Include : Node_Id;
   begin
      --  Get the info associated to the obtained name in the hash
      --  table and check whether it is already set to a value
      --  different from 0 (No_Node) which means that the withed
      --  entity is already in the withed package list. In this case
      --  try to enrich the exisiting with clause with eventual 'use',
      --  'elaborate' or warning disabling clauses.
      Get_Name_String (CTN.Name (Defining_Identifier (Current_File)));

      if Kind (Current_File) = K_Header_File then
         --  If the included file is the file in which we add the
         --  include, we return immediatly, because a file don't
         --  include itself
1420
1421
1422
         if To_Lower (CTN.Name (CTN.Header_Name (E))) =
           To_Lower (CTN.Name (Defining_Identifier (Current_File)))
         then
1423
1424
1425
1426
1427
1428
1429
1430
1431
            return;
         end if;

         Set_Str_To_Name_Buffer (Name_Buffer (1 .. Name_Len) & ".h");
      else
         Set_Str_To_Name_Buffer (Name_Buffer (1 .. Name_Len) & ".c");
      end if;

      Get_Name_String_And_Append (CTN.Name (CTN.Header_Name (E)));
1432
1433
      Get_Name_String_And_Append
        (CTN.Name (CTN.Entity (Distributed_Application_Unit (Current_File))));
1434
1435

      if Distributed_Application
1436
1437
1438
          (Entity (Distributed_Application_Unit (Current_File))) /=
        No_Node
      then
1439
         Get_Name_String_And_Append
1440
1441
1442
           (CTN.Name
              (Distributed_Application
                 (Entity (Distributed_Application_Unit (Current_File)))));
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
      end if;

      if Preserve_Case then
         N := Name_Find;
      else
         N := To_Lower (Name_Find);
      end if;

      Existing_Include := Node_Id (Get_Name_Table_Info (N));

      --  If the file was already included, we return immediatly
      if Present (Existing_Include) then
         return;
      end if;

      --  Else, we add the corresponding header file to included files
      Get_Name_String (CTN.Name (Header_Name ((E))));

      M := Name_Find;
1462
1463
1464
1465
      W :=
        Make_Include_Clause
          (Make_Defining_Identifier (M, not Preserve_Case),
           Is_Local (E));
1466
1467
1468
1469
1470
1471
1472
1473
1474
      Set_Name_Table_Info (N, Int (W));

      Append_Node_To_List (W, Included_Headers (Current_File));
   end Add_Include;

   ---------------------------
   -- Make_Define_Statement --
   ---------------------------

1475
1476
1477
1478
   function Make_Define_Statement
     (Defining_Identifier : Node_Id;
      Value               : Node_Id) return Node_Id
   is
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
      N : Node_Id;
   begin
      N := New_Node (K_Define_Statement);
      Set_Defining_Identifier (N, Defining_Identifier);
      Set_Defined_Value (N, Value);
      return N;
   end Make_Define_Statement;

   -----------------------
   -- Make_Pointer_Type --
   -----------------------

   function Make_Pointer_Type (Used_Type : Node_Id) return Node_Id is
      N : Node_Id;
   begin
      N := New_Node (K_Pointer_Type);
      Set_Used_Type (N, Used_Type);
      return (N);
   end Make_Pointer_Type;

   ------------------------
   -- Make_Constant_Type --
   ------------------------

   function Make_Constant_Type (Used_Type : Node_Id) return Node_Id is
      N : Node_Id;
   begin
      N := New_Node (K_Constant_Type);
      Set_Used_Type (N, Used_Type);
      return (N);
   end Make_Constant_Type;

   ----------------------------
   -- Make_Member_Designator --
   ----------------------------

   function Make_Member_Designator
     (Defining_Identifier : Node_Id;
      Aggregate_Name      : Node_Id;
1518
1519
      Is_Pointer          : Boolean := False) return Node_Id
   is
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
      N : Node_Id;
   begin
      N := New_Node (K_Member_Designator);
      Set_Defining_Identifier (N, Defining_Identifier);
      Set_Is_Pointer (N, Is_Pointer);
      Set_Aggregate_Name (N, Aggregate_Name);
      return (N);
   end Make_Member_Designator;

   ----------------------------
   -- Make_Array_Declaration --
   ----------------------------

   function Make_Array_Declaration
     (Defining_Identifier : Node_Id;
1535
1536
      Array_Size          : Node_Id) return Node_Id
   is
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
      N : Node_Id;
   begin
      N := New_Node (K_Array_Declaration);
      Set_Defining_Identifier (N, Defining_Identifier);
      Set_Array_Size (N, Array_Size);
      return (N);
   end Make_Array_Declaration;

   -----------------------
   -- Make_Array_Values --
   -----------------------

1549
   function Make_Array_Values (Values : List_Id := No_List) return Node_Id is
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
      L : List_Id;
      N : Node_Id;
   begin
      N := New_Node (K_Array_Values);
      if not Present (Values) then
         L := New_List (CTN.K_Enumeration_Literals);
         Set_Values (N, L);
      else
         Set_Values (N, Values);
      end if;
      return (N);
   end Make_Array_Values;

   ----------------------
   -- Make_Array_Value --
   ----------------------

1567
1568
1569
1570
   function Make_Array_Value
     (Array_Name : Node_Id;
      Array_Item : Node_Id) return Node_Id
   is
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
      N : Node_Id;
   begin
      N := New_Node (K_Array_Value);
      Set_Defining_Identifier (N, Array_Name);
      Set_Array_Item (N, Array_Item);
      return (N);
   end Make_Array_Value;

   ---------------------------
   -- Make_Switch_Statement --
   ---------------------------

   function Make_Switch_Statement
1584
1585
1586
     (Expression   : Node_Id;
      Alternatives : List_Id) return Node_Id
   is
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
      N : Node_Id;
   begin
      N := New_Node (K_Switch_Statement);
      Set_Expression (N, Expression);
      Set_Alternatives (N, Alternatives);
      return (N);
   end Make_Switch_Statement;

   -----------------------------
   -- Make_Switch_Alternative --
   -----------------------------

   function Make_Switch_Alternative
1600
1601
1602
     (Labels     : List_Id;
      Statements : List_Id) return Node_Id
   is
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
      N : Node_Id;
   begin
      N := New_Node (K_Switch_Alternative);
      Set_Labels (N, Labels);
      Set_Statements (N, Statements);
      return (N);
   end Make_Switch_Alternative;

   --------------------------
   -- Handle_Call_Sequence --
   --------------------------

   procedure Handle_Call_Sequence
jdelange's avatar
jdelange committed
1616
1617
1618
1619
1620
     (Caller            : Node_Id;
      Call_Seq          : Node_Id;
      Declarations      : List_Id;
      Statements        : List_Id;
      Containing_Device : Node_Id := No_Node)
1621
   is
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
      Destination_F : Node_Id;
      Source_F      : Node_Id;
      Source_Parent : Node_Id;
      Param_Value   : Node_Id;
      Call_Profile  : List_Id;
      Spg           : Node_Id;
      Spg_Call      : Node_Id;
      N             : Node_Id;
      F             : Node_Id;
      M             : Node_Id;
      Owner         : Node_Id;
      Declaration   : Node_Id;
      Hybrid        : constant Boolean :=
        AINU.Is_Subprogram (Caller)
        and then
          Properties.Get_Subprogram_Kind (Caller) =
          Properties.Subprogram_Hybrid_Ada_95; --  XXX why Ada ?
1639
1640
1641
1642
   begin
      --  The lists have to be created

      if Declarations = No_List or else Statements = No_List then
1643
1644
1645
         raise Program_Error
           with "Lists have to be created before any call " &
           "to Handle_Call_Sequence";
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
      end if;

      --  The call sequence generally contains at least one call to a
      --  subprogram.

      if AINU.Is_Empty (AIN.Subprogram_Calls (Call_Seq)) then
         Display_Located_Error
           (AIN.Loc (Call_Seq),
            "Empty call sequence",
            Fatal   => False,
            Warning => True);
         return;
      end if;
      Spg_Call := AIN.First_Node (AIN.Subprogram_Calls (Call_Seq));

      while Present (Spg_Call) loop
1662
         Spg          := AIN.Corresponding_Instance (Spg_Call);
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
         Call_Profile := New_List (CTN.K_List_Id);

         if not AINU.Is_Empty (AIN.Features (Spg)) then
            F := AIN.First_Node (AIN.Features (Spg));
            while Present (F) loop
               if Kind (F) = K_Subcomponent_Access_Instance
                 and then Get_Current_Backend_Kind = PolyORB_Kernel_C
               then
                  --  This case is specific to POK since we don't
                  --  handle the shared data with the same patterns as
                  --  in PolyORB-HI-C. This could be updated later.

1675
1676
1677
                  Param_Value :=
                    Make_Variable_Address
                      (Map_C_Defining_Identifier (Get_Accessed_Data (F)));
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689

                  Append_Node_To_List (Param_Value, Call_Profile);

               elsif AIN.Kind (F) = AIN.K_Parameter_Instance
                 and then AIN.Is_Out (F)
               then
                  --  Raise an error if the parameter is not connected
                  --  to any source.

                  if AINU.Length (AIN.Destinations (F)) = 0 then
                     Display_Located_Error
                       (AIN.Loc (F),
1690
1691
                        "This OUT parameter is not connected to" &
                        " any destination",
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
                        Fatal => True);
                  elsif AINU.Length (AIN.Destinations (F)) > 1 then
                     Display_Located_Error
                       (AIN.Loc (F),
                        "This OUT parameter has too many destinations",
                        Fatal => True);
                  end if;

                  --  At this point, we have a subprogram call
                  --  parameter that has exactly one destination.

1703
1704
                  Destination_F :=
                    AIN.Item (AIN.First_Node (AIN.Destinations (F)));
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717

                  --  For each OUT parameter, we declare a local
                  --  variable if the OUT parameter is connected to
                  --  another subprogram call or if the caller is a
                  --  thread. Otherwise, we use the corresponding
                  --  caller subprogram parameter.

                  --  The parameter association value takes 3 possible
                  --  values (see the (1), (2) and (3) comments below.

                  if AINU.Is_Thread (Caller)
                    or else AIN.Parent_Component (Destination_F) /= Caller
                  then
1718
                     --  (1) Here, we map the variable name from the
1719
1720
1721
1722
1723
                     --  subprogram *call* name and the feature
                     --  name. This avoids name clashing when a thread
                     --  calls twice the same subprogram.

                     if Get_Current_Backend_Kind = PolyORB_HI_C then
1724
1725
1726
                        M :=
                          Map_C_Data_Type_Designator
                            (Corresponding_Instance (F));
1727
                        Declaration :=
1728
1729
1730
1731
1732
1733
1734
                          Make_Variable_Declaration
                            (Defining_Identifier =>
                               Make_Defining_Identifier
                                 (Map_C_Variable_Name
                                    (F,
                                     Request_Variable => True)),
                             Used_Type => M);
1735

1736
                        Append_Node_To_List (Declaration, Declarations);
1737

1738
1739
1740
1741
1742
                        M :=
                          Make_Defining_Identifier
                            (Map_C_Variable_Name
                               (F,
                                Request_Variable => True));
1743
1744

                     elsif Get_Current_Backend_Kind = PolyORB_Kernel_C then
1745
1746
1747
                        M :=
                          Map_C_Data_Type_Designator
                            (Corresponding_Instance (F));
1748
1749

                        Declaration :=
1750
1751
1752
1753
1754
                          Make_Variable_Declaration
                            (Defining_Identifier =>
                               Make_Defining_Identifier
                                 (Map_Port_Data (Destination_F)),
                             Used_Type => M);
1755
1756
1757

                        Append_Node_To_List (Declaration, Declarations);

1758
1759
1760
                        M :=
                          Make_Defining_Identifier
                            (Map_Port_Data (Destination_F));
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
                     end if;

                     Param_Value := Make_Variable_Address (M);

                  elsif Hybrid then
                     --  (2) If the calleD parameter is connected to
                     --      the calleR parameter and then the calleR
                     --      IS hybrid, then we use the 'Status'
                     --      record field corresponding to the calleR
                     --      parameter.

1772
1773
1774
1775
1776
1777
                     Param_Value :=
                       Make_Member_Designator
                         (Make_Defining_Identifier
                            (To_C_Name
                               (AIN.Display_Name (AIN.Identifier (F)))),
                          Make_Defining_Identifier (PN (P_Status)));
yoogx's avatar
yoogx committed
1778

1779
1780
1781
1782
                  else
                     --  (3) If the calleD parameter is connected to
                     --      the calleR parameter and then then calleR
                     --      is NOT hybrid, then we use simply the
1783
                     --      corresponding parameter of the calleR.
1784

1785
1786
1787
1788
1789
                     Param_Value :=
                       Make_Defining_Identifier
                         (To_C_Name
                            (AIN.Display_Name
                               (AIN.Identifier (Destination_F))));
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
                  end if;

                  --  For each OUT parameter we build a parameter
                  --  association of the actual profile of the
                  --  implmentaion subprogram call <Param> =>
                  --  <Param_Value>.

                  CTU.Append_Node_To_List (Param_Value, Call_Profile);

               elsif AIN.Kind (F) = AIN.K_Parameter_Instance
                 and then AIN.Is_In (F)
               then
                  --  Raise an error if the parameter is not connected
                  --  to any source.

                  if AINU.Length (AIN.Sources (F)) = 0 then
                     Display_Located_Error
                       (AIN.Loc (F),
1808
1809
                        "This IN parameter is not connected to" &
                        " any source",
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
                        Fatal => True);
                  elsif AINU.Length (AIN.Sources (F)) > 1 then
                     Display_Located_Error
                       (AIN.Loc (F),
                        "This IN parameter has too many sources",
                        Fatal => True);
                  end if;

                  --  Here we have an IN parameter with exactly one
                  --  source.

                  Source_F := AIN.Item (AIN.First_Node (AIN.Sources (F)));

                  --  Get the source feature parent

                  Source_Parent := AIN.Parent_Component (Source_F);

                  --  The parameter value of the built parameter
                  --  association can take 4 different values. (see
                  --  comments (1), (2), (3) and (4) above).

                  if AINU.Is_Thread (Source_Parent) then
                     --  (1) If the Parent of 'Source_F' is a thread,
                     --  then we use the '<Thread>_Job_Req' record
                     --  field corresponding to F.

                     if Get_Current_Backend_Kind = PolyORB_HI_C then
                        Param_Value :=
                          Make_Member_Designator
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
                            (Defining_Identifier =>
                               Make_Member_Designator
                                 (Defining_Identifier =>
                                    Make_Member_Designator
                                      (Defining_Identifier =>
                                         Make_Defining_Identifier
                                           (Map_C_Enumerator_Name (Source_F)),
                                       Aggregate_Name =>
                                         Make_Defining_Identifier
                                           (Map_C_Enumerator_Name (Source_F))),
                                  Aggregate_Name =>
                                    Make_Defining_Identifier (MN (M_Vars))),
                             Aggregate_Name =>
                               Make_Defining_Identifier
                                 (Map_C_Variable_Name
                                    (Source_F,
                                     Port_Request => True)));
1856
                     else
1857
1858
1859
                        M :=
                          Map_C_Data_Type_Designator
                            (Corresponding_Instance (F));
1860
1861

                        Declaration :=
1862
1863
1864
1865
1866
                          Make_Variable_Declaration
                            (Defining_Identifier =>
                               Make_Defining_Identifier
                                 (Map_Port_Data (Source_F)),
                             Used_Type => M);
1867
1868
1869

                        Append_Node_To_List (Declaration, Declarations);

1870
1871
                        Param_Value :=
                          Make_Defining_Identifier (Map_Port_Data (Source_F));
1872
1873
1874
1875
1876
1877
1878
                     end if;

                  elsif Source_Parent /= Caller then
                     --  (2) If the the source call is different from
                     --      the englobing subprogram, we use the
                     --      formerly declared variable.

1879
1880
1881
1882
1883
                     Param_Value :=
                       Make_Defining_Identifier
                         (Map_C_Variable_Name
                            (Source_F,
                             Request_Variable => True));
1884
1885
1886
1887
1888
1889
1890

                  elsif Hybrid then
                     --  (3) If the calleD parameter is connected to
                     --      the calleR parameter then calleR IS
                     --      hybrid, then we use the 'Status' record field
                     --      corresponding to the calleR parameter.

1891
1892
1893
1894
1895
1896
                     Param_Value :=
                       Make_Member_Designator
                         (Make_Defining_Identifier
                            (To_C_Name
                               (AIN.Display_Name (AIN.Identifier (Source_F)))),
                          Make_Defining_Identifier (PN (P_Status)));
1897
1898
1899
1900
1901
1902
                  else
                     --  (4) If the calleD parameter is connected to
                     --      the calleR parameter and then then calleR
                     --      is NOT hybrid, then we use simply the
                     --      corresponding paremeter of the calleR.

1903
1904
1905
1906
                     Param_Value :=
                       Make_Defining_Identifier
                         (To_C_Name
                            (AIN.Display_Name (AIN.Identifier (Source_F))));
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
                  end if;

                  --  For each IN parameter we build a parameter
                  --  association association of the actual profile of
                  --  the implmentaion subprogram call <Param> =>
                  --  <Param_Value>.

                  CTU.Append_Node_To_List (Param_Value, Call_Profile);
               end if;

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

         end if;

         if not AINU.Is_Empty (Path (Spg_Call)) then
            --  If this is a feature subprogram call, generate a call
            --  to the corresponding method.  For this moment, we
            --  simply handle protected objects

            N := Message_Comment ("Invoking method");
            CTU.Append_Node_To_List (N, Statements);
            Owner := Get_Actual_Owner (Spg_Call);

1931
1932
1933
1934
            N :=
              Make_Variable_Address
                (CTN.Defining_Identifier
                   (CTN.Object_Node (Backend_Node (Identifier (Owner)))));
1935
1936
1937
1938
1939
1940
1941
1942

            Append_Node_To_List (N, Call_Profile);

            --  The name of the called subprogram is deduced from the
            --  corresponding subprogram spec instance (last element
            --  of the 'Path' list) and from the actual data component
            --  instance the call is connected to.

1943
1944
1945
1946
            N :=
              Map_C_Feature_Subprogram
                (Item (AIN.Last_Node (Path (Spg_Call))),
                 Corresponding_Instance (Get_Actual_Owner (Spg_Call)));
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965

            N := Make_Call_Profile (N, Call_Profile);
            CTU.Append_Node_To_List (N, Statements);

         else
            --  If this is a classic subprogram, call its
            --  implementation.

            if Get_Current_Backend_Kind = PolyORB_HI_C then
               Add_Include (PHCR.RH (PHCR.RH_Subprograms));
            elsif Get_Current_Backend_Kind = PolyORB_Kernel_C then
               Add_Include (PKR.RH (PKR.RH_Subprograms));
            end if;

            N := Message_Comment ("Call implementation");
            CTU.Append_Node_To_List (N, Statements);

            if Get_Current_Backend_Kind = PolyORB_HI_C then
               N := Map_C_Defining_Identifier (Spg);
jdelange's avatar
jdelange committed
1966
1967
               if Containing_Device /= No_Node then
                  CTU.Append_Node_To_List
1968
1969
                    (Make_Defining_Identifier
                       (Map_C_Enumerator_Name (Containing_Device)),
jdelange's avatar
jdelange committed
1970
1971
                     Call_Profile);
               end if;
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
            elsif Get_Current_Backend_Kind = PolyORB_Kernel_C then
               N := Map_C_Defining_Identifier (Spg);
            end if;
            N := Make_Call_Profile (N, Call_Profile);
            CTU.Append_Node_To_List (N, Statements);
         end if;

         Spg_Call := AIN.Next_Node (Spg_Call);
      end loop;
   end Handle_Call_Sequence;

   -------------------------
   -- Get_C_Default_Value --
   -------------------------

   function Get_C_Default_Value (D : Node_Id) return Node_Id is
      Data_Representation : Supported_Data_Representation;
      Result              : Node_Id;
   begin
      pragma Assert (AINU.Is_Data (D));

      Data_Representation := Get_Data_Representation (D);

      case Data_Representation is
         when Data_Integer =>
            --  For integers, default value is 0

1999
            Result := CTU.Make_Literal (CV.New_Int_Value (0, 1, 10));
2000
2001
2002
2003

         when Data_Float | Data_Fixed =>
            --  For reals, the default value is 0.0

2004
            Result := CTU.Make_Literal (CV.New_Floating_Point_Value (0.0));
2005
2006
2007
2008

         when Data_Boolean =>
            --  For booleans, the default value is FALSE

2009
            Result := CTU.Make_Literal (CV.New_Int_Value (0, 1, 10));
2010
2011
2012
2013

         when Data_Character =>
            --  For characters, the default value is the space ' '

2014
2015
            Result :=
              CTU.Make_Literal (CV.New_Char_Value (Character'Pos (' ')));
2016
2017
2018
2019
2020

         when Data_Wide_Character =>
            --  For wide characters, the default value is the wide
            --  space ' '.

2021
2022
            Result :=
              CTU.Make_Literal (CV.New_Char_Value (Character'Pos (' ')));
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044

         when Data_String =>
            Display_Located_Error
              (AIN.Loc (D),
               "Bounded strings default values not supported yet!",
               Fatal => True);

         when Data_Wide_String =>
            Display_Located_Error
              (AIN.Loc (D),
               "Bounded wide strings default values not supported yet!",
               Fatal => True);

         when Data_Array =>
            Display_Located_Error
              (AIN.Loc (D),
               "Bounded arrays default values not supported yet!",
               Fatal => True);

         when Data_With_Accessors =>
            --  This is definitely a code generation error

2045
2046
2047
            raise Program_Error
              with "Data types with accessors should" &
              " not have default values";
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061

         when others =>
            raise Program_Error with "Unsupported data type default value!";

      end case;

      return Result;
   end Get_C_Default_Value;

   -------------------------
   -- Make_Include_Clause --
   -------------------------

   function Make_Include_Clause
2062
2063
     (Header_Name : Node_Id;
      Local       : Boolean := False) return Node_Id
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
   is
      N : Node_Id;
   begin
      N := New_Node (K_Include_Clause);
      Set_Header_Name (N, Header_Name);
      Set_Is_Local (N, Local);

      return N;
   end Make_Include_Clause;

   ---------------------------
   -- Add_Define_Deployment --
   ---------------------------

2078
2079
2080
2081
2082
   procedure Add_Define_Deployment (E : Node_Id) is
      W            : Node_Id;
      N            : Name_Id;
      F            : Node_Id;
      Existing_Def : Node_Id;
2083
2084
2085
2086
2087
   begin
      Set_Str_To_Name_Buffer ("deployment");
      Get_Name_String_And_Append (CTN.Name (E));

      Get_Name_String_And_Append
2088
        (CTN.Name (CTN.Entity (Table (Last).Current_Entity)));
2089
2090
      N := Name_Find;

2091
      Existing_Def := Node_Id (Get_Name_Table_Info (N));
2092
2093

      --  If the file was already included, we return immediatly