aboutsummaryrefslogtreecommitdiff
path: root/gcc/ada/prj-proc.adb
blob: 5dbb7e036dcec5edcc50c43488b9a7a8a7ee737b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
220
221
222
223
224
225
226
227
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
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
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
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
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
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
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT COMPILER COMPONENTS                         --
--                                                                          --
--                              P R J . P R O C                             --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                            $Revision$
--                                                                          --
--             Copyright (C) 2001 Free Software Foundation, Inc.            --
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 2,  or (at your option) any later ver- --
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT 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 GNAT;  see file COPYING.  If not, write --
-- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
-- MA 02111-1307, USA.                                                      --
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
-- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
--                                                                          --
------------------------------------------------------------------------------

with Errout;   use Errout;
with Namet;    use Namet;
with Opt;
with Output;   use Output;
with Prj.Attr; use Prj.Attr;
with Prj.Com;  use Prj.Com;
with Prj.Ext;  use Prj.Ext;
with Prj.Nmsc; use Prj.Nmsc;
with Stringt;  use Stringt;

with GNAT.Case_Util;
with GNAT.HTable;

package body Prj.Proc is

   Error_Report : Put_Line_Access := null;

   package Processed_Projects is new GNAT.HTable.Simple_HTable
     (Header_Num => Header_Num,
      Element    => Project_Id,
      No_Element => No_Project,
      Key        => Name_Id,
      Hash       => Hash,
      Equal      => "=");
   --  This hash table contains all processed projects

   procedure Add (To_Exp : in out String_Id; Str : String_Id);
   --  Concatenate two strings and returns another string if both
   --  arguments are not null string.

   procedure Add_Attributes
     (Decl     : in out Declarations;
      First    : Attribute_Node_Id);
   --  Add all attributes, starting with First, with their default
   --  values to the package or project with declarations Decl.

   function Expression
     (Project           : Project_Id;
      From_Project_Node : Project_Node_Id;
      Pkg               : Package_Id;
      First_Term        : Project_Node_Id;
      Kind              : Variable_Kind)
      return              Variable_Value;
   --  From N_Expression project node From_Project_Node, compute the value
   --  of an expression and return it as a Variable_Value.

   function Imported_Or_Modified_Project_From
     (Project   : Project_Id;
      With_Name : Name_Id)
      return      Project_Id;
   --  Find an imported or modified project of Project whose name is With_Name.

   function Package_From
     (Project   : Project_Id;
      With_Name : Name_Id)
      return      Package_Id;
   --  Find the package of Project whose name is With_Name.

   procedure Process_Declarative_Items
     (Project           : Project_Id;
      From_Project_Node : Project_Node_Id;
      Pkg               : Package_Id;
      Item              : Project_Node_Id);
   --  Process declarative items starting with From_Project_Node, and put them
   --  in declarations Decl. This is a recursive procedure; it calls itself for
   --  a package declaration or a case construction.

   procedure Recursive_Process
     (Project           : out Project_Id;
      From_Project_Node : Project_Node_Id;
      Modified_By       : Project_Id);
   --  Process project with node From_Project_Node in the tree.
   --  Do nothing if From_Project_Node is Empty_Node.
   --  If project has already been processed, simply return its project id.
   --  Otherwise create a new project id, mark it as processed, call itself
   --  recursively for all imported projects and a modified project, if any.
   --  Then process the declarative items of the project.

   procedure Check (Project : in out Project_Id);
   --  Set all projects to not checked, then call Recursive_Check for
   --  the main project Project.
   --  Project is set to No_Project if errors occurred.

   procedure Recursive_Check (Project : Project_Id);
   --  If Project is marked as not checked, mark it as checked,
   --  call Check_Naming_Scheme for the project, then call itself
   --  for a possible modified project and all the imported projects
   --  of Project.

   ---------
   -- Add --
   ---------

   procedure Add (To_Exp : in out String_Id; Str : String_Id) is
   begin
      if To_Exp = Types.No_String or else String_Length (To_Exp) = 0 then

         --  To_Exp is nil or empty. The result is Str.

         To_Exp := Str;

      --  If Str is nil, then do not change To_Ext

      elsif Str /= No_String then
         Start_String (To_Exp);
         Store_String_Chars (Str);
         To_Exp := End_String;
      end if;
   end Add;

   --------------------
   -- Add_Attributes --
   --------------------

   procedure Add_Attributes
     (Decl           : in out Declarations;
      First          : Attribute_Node_Id) is
      The_Attribute  : Attribute_Node_Id := First;
      Attribute_Data : Attribute_Record;

   begin
      while The_Attribute /= Empty_Attribute loop
         Attribute_Data := Attributes.Table (The_Attribute);

         if Attribute_Data.Kind_2 /= Associative_Array then
            declare
               New_Attribute : Variable_Value;

            begin
               case Attribute_Data.Kind_1 is

                  --  Undefined should not happen

                  when Undefined =>
                     pragma Assert
                       (False, "attribute with an undefined kind");
                     raise Program_Error;

                  --  Single attributes have a default value of empty string

                  when Single =>
                     New_Attribute :=
                       (Kind     => Single,
                        Location => No_Location,
                        Default  => True,
                        Value    => Empty_String);

                  --  List attributes have a default value of nil list

                  when List =>
                     New_Attribute :=
                       (Kind     => List,
                        Location => No_Location,
                        Default  => True,
                        Values   => Nil_String);

               end case;

               Variable_Elements.Increment_Last;
               Variable_Elements.Table (Variable_Elements.Last) :=
                 (Next  => Decl.Attributes,
                  Name  => Attribute_Data.Name,
                  Value => New_Attribute);
               Decl.Attributes := Variable_Elements.Last;
            end;
         end if;

         The_Attribute := Attributes.Table (The_Attribute).Next;
      end loop;

   end Add_Attributes;

   -----------
   -- Check --
   -----------

   procedure Check (Project : in out Project_Id) is
   begin
      --  Make sure that all projects are marked as not checked.

      for Index in 1 .. Projects.Last loop
         Projects.Table (Index).Checked := False;
      end loop;

      Recursive_Check (Project);

      if Errout.Errors_Detected > 0 then
         Project := No_Project;
      end if;

   end Check;

   ----------------
   -- Expression --
   ----------------

   function Expression
     (Project           : Project_Id;
      From_Project_Node : Project_Node_Id;
      Pkg               : Package_Id;
      First_Term        : Project_Node_Id;
      Kind              : Variable_Kind)
      return              Variable_Value
   is
      The_Term : Project_Node_Id := First_Term;
      --  The term in the expression list

      The_Current_Term : Project_Node_Id := Empty_Node;
      --  The current term node id

      Term_Kind : Variable_Kind;
      --  The kind of the current term

      Result : Variable_Value (Kind => Kind);
      --  The returned result

      Last : String_List_Id := Nil_String;
      --  Reference to the last string elements in Result, when Kind is List.

   begin
      Result.Location := Location_Of (From_Project_Node);

      --  Process each term of the expression, starting with First_Term

      while The_Term /= Empty_Node loop

         --  We get the term data and kind ...

         Term_Kind := Expression_Kind_Of (The_Term);

         The_Current_Term := Current_Term (The_Term);

         case Kind_Of (The_Current_Term) is

            when N_Literal_String =>

               case Kind is

                  when Undefined =>

                     --  Should never happen

                     pragma Assert (False, "Undefined expression kind");
                     raise Program_Error;

                  when Single =>
                     Add (Result.Value, String_Value_Of (The_Current_Term));

                  when List =>

                     String_Elements.Increment_Last;

                     if Last = Nil_String then

                        --  This can happen in an expression such as
                        --  () & "toto"

                        Result.Values := String_Elements.Last;

                     else
                        String_Elements.Table (Last).Next :=
                          String_Elements.Last;
                     end if;

                     Last := String_Elements.Last;
                     String_Elements.Table (Last) :=
                       (Value    => String_Value_Of (The_Current_Term),
                        Location => Location_Of (The_Current_Term),
                        Next     => Nil_String);

               end case;

            when N_Literal_String_List =>

               declare
                  String_Node : Project_Node_Id :=
                                  First_Expression_In_List (The_Current_Term);

                  Value : Variable_Value;

               begin
                  if String_Node /= Empty_Node then

                     --  If String_Node is nil, it is an empty list,
                     --  there is nothing to do

                     Value := Expression
                       (Project           => Project,
                        From_Project_Node => From_Project_Node,
                        Pkg               => Pkg,
                        First_Term        => Tree.First_Term (String_Node),
                        Kind              => Single);
                     String_Elements.Increment_Last;

                     if Result.Values = Nil_String then

                        --  This literal string list is the first term
                        --  in a string list expression

                        Result.Values := String_Elements.Last;

                     else
                        String_Elements.Table (Last).Next :=
                          String_Elements.Last;
                     end if;

                     Last := String_Elements.Last;
                     String_Elements.Table (Last) :=
                       (Value    => Value.Value,
                        Location => Value.Location,
                        Next     => Nil_String);

                     loop
                        --  Add the other element of the literal string list
                        --  one after the other

                        String_Node :=
                          Next_Expression_In_List (String_Node);

                        exit when String_Node = Empty_Node;

                        Value :=
                          Expression
                          (Project           => Project,
                           From_Project_Node => From_Project_Node,
                           Pkg               => Pkg,
                           First_Term        => Tree.First_Term (String_Node),
                           Kind              => Single);

                        String_Elements.Increment_Last;
                        String_Elements.Table (Last).Next :=
                          String_Elements.Last;
                        Last := String_Elements.Last;
                        String_Elements.Table (Last) :=
                          (Value    => Value.Value,
                           Location => Value.Location,
                           Next     => Nil_String);
                     end loop;

                  end if;

               end;

            when N_Variable_Reference | N_Attribute_Reference =>

               declare
                  The_Project     : Project_Id  := Project;
                  The_Package     : Package_Id  := Pkg;
                  The_Name        : Name_Id     := No_Name;
                  The_Variable_Id : Variable_Id := No_Variable;
                  The_Variable    : Variable;
                  Term_Project    : constant Project_Node_Id :=
                                      Project_Node_Of (The_Current_Term);
                  Term_Package    : constant Project_Node_Id :=
                                      Package_Node_Of (The_Current_Term);

               begin
                  if Term_Project /= Empty_Node and then
                     Term_Project /= From_Project_Node
                  then
                     --  This variable or attribute comes from another project

                     The_Name := Name_Of (Term_Project);
                     The_Project := Imported_Or_Modified_Project_From
                       (Project => Project, With_Name => The_Name);
                  end if;

                  if Term_Package /= Empty_Node then

                     --  This is an attribute of a package

                     The_Name := Name_Of (Term_Package);
                     The_Package := Projects.Table (The_Project).Decl.Packages;

                     while The_Package /= No_Package
                       and then Packages.Table (The_Package).Name /= The_Name
                     loop
                        The_Package := Packages.Table (The_Package).Next;
                     end loop;

                     pragma Assert
                       (The_Package /= No_Package,
                        "package not found.");

                  elsif Kind_Of (The_Current_Term) = N_Attribute_Reference then
                     The_Package := No_Package;
                  end if;

                  The_Name := Name_Of (The_Current_Term);

                  if The_Package /= No_Package then

                     --  First, if there is a package, look into the package

                     if Kind_Of (The_Current_Term) = N_Variable_Reference then
                        The_Variable_Id :=
                          Packages.Table (The_Package).Decl.Variables;

                     else
                        The_Variable_Id :=
                          Packages.Table (The_Package).Decl.Attributes;
                     end if;

                     while The_Variable_Id /= No_Variable
                       and then
                         Variable_Elements.Table (The_Variable_Id).Name /=
                                                                    The_Name
                     loop
                        The_Variable_Id :=
                          Variable_Elements.Table (The_Variable_Id).Next;
                     end loop;

                  end if;

                  if The_Variable_Id = No_Variable then

                     --  If we have not found it, look into the project

                     if Kind_Of (The_Current_Term) = N_Variable_Reference then
                        The_Variable_Id :=
                          Projects.Table (The_Project).Decl.Variables;

                     else
                        The_Variable_Id :=
                          Projects.Table (The_Project).Decl.Attributes;
                     end if;

                     while The_Variable_Id /= No_Variable
                       and then
                         Variable_Elements.Table (The_Variable_Id).Name /=
                                                                     The_Name
                     loop
                        The_Variable_Id :=
                          Variable_Elements.Table (The_Variable_Id).Next;
                     end loop;

                  end if;

                  pragma Assert (The_Variable_Id /= No_Variable,
                                 "variable or attribute not found");

                  The_Variable := Variable_Elements.Table (The_Variable_Id);

                  case Kind is

                     when Undefined =>

                        --  Should never happen

                        pragma Assert (False, "undefined expression kind");
                        null;

                     when Single =>

                        case The_Variable.Value.Kind is

                           when Undefined =>
                              null;

                           when Single =>
                              Add (Result.Value, The_Variable.Value.Value);

                           when List =>

                              --  Should never happen

                              pragma Assert
                                (False,
                                 "list cannot appear in single " &
                                 "string expression");
                              null;

                        end case;

                     when List =>
                        case The_Variable.Value.Kind is

                           when Undefined =>
                              null;

                           when Single =>
                              String_Elements.Increment_Last;

                              if Last = Nil_String then

                                 --  This can happen in an expression such as
                                 --  () & Var

                                 Result.Values := String_Elements.Last;

                              else
                                 String_Elements.Table (Last).Next :=
                                   String_Elements.Last;
                              end if;

                              Last := String_Elements.Last;
                              String_Elements.Table (Last) :=
                                (Value    => The_Variable.Value.Value,
                                 Location => Location_Of (The_Current_Term),
                                 Next     => Nil_String);

                           when List =>

                              declare
                                 The_List : String_List_Id :=
                                              The_Variable.Value.Values;

                              begin
                                 while The_List /= Nil_String loop
                                    String_Elements.Increment_Last;

                                    if Last = Nil_String then
                                       Result.Values := String_Elements.Last;

                                    else
                                       String_Elements.Table (Last).Next :=
                                         String_Elements.Last;

                                    end if;

                                    Last := String_Elements.Last;
                                    String_Elements.Table (Last) :=
                                      (Value    =>
                                         String_Elements.Table
                                                          (The_List).Value,
                                       Location => Location_Of
                                                          (The_Current_Term),
                                       Next     => Nil_String);
                                    The_List :=
                                      String_Elements.Table (The_List).Next;

                                 end loop;
                              end;
                        end case;
                  end case;
               end;

            when N_External_Value =>
               String_To_Name_Buffer
                 (String_Value_Of (External_Reference_Of (The_Current_Term)));

               declare
                  Name    : constant Name_Id  := Name_Find;
                  Default : String_Id         := No_String;
                  Value   : String_Id         := No_String;

                  Default_Node : constant Project_Node_Id :=
                                   External_Default_Of (The_Current_Term);

               begin
                  if Default_Node /= Empty_Node then
                     Default := String_Value_Of (Default_Node);
                  end if;

                  Value := Prj.Ext.Value_Of (Name, Default);

                  if Value = No_String then
                     if Error_Report = null then
                        Error_Msg
                          ("undefined external reference",
                           Location_Of (The_Current_Term));

                     else
                        Error_Report
                          ("""" & Get_Name_String (Name) &
                           """ is an undefined external reference");
                     end if;

                     Value := Empty_String;

                  end if;

                  case Kind is

                     when Undefined =>
                        null;

                     when Single =>
                        Add (Result.Value, Value);

                     when List =>
                        String_Elements.Increment_Last;

                        if Last = Nil_String then
                           Result.Values := String_Elements.Last;

                        else
                           String_Elements.Table (Last).Next :=
                             String_Elements.Last;
                        end if;

                        Last := String_Elements.Last;
                        String_Elements.Table (Last) :=
                          (Value    => Value,
                           Location => Location_Of (The_Current_Term),
                           Next     => Nil_String);

                  end case;

               end;

            when others =>

               --  Should never happen

               pragma Assert
                 (False,
                  "illegal node kind in an expression");
               raise Program_Error;

         end case;

         The_Term := Next_Term (The_Term);

      end loop;

      return Result;
   end Expression;

   ---------------------------------------
   -- Imported_Or_Modified_Project_From --
   ---------------------------------------

   function Imported_Or_Modified_Project_From
     (Project   : Project_Id;
      With_Name : Name_Id)
      return      Project_Id
   is
      Data : constant Project_Data := Projects.Table (Project);
      List : Project_List          := Data.Imported_Projects;

   begin
      --  First check if it is the name of a modified project

      if Data.Modifies /= No_Project
        and then Projects.Table (Data.Modifies).Name = With_Name
      then
         return Data.Modifies;

      else
         --  Then check the name of each imported project

         while List /= Empty_Project_List
           and then
             Projects.Table
               (Project_Lists.Table (List).Project).Name /= With_Name

         loop
            List := Project_Lists.Table (List).Next;
         end loop;

         pragma Assert
           (List /= Empty_Project_List,
           "project not found");

         return Project_Lists.Table (List).Project;
      end if;
   end Imported_Or_Modified_Project_From;

   ------------------
   -- Package_From --
   ------------------

   function Package_From
     (Project   : Project_Id;
      With_Name : Name_Id)
      return      Package_Id
   is
      Data   : constant Project_Data := Projects.Table (Project);
      Result : Package_Id := Data.Decl.Packages;

   begin
      --  Check the name of each existing package of Project

      while Result /= No_Package
        and then
        Packages.Table (Result).Name /= With_Name
      loop
         Result := Packages.Table (Result).Next;
      end loop;

      if Result = No_Package then
         --  Should never happen
         Write_Line ("package """ & Get_Name_String (With_Name) &
                     """ not found");
         raise Program_Error;

      else
         return Result;
      end if;
   end Package_From;

   -------------
   -- Process --
   -------------

   procedure Process
     (Project           : out Project_Id;
      From_Project_Node : Project_Node_Id;
      Report_Error      : Put_Line_Access)
   is
   begin
      Error_Report := Report_Error;

      --  Make sure there is no projects in the data structure

      Projects.Set_Last (No_Project);
      Processed_Projects.Reset;

      --  And process the main project and all of the projects it depends on,
      --  recursively

      Recursive_Process
        (Project           => Project,
         From_Project_Node => From_Project_Node,
         Modified_By       => No_Project);

      if Errout.Errors_Detected > 0 then
         Project := No_Project;
      end if;

      if Project /= No_Project then
         Check (Project);
      end if;

   end Process;

   -------------------------------
   -- Process_Declarative_Items --
   -------------------------------

   procedure Process_Declarative_Items
     (Project           : Project_Id;
      From_Project_Node : Project_Node_Id;
      Pkg               : Package_Id;
      Item              : Project_Node_Id) is

      Current_Declarative_Item : Project_Node_Id := Item;

      Current_Item : Project_Node_Id := Empty_Node;

   begin
      --  For each declarative item

      while Current_Declarative_Item /= Empty_Node loop

         --  Get its data

         Current_Item := Current_Item_Node (Current_Declarative_Item);

         --  And set Current_Declarative_Item to the next declarative item
         --  ready for the next iteration

         Current_Declarative_Item := Next_Declarative_Item
                                            (Current_Declarative_Item);

         case Kind_Of (Current_Item) is

            when N_Package_Declaration =>
               Packages.Increment_Last;

               declare
                  New_Pkg         : constant Package_Id := Packages.Last;
                  The_New_Package : Package_Element;

                  Project_Of_Renamed_Package : constant Project_Node_Id :=
                                                 Project_Of_Renamed_Package_Of
                                                   (Current_Item);

               begin
                  The_New_Package.Name := Name_Of (Current_Item);

                  if Pkg /= No_Package then
                     The_New_Package.Next :=
                       Packages.Table (Pkg).Decl.Packages;
                     Packages.Table (Pkg).Decl.Packages := New_Pkg;
                  else
                     The_New_Package.Next :=
                       Projects.Table (Project).Decl.Packages;
                     Projects.Table (Project).Decl.Packages := New_Pkg;
                  end if;

                  Packages.Table (New_Pkg) := The_New_Package;

                  if Project_Of_Renamed_Package /= Empty_Node then

                     --  Renamed package

                     declare
                        Project_Name : constant Name_Id :=
                                         Name_Of
                                           (Project_Of_Renamed_Package);

                        Renamed_Project : constant Project_Id :=
                                            Imported_Or_Modified_Project_From
                                              (Project, Project_Name);

                        Renamed_Package : constant Package_Id :=
                                            Package_From
                                              (Renamed_Project,
                                               Name_Of (Current_Item));

                     begin
                        Packages.Table (New_Pkg).Decl :=
                          Packages.Table (Renamed_Package).Decl;
                     end;

                  else
                     --  Set the default values of the attributes

                     Add_Attributes
                       (Packages.Table (New_Pkg).Decl,
                        Package_Attributes.Table
                           (Package_Id_Of (Current_Item)).First_Attribute);

                     Process_Declarative_Items
                       (Project           => Project,
                        From_Project_Node => From_Project_Node,
                        Pkg               => New_Pkg,
                        Item              => First_Declarative_Item_Of
                                                             (Current_Item));
                  end if;

               end;

            when N_String_Type_Declaration =>

               --  There is nothing to process

               null;

            when N_Attribute_Declaration      |
                 N_Typed_Variable_Declaration |
                 N_Variable_Declaration       =>

                  pragma Assert (Expression_Of (Current_Item) /= Empty_Node,
                                 "no expression for an object declaration");

               declare
                  New_Value : constant Variable_Value :=
                                Expression
                                  (Project           => Project,
                                   From_Project_Node => From_Project_Node,
                                   Pkg               => Pkg,
                                   First_Term        =>
                                     Tree.First_Term (Expression_Of
                                                              (Current_Item)),
                                   Kind              =>
                                     Expression_Kind_Of (Current_Item));

                  The_Variable : Variable_Id := No_Variable;

                  Current_Item_Name : constant Name_Id :=
                                        Name_Of (Current_Item);

               begin
                  if Kind_Of (Current_Item) = N_Typed_Variable_Declaration then

                     if String_Equal (New_Value.Value, Empty_String) then
                        Error_Msg_Name_1 := Name_Of (Current_Item);

                        if Error_Report = null then
                           Error_Msg
                             ("no value defined for %",
                              Location_Of (Current_Item));

                        else
                           Error_Report
                             ("no value defined for " &
                              Get_Name_String (Error_Msg_Name_1));
                        end if;

                     else
                        declare
                           Current_String : Project_Node_Id :=
                                              First_Literal_String
                                                (String_Type_Of
                                                  (Current_Item));

                        begin
                           while Current_String /= Empty_Node
                             and then not String_Equal
                                            (String_Value_Of (Current_String),
                                             New_Value.Value)
                           loop
                              Current_String :=
                                Next_Literal_String (Current_String);
                           end loop;

                           if Current_String = Empty_Node then
                              String_To_Name_Buffer (New_Value.Value);
                              Error_Msg_Name_1 := Name_Find;
                              Error_Msg_Name_2 := Name_Of (Current_Item);

                              if Error_Report = null then
                                 Error_Msg
                                   ("value { is illegal for typed string %",
                                    Location_Of (Current_Item));

                              else
                                 Error_Report
                                   ("value """ &
                                    Get_Name_String (Error_Msg_Name_1) &
                                    """ is illegal for typed string """ &
                                    Get_Name_String (Error_Msg_Name_2) &
                                    """");
                              end if;
                           end if;
                        end;
                     end if;
                  end if;

                  if Kind_Of (Current_Item) /= N_Attribute_Declaration
                    or else
                      Associative_Array_Index_Of (Current_Item) = No_String
                  then
                     --  Usual case

                     --  Code below really needs more comments ???

                     if Kind_Of (Current_Item) = N_Attribute_Declaration then
                        if Pkg /= No_Package then
                           The_Variable :=
                             Packages.Table (Pkg).Decl.Attributes;

                        else
                           The_Variable :=
                             Projects.Table (Project).Decl.Attributes;
                        end if;

                     else
                        if Pkg /= No_Package then
                           The_Variable :=
                             Packages.Table (Pkg).Decl.Variables;

                        else
                           The_Variable :=
                             Projects.Table (Project).Decl.Variables;
                        end if;

                     end if;

                     while
                       The_Variable /= No_Variable
                         and then
                           Variable_Elements.Table (The_Variable).Name /=
                                                          Current_Item_Name
                     loop
                        The_Variable :=
                          Variable_Elements.Table (The_Variable).Next;
                     end loop;

                     if The_Variable = No_Variable then
                        pragma Assert
                          (Kind_Of (Current_Item) /= N_Attribute_Declaration,
                           "illegal attribute declaration");

                        Variable_Elements.Increment_Last;
                        The_Variable := Variable_Elements.Last;

                        if Pkg /= No_Package then
                           Variable_Elements.Table (The_Variable) :=
                             (Next    =>
                                Packages.Table (Pkg).Decl.Variables,
                              Name    => Current_Item_Name,
                              Value   => New_Value);
                           Packages.Table (Pkg).Decl.Variables := The_Variable;

                        else
                           Variable_Elements.Table (The_Variable) :=
                             (Next    =>
                                Projects.Table (Project).Decl.Variables,
                              Name    => Current_Item_Name,
                              Value   => New_Value);
                           Projects.Table (Project).Decl.Variables :=
                             The_Variable;
                        end if;

                     else
                        Variable_Elements.Table (The_Variable).Value :=
                          New_Value;

                     end if;

                  else
                     --  Associative array attribute

                     String_To_Name_Buffer
                       (Associative_Array_Index_Of (Current_Item));

                     if Case_Insensitive (Current_Item) then
                        GNAT.Case_Util.To_Lower (Name_Buffer (1 .. Name_Len));
                     end if;

                     declare
                        The_Array : Array_Id;

                        The_Array_Element : Array_Element_Id :=
                                              No_Array_Element;

                        Index_Name : constant Name_Id := Name_Find;

                     begin

                        if Pkg /= No_Package then
                           The_Array := Packages.Table (Pkg).Decl.Arrays;

                        else
                           The_Array := Projects.Table (Project).Decl.Arrays;
                        end if;

                        while
                          The_Array /= No_Array
                            and then Arrays.Table (The_Array).Name /=
                                                           Current_Item_Name
                        loop
                           The_Array := Arrays.Table (The_Array).Next;
                        end loop;

                        if The_Array = No_Array then
                           Arrays.Increment_Last;
                           The_Array := Arrays.Last;

                           if Pkg /= No_Package then
                              Arrays.Table (The_Array) :=
                                (Name  => Current_Item_Name,
                                 Value => No_Array_Element,
                                 Next  => Packages.Table (Pkg).Decl.Arrays);
                              Packages.Table (Pkg).Decl.Arrays := The_Array;

                           else
                              Arrays.Table (The_Array) :=
                                (Name  => Current_Item_Name,
                                 Value => No_Array_Element,
                                 Next  =>
                                   Projects.Table (Project).Decl.Arrays);
                              Projects.Table (Project).Decl.Arrays :=
                                The_Array;
                           end if;

                        else
                           The_Array_Element := Arrays.Table (The_Array).Value;
                        end if;

                        while The_Array_Element /= No_Array_Element
                          and then
                            Array_Elements.Table (The_Array_Element).Index /=
                                                                  Index_Name
                        loop
                           The_Array_Element :=
                             Array_Elements.Table (The_Array_Element).Next;
                        end loop;

                        if The_Array_Element = No_Array_Element then
                           Array_Elements.Increment_Last;
                           The_Array_Element := Array_Elements.Last;
                           Array_Elements.Table (The_Array_Element) :=
                             (Index  => Index_Name,
                              Value  => New_Value,
                              Next   => Arrays.Table (The_Array).Value);
                           Arrays.Table (The_Array).Value := The_Array_Element;

                        else
                           Array_Elements.Table (The_Array_Element).Value :=
                             New_Value;
                        end if;
                     end;
                  end if;
               end;

            when N_Case_Construction =>
               declare
                  The_Project   : Project_Id      := Project;
                  The_Package   : Package_Id      := Pkg;
                  The_Variable  : Variable_Value  := Nil_Variable_Value;
                  Case_Value    : String_Id       := No_String;
                  Case_Item     : Project_Node_Id := Empty_Node;
                  Choice_String : Project_Node_Id := Empty_Node;
                  Decl_Item     : Project_Node_Id := Empty_Node;

               begin
                  declare
                     Variable_Node : constant Project_Node_Id :=
                                       Case_Variable_Reference_Of
                                         (Current_Item);

                     Var_Id : Variable_Id := No_Variable;
                     Name   : Name_Id     := No_Name;

                  begin
                     if Project_Node_Of (Variable_Node) /= Empty_Node then
                        Name := Name_Of (Project_Node_Of (Variable_Node));
                        The_Project :=
                          Imported_Or_Modified_Project_From (Project, Name);
                     end if;

                     if Package_Node_Of (Variable_Node) /= Empty_Node then
                        Name := Name_Of (Package_Node_Of (Variable_Node));
                        The_Package := Package_From (The_Project, Name);
                     end if;

                     Name := Name_Of (Variable_Node);

                     if The_Package /= No_Package then
                        Var_Id := Packages.Table (The_Package).Decl.Variables;
                        Name := Name_Of (Variable_Node);
                        while Var_Id /= No_Variable
                          and then
                            Variable_Elements.Table (Var_Id).Name /= Name
                        loop
                           Var_Id := Variable_Elements.Table (Var_Id).Next;
                        end loop;
                     end if;

                     if Var_Id = No_Variable
                       and then Package_Node_Of (Variable_Node) = Empty_Node
                     then
                        Var_Id := Projects.Table (The_Project).Decl.Variables;
                        while Var_Id /= No_Variable
                          and then
                            Variable_Elements.Table (Var_Id).Name /= Name
                        loop
                           Var_Id := Variable_Elements.Table (Var_Id).Next;
                        end loop;
                     end if;

                     if Var_Id = No_Variable then

                        --  Should never happen

                        Write_Line ("variable """ &
                                    Get_Name_String (Name) &
                                    """ not found");
                        raise Program_Error;
                     end if;

                     The_Variable := Variable_Elements.Table (Var_Id).Value;

                     if The_Variable.Kind /= Single then

                        --  Should never happen

                        Write_Line ("variable""" &
                                    Get_Name_String (Name) &
                                    """ is not a single string variable");
                        raise Program_Error;
                     end if;

                     Case_Value := The_Variable.Value;
                  end;

                  Case_Item := First_Case_Item_Of (Current_Item);
                  Case_Item_Loop :
                     while Case_Item /= Empty_Node loop
                        Choice_String := First_Choice_Of (Case_Item);

                        if Choice_String = Empty_Node then
                           Decl_Item := First_Declarative_Item_Of (Case_Item);
                           exit Case_Item_Loop;
                        end if;

                        Choice_Loop :
                           while Choice_String /= Empty_Node loop
                              if String_Equal (Case_Value,
                                               String_Value_Of (Choice_String))
                              then
                                 Decl_Item :=
                                   First_Declarative_Item_Of (Case_Item);
                                 exit Case_Item_Loop;
                              end if;

                              Choice_String :=
                                Next_Literal_String (Choice_String);
                           end loop Choice_Loop;
                        Case_Item := Next_Case_Item (Case_Item);
                     end loop Case_Item_Loop;

                  if Decl_Item /= Empty_Node then
                     Process_Declarative_Items
                       (Project           => Project,
                        From_Project_Node => From_Project_Node,
                        Pkg               => Pkg,
                        Item              => Decl_Item);
                  end if;
               end;

            when others =>

               --  Should never happen

               Write_Line ("Illegal declarative item: " &
                           Project_Node_Kind'Image (Kind_Of (Current_Item)));
               raise Program_Error;
         end case;
      end loop;
   end Process_Declarative_Items;

   ---------------------
   -- Recursive_Check --
   ---------------------

   procedure Recursive_Check (Project : Project_Id) is
      Data                  : Project_Data;
      Imported_Project_List : Project_List := Empty_Project_List;

   begin
      --  Do nothing if Project is No_Project, or Project has already
      --  been marked as checked.

      if Project /= No_Project
        and then not Projects.Table (Project).Checked
      then
         Data := Projects.Table (Project);

         --  Call itself for a possible modified project.
         --  (if there is no modified project, then nothing happens).

         Recursive_Check (Data.Modifies);

         --  Call itself for all imported projects

         Imported_Project_List := Data.Imported_Projects;
         while Imported_Project_List /= Empty_Project_List loop
            Recursive_Check
              (Project_Lists.Table (Imported_Project_List).Project);
            Imported_Project_List :=
              Project_Lists.Table (Imported_Project_List).Next;
         end loop;

         --  Mark project as checked

         Projects.Table (Project).Checked := True;

         if Opt.Verbose_Mode then
            Write_Str ("Checking project file """);
            Write_Str (Get_Name_String (Data.Name));
            Write_Line ("""");
         end if;

         Prj.Nmsc.Ada_Check (Project, Error_Report);
      end if;
   end Recursive_Check;

   -----------------------
   -- Recursive_Process --
   -----------------------

   procedure Recursive_Process
     (Project           : out Project_Id;
      From_Project_Node : Project_Node_Id;
      Modified_By       : Project_Id)
   is
      With_Clause : Project_Node_Id;

   begin
      if From_Project_Node = Empty_Node then
         Project := No_Project;

      else
         declare
            Processed_Data   : Project_Data := Empty_Project;
            Imported         : Project_List := Empty_Project_List;
            Declaration_Node : Project_Node_Id := Empty_Node;
            Name             : constant Name_Id :=
                                 Name_Of (From_Project_Node);

         begin
            Project := Processed_Projects.Get (Name);

            if Project /= No_Project then
               return;
            end if;

            Projects.Increment_Last;
            Project := Projects.Last;
            Processed_Projects.Set (Name, Project);
            Processed_Data.Name        := Name;
            Processed_Data.Path_Name   := Path_Name_Of (From_Project_Node);
            Processed_Data.Location    := Location_Of (From_Project_Node);
            Processed_Data.Directory   := Directory_Of (From_Project_Node);
            Processed_Data.Modified_By := Modified_By;
            Add_Attributes (Processed_Data.Decl, Attribute_First);
            With_Clause := First_With_Clause_Of (From_Project_Node);

            while With_Clause /= Empty_Node loop
               declare
                  New_Project : Project_Id;
                  New_Data    : Project_Data;

               begin
                  Recursive_Process
                    (Project           => New_Project,
                     From_Project_Node => Project_Node_Of (With_Clause),
                     Modified_By       => No_Project);
                  New_Data := Projects.Table (New_Project);

                  --  If we were the first project to import it,
                  --  set First_Referred_By to us.

                  if New_Data.First_Referred_By = No_Project then
                     New_Data.First_Referred_By := Project;
                     Projects.Table (New_Project) := New_Data;
                  end if;

                  --  Add this project to our list of imported projects

                  Project_Lists.Increment_Last;
                  Project_Lists.Table (Project_Lists.Last) :=
                    (Project => New_Project, Next => Empty_Project_List);

                  --  Imported is the id of the last imported project.
                  --  If it is nil, then this imported project is our first.

                  if Imported = Empty_Project_List then
                     Processed_Data.Imported_Projects := Project_Lists.Last;

                  else
                     Project_Lists.Table (Imported).Next := Project_Lists.Last;
                  end if;

                  Imported := Project_Lists.Last;

                  With_Clause := Next_With_Clause_Of (With_Clause);
               end;
            end loop;

            Declaration_Node := Project_Declaration_Of (From_Project_Node);

            Recursive_Process
              (Project           => Processed_Data.Modifies,
               From_Project_Node => Modified_Project_Of (Declaration_Node),
               Modified_By       => Project);

            Projects.Table (Project) := Processed_Data;

            Process_Declarative_Items
              (Project           => Project,
               From_Project_Node => From_Project_Node,
               Pkg               => No_Package,
               Item              => First_Declarative_Item_Of
                                      (Declaration_Node));

         end;
      end if;
   end Recursive_Process;

end Prj.Proc;