ocarina-backends-ada_tree-nutils.adb 94.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 . A D A _ T R E E . N U T I L S      --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--    Copyright (C) 2006-2009 Telecom ParisTech, 2010-2020 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
33
34
--                                                                          --
------------------------------------------------------------------------------

with GNAT.Table;
with GNAT.Case_Util;

yoogx's avatar
yoogx committed
35
36
37
38
39
40
with Charset;        use Charset;
with Locations;      use Locations;
with Ocarina.Namet;  use Ocarina.Namet;
with Ocarina.Output; use Ocarina.Output;
with Ocarina.Types;  use Ocarina.Types;
with Utils;          use Utils;
41
42
43
44
45
46
47
48
49
50
51
52

with Ocarina.Backends.Ada_Values; use Ocarina.Backends.Ada_Values;
with Ocarina.Backends.Utils;      use Ocarina.Backends.Utils;
with Ocarina.Backends.Messages;   use Ocarina.Backends.Messages;

with Ocarina.ME_AADL.AADL_Tree.Nodes;

package body Ocarina.Backends.Ada_Tree.Nutils is

   package ADN renames Ocarina.Backends.Ada_Tree.Nodes;
   package AAN renames Ocarina.ME_AADL.AADL_Tree.Nodes;

53
54
   Var_Suffix  : constant String := "_Ü";
   Initialized : Boolean         := False;
55
56
57
58
59
60
61
62
63
64
65

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

   type Entity_Stack_Entry is record
      Current_Package : Node_Id;
      Current_Entity  : Node_Id;
   end record;

   No_Depth : constant Int := -1;
66
67
68
69
70
71
   package Entity_Stack is new GNAT.Table
     (Entity_Stack_Entry,
      Int,
      No_Depth + 1,
      10,
      10);
72
73
74
75

   use Entity_Stack;

   function Create_Unique_Identifier
76
77
     (Name   : Name_Id;
      Suffix : String := "") return Name_Id;
78
79
80
81
82
83
84
85
86
   --  This function returns a unique identifier for Name with a UT_ prefix,
   --  followed by the name of the node, name of the package, Name
   --  and Suffix if exists.

   function Get_Style_State return Value_Id;
   --  This function returns a string literal which is the value given
   --  to the pragma style_checks. The 'Off' value is does not ignore
   --  line length.

87
   procedure New_Operator (O : Operator_Type; I : String := "");
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

   ----------------------
   -- Add_With_Package --
   ----------------------

   procedure Add_With_Package
     (E            : Node_Id;
      Used         : Boolean := False;
      Warnings_Off : Boolean := False;
      Elaborated   : Boolean := False)
   is

      function To_Library_Unit (E : Node_Id) return Node_Id;
      --  Return the library unit which E belongs to in order to with
      --  it. As a special rule, package Standard returns No_Node.

      ---------------------
      -- To_Library_Unit --
      ---------------------

      function To_Library_Unit (E : Node_Id) return Node_Id is
         U : Node_Id;

      begin
         pragma Assert (Kind (E) = K_Designator);
         U := Corresponding_Node (Defining_Identifier (E));

         --  This node is not properly built as the corresponding node
         --  is not set.

         if No (U) then
            if Output_Tree_Warnings then
120
               Write_Str ("WARNING: node ");
121
122
123
124
125
126
127
128
129
130
               Write_Name (Name (Defining_Identifier (E)));
               Write_Line (" has a null corresponding node");
            end if;
            return E;
         end if;

         if ADN.Kind (U) = K_Package_Declaration then
            U := Package_Specification (U);
         end if;

131
132
133
         pragma Assert
           (Kind (U) = K_Package_Specification
            or else Kind (U) = K_Package_Instantiation);
134
135
136
137
138
139
140

         --  This is a subunit and we do not need to add a with for
         --  this unit but for one of its parents.  If the kind of the
         --  parent unit name is a K_Package_Instantiation, we
         --  consider it as a subunit.

         if Kind (U) = K_Package_Instantiation
141
142
           or else Is_Subunit_Package (U)
         then
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
            U := Parent_Unit_Name (E);

            --  This is a special case to handle package Standard

            if No (U) then
               return No_Node;
            end if;

            return To_Library_Unit (U);
         end if;

         return E;
      end To_Library_Unit;

      P             : constant Node_Id := To_Library_Unit (E);
      W             : Node_Id;
      N             : Name_Id;
      I             : Node_Id;
      Existing_With : Node_Id;

   begin
      if No (P) then
         return;
      end if;

      --  Build a string "<current_entity>%[s,b] <withed_entity>" that
      --  is the current entity name, a character 's' (resp 'b') to
      --  indicate whether we consider the spec (resp. body) of the
      --  current entity and the withed entity name.

      --  To avoid that a package "with"es itself

      if Kind (Current_Package) /= K_Subprogram_Implementation
        and then Kind (Current_Package) /= K_Subprogram_Specification
      then
         --  and then Corresponding_Node (Defining_Identifier (P))
         --  = Package_Declaration (Current_Package)

         if To_Lower (Fully_Qualified_Name (P)) =
           To_Lower
183
184
             (Fully_Qualified_Name
                (Defining_Identifier (Package_Declaration (Current_Package))))
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
         then
            return;
         end if;
      end if;

      --  Routine that check wether the package P has already been
      --  added to the withed packages of the current package. When we
      --  add a 'with' clause to a package specification, we check
      --  only if this clause has been added to the current
      --  spec. However, when we add a 'with' clause to a package
      --  body, we check that the clause has been added in both the
      --  spec and the body.

      --  IMPORTANT: Provided that all specs are generated before all
      --  bodies, this behaviour is automatically applied. We just
      --  need to encode the package name *without* precising whether
      --  it is a spec or a body

      --  Encoding the withed package and the current entity

      N := Fully_Qualified_Name (P);

      if Kind (Current_Package) /= K_Subprogram_Implementation
        and then Kind (Current_Package) /= K_Subprogram_Specification
      then
         I := Defining_Identifier (Package_Declaration (Current_Package));

         Get_Name_String (Fully_Qualified_Name (I));

         --  In both the PolyORB-HI and PolyORB-QoS generators some
         --  packages that are generated for different nodes have
         --  exactly the same name. We must encode the node name to
         --  differenciate them. This happens only when we deal with a
         --  package generated for a root node

         if Present
221
222
223
             (Main_Subprogram
                (Distributed_Application_Unit
                   (Package_Declaration (Current_Package))))
224
225
226
227
228
229
230
         then
            Add_Char_To_Name_Buffer (' ');
            Get_Name_String_And_Append
              (ADN.Name
                 (Defining_Identifier
                    (Main_Subprogram
                       (Distributed_Application_Unit
231
                          (Package_Declaration (Current_Package))))));
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
         end if;

      elsif Kind (Current_Package) /= K_Subprogram_Specification then
         I := Defining_Identifier (Specification (Current_Package));
         Get_Name_String (Fully_Qualified_Name (I));
      else
         I := Defining_Identifier (Current_Package);
         Get_Name_String (Fully_Qualified_Name (I));
      end if;

      Add_Char_To_Name_Buffer (' ');
      Get_Name_String_And_Append (N);
      N := To_Lower (Name_Find);

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

      Existing_With := Node_Id (Get_Name_Table_Info (N));

      if Present (Existing_With) then
         Set_Used (Existing_With, ADN.Used (Existing_With) or else Used);
         Set_Warnings_Off
           (Existing_With,
            ADN.Warnings_Off (Existing_With) or else Warnings_Off);
         Set_Elaborated
261
262
           (Existing_With,
            ADN.Elaborated (Existing_With) or else Elaborated);
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
         return;
      end if;

      --  Debug message (if wanted by the user)

      if Output_Unit_Withing then
         Write_Name (N);
         Write_Eol;
      end if;

      --  Add entity to the withed packages list of the current
      --  package

      W := Make_Withed_Package (P, Used, Warnings_Off, Elaborated);

      --  Mark the 'with' clause as being added to the current package

      Set_Name_Table_Info (N, Int (W));

      Append_Node_To_List (W, Withed_Packages (Current_Package));
   end Add_With_Package;

   ------------------------------------
   -- Append_Node_To_Current_Package --
   ------------------------------------

289
   procedure Append_Node_To_Current_Package (N : Node_Id) is
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
   begin
      case Kind (Current_Package) is
         when K_Package_Specification =>
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
         when K_Package_Implementation =>
            Append_Node_To_List (N, ADN.Statements (Current_Package));
         when others =>
            raise Program_Error;
      end case;
   end Append_Node_To_Current_Package;

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

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

   begin
      Last := Last_Node (L);
      if No (Last) then
         Set_First_Node (L, E);
      else
         Set_Next_Node (Last, E);
      end if;
      Last := E;
      while Present (Last) loop
         Set_Last_Node (L, Last);
         Last := 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 := Next_Node (N);
   begin
      Set_Next_Node (N, E);
      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 := First_Node (L);
      if Entity = N then
         Set_Next_Node (E, Entity);
         Set_First_Node (L, E);
      else
         while Present (Entity) loop
            exit when Next_Node (Entity) = N;
            Entity := Next_Node (Entity);
         end loop;

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

   ---------------------
   -- Copy_Designator --
   ---------------------

   function Copy_Designator
     (Designator : Node_Id;
360
      Withed     : Boolean := True) return Node_Id
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
   is
      D : Node_Id;
      P : Node_Id := Parent_Unit_Name (Designator);

   begin
      D := Copy_Node (Designator);
      if Kind (Designator) = K_Designator
        or else Kind (Designator) = K_Defining_Identifier
      then
         P := Parent_Unit_Name (Designator);
      elsif Kind (Designator) = K_Attribute_Designator then
         P := Parent_Unit_Name (Prefix (Designator));
      end if;

      if Present (P) then
         P := Copy_Designator (P, False);
         if Withed then
            Add_With_Package (P);
         end if;
      end if;
      return D;
   end Copy_Designator;

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

   function Copy_Node (N : Node_Id) return Node_Id is
      C : Node_Id;

   begin
      case Kind (N) is
         when K_Designator =>
            C := New_Node (K_Designator);
            Set_Defining_Identifier (C, Defining_Identifier (N));
            Set_Frontend_Node (C, Frontend_Node (N));
            Set_Homogeneous_Parent_Unit_Name (C, Parent_Unit_Name (N));

         when K_Defining_Identifier =>
            C := New_Node (K_Defining_Identifier);
            Set_Name (C, Name (N));
            Set_Homogeneous_Parent_Unit_Name (C, Parent_Unit_Name (N));
            Set_Corresponding_Node (C, Corresponding_Node (N));

         when K_Attribute_Designator =>
            C := New_Node (K_Attribute_Designator);
            Set_Name (C, Name (N));
            Set_Prefix (C, Copy_Node (Prefix (N)));

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

   ------------------------------------------
   -- Create_Subtype_From_Range_Constraint --
   ------------------------------------------

420
421
   function Create_Subtype_From_Range_Constraint
     (R : Node_Id) return Node_Id
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
   is
      N         : Node_Id := No_Node;
      C_First   : Node_Id := No_Node;
      C_Last    : Node_Id := No_Node;
      C_Index   : Node_Id := No_Node;
      Ident     : Node_Id := No_Node;
      Sub_Ident : Node_Id := No_Node;
   begin
      pragma Assert (Kind (R) = K_Range_Constraint);

      --  Stock identifier of the node in the variable Ident.
      --  If the node is not a literal, only its identifier is necessary.
      --  Variables C_first, C_Last and C_Index keep informations to
      --  construct the type replacing the range constraint.
      --  C_First and C_Last stock identifier of the node except for
      --  a literal node.

      if Present (Nodes.First (R)) then
         case Kind (Nodes.First (R)) is
            when K_Attribute_Designator =>
               C_First := Defining_Identifier (Nodes.Prefix (Nodes.First (R)));
443
               Ident   := C_First;
444
445
446

            when K_Designator =>
               C_First := Defining_Identifier (Nodes.First (R));
447
               Ident   := C_First;
448
449
450

            when K_Literal =>
               C_First := Nodes.First (R);
451
452
453
454
               Ident   :=
                 Make_Defining_Identifier
                   (Get_String_Name
                      (Ada_Values.Image (Nodes.Value (C_First))));
455
456
457

            when K_Defining_Identifier =>
               C_First := Nodes.First (R);
458
               Ident   := C_First;
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
            when others =>
               null;
         end case;
      end if;

      if Present (Nodes.Last (R)) then
         case Kind (Nodes.Last (R)) is
            when K_Attribute_Designator =>
               C_Last := Defining_Identifier (Nodes.Prefix (Nodes.Last (R)));

            when K_Designator =>
               C_Last := Defining_Identifier (Nodes.Last (R));

            when K_Literal =>
               C_Last :=
                 Make_Defining_Identifier
475
476
                   (Get_String_Name
                      (Ada_Values.Image (Nodes.Value (Nodes.Last (R)))));
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496

            when K_Defining_Identifier =>
               C_Last := Nodes.Last (R);

            when others =>
               null;
         end case;

         --  Construct identifier of the type : First_range_Last_Range
         Get_Name_String (Name (Ident));
         Add_Char_To_Name_Buffer ('_');
         Get_Name_String_And_Append (Name (C_Last));
         Sub_Ident := Make_Defining_Identifier (Name_Find);
      end if;

      if Present (Index_Type (R)) then
         case Kind (Index_Type (R)) is
            when K_Attribute_Designator =>
               Ident := Defining_Identifier (Nodes.Prefix (Index_Type (R)));

497
               if C_First = No_Node and then C_Last = No_Node then
498
499
500

                  --  Consider only Range attribute. Can be problematic
                  --  with a size attribute for instance.
501
502
503
504
505
506
507
508
                  C_Index :=
                    Make_Range_Constraint
                      (Make_Attribute_Designator (Ident, A_First),
                       Make_Attribute_Designator (Ident, A_Last),
                       Ident);

                  Sub_Ident :=
                    Make_Defining_Identifier (Name (Index_Type (R)));
509
510
               end if;
            when K_Designator =>
511
               Ident   := Defining_Identifier (Index_Type (R));
512
513
514
515
516
517
518
519
520
               C_Index := Index_Type (R);

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

      --  Case of unconstraint array (range <>)
      --  or a range attribute (Index'Range).
521
522
      if (C_First = No_Node)
        and then (C_Last = No_Node)
523
        and then (C_Index /= No_Node)
524
525
        and then Ident /= No_Node
      then
526
527
528
529
530
531

         --  if C_Index is an unconstraint array (range <>)
         --  return a range constraint, else return created type.
         if Kind (C_Index) = K_Designator then
            N := Make_Range_Constraint (No_Node, No_Node, Ident);
         else
532
533
534
535
536
            Sub_Ident :=
              Make_Defining_Identifier
                (Create_Unique_Identifier
                   (Name (Ident),
                    Get_Name_String (Name (Sub_Ident))));
537
            if Get_Name_Table_Info (Name (Sub_Ident)) = Int (No_Node) then
538
539
540
541
542
               N :=
                 Make_Full_Type_Declaration
                   (Defining_Identifier => Sub_Ident,
                    Type_Definition     => C_Index,
                    Is_Subtype          => True);
543
544
545
               Set_Name_Table_Info (Name (Sub_Ident), Int (Sub_Ident));
               Append_Node_To_Current_Package (N);
            else
546
547
548
               N :=
                 Corresponding_Node
                   (Node_Id (Get_Name_Table_Info (Name (Sub_Ident))));
549
550
551
552
553
554
            end if;
         end if;

      --  Case range constraint is of the form :
      --  My_Type range Range_First .. Range_Last
      --  create a type : subtype UT_Type is My_Type Range_First ..Range_Last
555
556
557
558
      elsif (C_First /= No_Node)
        and then (C_Last /= No_Node)
        and then (C_Index /= No_Node)
      then
559

560
561
562
         Sub_Ident :=
           Make_Defining_Identifier
             (Create_Unique_Identifier (Name (Sub_Ident)));
563

564
565
566
567
568
569
         N :=
           Make_Full_Type_Declaration
             (Defining_Identifier => Sub_Ident,
              Type_Definition     =>
                Make_Range_Constraint (C_First, C_Last, Ident),
              Is_Subtype => True);
570
571
572
573
574

         if Get_Name_Table_Info (Name (Sub_Ident)) = Int (No_Node) then
            Set_Name_Table_Info (Name (Sub_Ident), Int (Sub_Ident));
            Append_Node_To_Current_Package (N);
         else
575
576
577
            N :=
              Corresponding_Node
                (Node_Id (Get_Name_Table_Info (Name (Sub_Ident))));
578
579
580
581
         end if;

      --  Case range constraint is of the form : 1 .. Max_Size,
      --  create a type : type UT_Type is Integer range 1 .. Max_Size
582
583
584
585
      elsif (C_First /= No_Node)
        and then (C_Last /= No_Node)
        and then (C_Index = No_Node)
      then
586

587
588
589
         Sub_Ident :=
           Make_Defining_Identifier
             (Create_Unique_Identifier (Name (Sub_Ident)));
590

591
592
593
594
595
596
597
598
599
         N :=
           Make_Full_Type_Declaration
             (Defining_Identifier => Sub_Ident,
              Type_Definition     =>
                Make_Range_Constraint
                  (C_First,
                   C_Last,
                   Make_Defining_Identifier (TN (T_Integer))),
              Is_Subtype => True);
600
601
602
603
604

         if Get_Name_Table_Info (Name (Sub_Ident)) = Int (No_Node) then
            Set_Name_Table_Info (Name (Sub_Ident), Int (Sub_Ident));
            Append_Node_To_Current_Package (N);
         else
605
606
607
            N :=
              Corresponding_Node
                (Node_Id (Get_Name_Table_Info (Name (Sub_Ident))));
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
         end if;
      end if;

      return N;
   end Create_Subtype_From_Range_Constraint;

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

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

   ---------------------------------------
   -- Defining_Identifier_To_Designator --
   ---------------------------------------

   function Defining_Identifier_To_Designator
     (N                       : Node_Id;
      Copy                    : Boolean := False;
      Keep_Parent             : Boolean := True;
648
      Keep_Corresponding_Node : Boolean := True) return Node_Id
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
   is
      P      : Node_Id;
      Def_Id : Node_Id := N;
   begin
      pragma Assert (ADN.Kind (N) = K_Defining_Identifier);

      if Copy then
         Def_Id := Copy_Node (N);
      end if;

      if not Keep_Parent then
         Def_Id := Make_Defining_Identifier (ADN.Name (N));
      end if;

      if Keep_Corresponding_Node then
664
         Set_Corresponding_Node (Def_Id, Corresponding_Node (N));
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
785
786
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
858
859
860
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
887
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
      end if;

      P := New_Node (K_Designator);
      Set_Defining_Identifier (P, Def_Id);

      if Keep_Parent then
         Set_Homogeneous_Parent_Unit_Name (P, Parent_Unit_Name (N));
      end if;

      return P;
   end Defining_Identifier_To_Designator;

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

   function Message_Comment (M : Name_Id) return Node_Id is
      C : Node_Id;
   begin
      C := Make_Ada_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_Ada_Comment (Name_Find);
      return C;
   end Message_Comment;

   --------------------------
   -- Fully_Qualified_Name --
   --------------------------

   function Fully_Qualified_Name (N : Node_Id) return Name_Id is
      Parent_Node : Node_Id := No_Node;
      Parent_Name : Name_Id := No_Name;

   begin
      case Kind (N) is
         when K_Designator =>
            Parent_Node := Parent_Unit_Name (N);

            if not Present (Parent_Node) then
               Parent_Node := Parent_Unit_Name (Defining_Identifier (N));
            end if;

            if Present (Parent_Node) then
               Parent_Name := Fully_Qualified_Name (Parent_Node);
            end if;

            Name_Len := 0;
            if Present (Parent_Node) then
               Get_Name_String (Parent_Name);
               Add_Char_To_Name_Buffer ('.');
            end if;
            Get_Name_String_And_Append (Name (Defining_Identifier (N)));
            return Name_Find;

         when K_Defining_Identifier =>
            Parent_Node := Parent_Unit_Name (N);
            if Present (Parent_Node) then
               Parent_Name := Fully_Qualified_Name (Parent_Node);
            end if;

            Name_Len := 0;
            if Present (Parent_Node) then
               Get_Name_String (Parent_Name);
               Add_Char_To_Name_Buffer ('.');
            end if;
            Get_Name_String_And_Append (Name (N));
            return Name_Find;

         when K_Attribute_Designator =>
            Get_Name_String (Fully_Qualified_Name (Prefix (N)));
            Add_Char_To_Name_Buffer (''');
            Get_Name_String_And_Append (Name (N));
            return Name_Find;

         when others =>
            raise Program_Error;
      end case;
   end Fully_Qualified_Name;

   ---------------------
   -- Get_Style_State --
   ---------------------

   function Get_Style_State return Value_Id is

      --  The maximum line length allowed by GNAT is 32766

      Max_Line_Length : constant Int := 32766;
      Result          : Value_Id;
   begin
      Set_Str_To_Name_Buffer ("NM");
      Add_Nat_To_Name_Buffer (Max_Line_Length);
      Result := New_String_Value (Name_Find);
      return Result;
   end Get_Style_State;

   -----------
   -- 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_Double_Asterisk, "**");
      New_Token (Tok_Ampersand, "&");
      New_Token (Tok_Minus, "-");
      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_Comma, ",");
      New_Token (Tok_Less, "<");
      New_Token (Tok_Equal, "=");
      New_Token (Tok_Greater, ">");
      New_Token (Tok_Not_Equal, "/=");
      New_Token (Tok_Greater_Equal, ">=");
      New_Token (Tok_Less_Equal, "<=");
      New_Token (Tok_Box, "<>");
      New_Token (Tok_Colon_Equal, ":=");
      New_Token (Tok_Colon, ":");
      New_Token (Tok_Greater_Greater, ">>");
      New_Token (Tok_Less_Less, "<<");
      New_Token (Tok_Semicolon, ";");
      New_Token (Tok_Arrow, "=>");
      New_Token (Tok_Vertical_Bar, "|");
      New_Token (Tok_Dot_Dot, "..");
      New_Token (Tok_Minus_Minus, "--");

      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_Minus, "-");
      New_Operator (Op_Plus, "+");
      New_Operator (Op_Asterisk, "*");
      New_Operator (Op_Slash, "/");
      New_Operator (Op_Less, "<");
      New_Operator (Op_Equal, "=");
      New_Operator (Op_Greater, ">");
      New_Operator (Op_Not_Equal, "/=");
      New_Operator (Op_Greater_Equal, ">=");
      New_Operator (Op_Less_Equal, "<=");
      New_Operator (Op_Box, "<>");
      New_Operator (Op_Colon_Equal, ":=");
      New_Operator (Op_Colon, "--");
      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 Component_Id loop
         Set_Str_To_Name_Buffer (Component_Id'Image (C));
         Set_Str_To_Name_Buffer (Name_Buffer (3 .. Name_Len));
         GNAT.Case_Util.To_Mixed (Name_Buffer (1 .. Name_Len));
         CN (C) := 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 S in Subprogram_Id loop
         Set_Str_To_Name_Buffer (Subprogram_Id'Image (S));
         Set_Str_To_Name_Buffer (Name_Buffer (3 .. Name_Len));
         GNAT.Case_Util.To_Mixed (Name_Buffer (1 .. Name_Len));
         SN (S) := 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));
         GNAT.Case_Util.To_Mixed (Name_Buffer (1 .. Name_Len));
         TN (T) := 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));
         Add_Str_To_Name_Buffer (Var_Suffix);
         GNAT.Case_Util.To_Mixed (Name_Buffer (1 .. Name_Len));
         VN (V) := Name_Find;
      end loop;

      for G in Pragma_Id loop
         Set_Str_To_Name_Buffer (Pragma_Id'Image (G));
         Set_Str_To_Name_Buffer (Name_Buffer (8 .. Name_Len));
         GNAT.Case_Util.To_Mixed (Name_Buffer (1 .. Name_Len));
         GN (G) := Name_Find;
      end loop;

      for E in Error_Id loop
         Set_Str_To_Name_Buffer (Error_Id'Image (E));
         Set_Str_To_Name_Buffer (Name_Buffer (3 .. Name_Len));
         GNAT.Case_Util.To_Mixed (Name_Buffer (1 .. Name_Len));
         EN (E) := Name_Find;
      end loop;
926
927
928
929
930
931
932

      for A in Aspect_Id loop
         Set_Str_To_Name_Buffer (Aspect_Id'Image (A));
         Set_Str_To_Name_Buffer (Name_Buffer (3 .. Name_Len));
         GNAT.Case_Util.To_Mixed (Name_Buffer (1 .. Name_Len));
         ASN (A) := Name_Find;
      end loop;
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
   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 (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 := First_Node (L);

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

      return C;
   end Length;

   ---------------------------------
   -- Make_Access_Type_Definition --
   ---------------------------------

   function Make_Access_Type_Definition
     (Subtype_Indication : Node_Id;
      Is_All             : Boolean := False;
      Is_Constant        : Boolean := False;
983
      Is_Not_Null        : Boolean := False) return Node_Id
984
985
986
987
988
989
   is
      N : Node_Id;
   begin
      N := New_Node (K_Access_Type_Definition);
      Set_Subtype_Indication (N, Subtype_Indication);

990
      Set_Is_All (N, Is_All);
991
992
993
994
995
996
997
998
999
1000
      Set_Is_Constant (N, Is_Constant);
      Set_Is_Not_Null (N, Is_Not_Null);
      return N;
   end Make_Access_Type_Definition;

   ----------------------
   -- Make_Ada_Comment --
   ----------------------

   function Make_Ada_Comment
For faster browsing, not all history is shown. View entire blame