File: unitmanager.adb

package info (click to toggle)
spark 2012.0.deb-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 29,260 kB
  • ctags: 3,098
  • sloc: ada: 186,243; cpp: 13,497; makefile: 685; yacc: 440; lex: 176; ansic: 119; sh: 16
file content (678 lines) | stat: -rw-r--r-- 26,017 bytes parent folder | download | duplicates (3)
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
-------------------------------------------------------------------------------
-- (C) Altran Praxis Limited
-------------------------------------------------------------------------------
--
-- The SPARK toolset is free software; you can redistribute it and/or modify it
-- under terms of the GNU General Public License as published by the Free
-- Software Foundation; either version 3, or (at your option) any later
-- version. The SPARK toolset is distributed in the hope that it will be
-- useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
-- Public License for more details. You should have received a copy of the GNU
-- General Public License distributed with the SPARK toolset; see file
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy of
-- the license.
--
--=============================================================================

with SparkLex;
with RegularExpression;
with SPARK_IO;
with Directory_Operations;
with UnitManager.UnitStore;
with SparkMakeErrors;

package body UnitManager
--# own State is UnitManager.UnitStore.State;
is

   procedure Initialise
     (The_Directories : in     StringList.Object;
      Include         : in     StringList.Object;
      Exclude         : in     StringList.Object;
      Root_File       : in     E_Strings.T;
      Duplicates      : in     Boolean;
      Success         :    out Boolean)
   --# global in     CommandLineData.Content;
   --#        in     Dictionary.Dict;
   --#        in out ErrorHandler.Error_Context;
   --#        in out LexTokenManager.State;
   --#        in out SPARK_IO.File_Sys;
   --#        in out UnitStore.State;
   --#           out SparkLex.Curr_Line;
   --# derives ErrorHandler.Error_Context,
   --#         LexTokenManager.State,
   --#         SparkLex.Curr_Line,
   --#         SPARK_IO.File_Sys,
   --#         Success,
   --#         UnitStore.State            from CommandLineData.Content,
   --#                                         Dictionary.Dict,
   --#                                         Duplicates,
   --#                                         ErrorHandler.Error_Context,
   --#                                         Exclude,
   --#                                         Include,
   --#                                         LexTokenManager.State,
   --#                                         Root_File,
   --#                                         SPARK_IO.File_Sys,
   --#                                         The_Directories,
   --#                                         UnitStore.State;
   is
      Directory_It : StringList.Iterator;
      File_It      : StringList.Iterator;
      Include_It   : StringList.Iterator;
      Exclude_It   : StringList.Iterator;

      Current_Dir  : E_Strings.T;
      Current_File : E_Strings.T;

      Add_This_File : Boolean;

      The_Reg_Exp : RegularExpression.Object;

      --------------------------------------------------------------------------
      procedure Add_File (Current_File : in     E_Strings.T;
                          Duplicates   : in     Boolean;
                          Success      : in out Boolean)
      --# global in     CommandLineData.Content;
      --#        in     Dictionary.Dict;
      --#        in out ErrorHandler.Error_Context;
      --#        in out LexTokenManager.State;
      --#        in out SPARK_IO.File_Sys;
      --#        in out UnitStore.State;
      --#           out SparkLex.Curr_Line;
      --# derives ErrorHandler.Error_Context,
      --#         LexTokenManager.State,
      --#         UnitStore.State            from *,
      --#                                         CommandLineData.Content,
      --#                                         Current_File,
      --#                                         Dictionary.Dict,
      --#                                         ErrorHandler.Error_Context,
      --#                                         LexTokenManager.State,
      --#                                         SPARK_IO.File_Sys &
      --#         SparkLex.Curr_Line         from CommandLineData.Content,
      --#                                         Current_File,
      --#                                         Dictionary.Dict,
      --#                                         ErrorHandler.Error_Context,
      --#                                         LexTokenManager.State,
      --#                                         SPARK_IO.File_Sys &
      --#         SPARK_IO.File_Sys,
      --#         Success                    from *,
      --#                                         CommandLineData.Content,
      --#                                         Current_File,
      --#                                         Dictionary.Dict,
      --#                                         Duplicates,
      --#                                         ErrorHandler.Error_Context,
      --#                                         LexTokenManager.State,
      --#                                         SPARK_IO.File_Sys,
      --#                                         UnitStore.State;
      is
         Current_Unit : Unit.Object;
      begin
         -- Extract the unit
         Unit.Get_Unit (In_File  => Current_File,
                        The_Unit => Current_Unit);

         if Current_Unit = Unit.Null_Object then
            -- This will be reported as warning and the unit ignored.
            SparkMakeErrors.Report
              (The_Fault => SparkMakeErrors.Invalid_Unit,
               E_Str1    => Current_File,
               E_Str2    => E_Strings.Empty_String,
               E_Str3    => E_Strings.Empty_String);
         else
            UnitStore.Add (The_Unit => Current_Unit,
                           Added    => Success);
            if not Success then
               -- check to see if the filenames are different.  Okay if unit appears
               -- twice in same file, but fail if filenames are different, i.e.
               -- the same unit appears in more than one file.
               if E_Strings.Eq_String
                 (E_Str1 => Current_File,
                  E_Str2 => UnitStore.Get (The_Unit => Current_Unit.The_Id).The_File) then
                  Success := True;
               else
                  -- check to see whether duplicates are errors switch is set
                  -- report errors/warnings as appropriate
                  if Duplicates then
                     SparkMakeErrors.Report
                       (The_Fault => SparkMakeErrors.Duplicate_Errors,
                        E_Str1    => Current_Unit.The_Id.The_Name,
                        E_Str2    => Current_File,
                        E_Str3    => UnitStore.Get (The_Unit => Current_Unit.The_Id).The_File);
                     Success := False;
                  else
                     SparkMakeErrors.Report
                       (The_Fault => SparkMakeErrors.Duplicate_Okay,
                        E_Str1    => Current_Unit.The_Id.The_Name,
                        E_Str2    => Current_File,
                        E_Str3    => UnitStore.Get (The_Unit => Current_Unit.The_Id).The_File);
                     Success := True;
                  end if;
               end if;
            end if;
         end if;
      end Add_File;

      --------------------------------------------------------------------------
   begin
      Success := True;
      SparkLex.Clear_Line_Context;

      --ensure root file is added, even if not in current directory
      if not E_Strings.Is_Empty (E_Str => Root_File) then
         Add_File (Current_File => Root_File,
                   Duplicates   => Duplicates,
                   Success      => Success);
      end if;

      -- For all the directories
      --
      Directory_It := StringList.Get_First (In_List => The_Directories);
      while Success and not StringList.Is_Null (It => Directory_It) loop

         Current_Dir := StringList.Value (Directory_It);
         SPARK_IO.Put_String (SPARK_IO.Standard_Output, "Processing directory ", 0);
         E_Strings.Put_Line (File  => SPARK_IO.Standard_Output,
                             E_Str => Current_Dir);

         -- For all the include file regular expressions
         --
         Include_It := StringList.Get_First (In_List => Include);
         while Success and not StringList.Is_Null (It => Include_It) loop

            -- For all the files matching this regular expression
            --
            The_Reg_Exp := RegularExpression.Create (StringList.Value (Include_It));

            File_It :=
              StringList.Get_First
              (In_List => Directory_Operations.Find_Files (Matching     => The_Reg_Exp,
                                                           In_Directory => Current_Dir,
                                                           Recursively  => True));

            while Success and not StringList.Is_Null (It => File_It) loop

               Add_This_File := True;
               Current_File  := StringList.Value (File_It);

               -- don't exclude the root file
               if not E_Strings.Eq_String (E_Str1 => Current_File,
                                           E_Str2 => Root_File) then
                  -- For all the exclude file regular expressions
                  --
                  Exclude_It := StringList.Get_First (In_List => Exclude);
                  while not StringList.Is_Null (It => Exclude_It) loop

                     if RegularExpression.Matches
                       (E_Str       => Current_File,
                        The_Reg_Exp => RegularExpression.Create (StringList.Value (Exclude_It))) then

                        Add_This_File := False;
                        exit;
                     end if;
                     Exclude_It := StringList.Next (Exclude_It);
                  end loop;
               end if;

               if Add_This_File then
                  Add_File (Current_File => Current_File,
                            Duplicates   => Duplicates,
                            Success      => Success);
               end if;

               File_It := StringList.Next (File_It);
            end loop;

            Include_It := StringList.Next (Include_It);
         end loop;

         Directory_It := StringList.Next (Directory_It);
      end loop;

   end Initialise;

   -------------------------------------------------------------

   function Get_All_Units return  Units.Stack
   --# global in UnitStore.State;
   is
   begin
      return UnitStore.Get_All_Units;
   end Get_All_Units;

   -------------------------------------------------------------

   procedure Get_File (For_Unit : in     Unit.Id;
                       The_File :    out E_Strings.T;
                       Found    :    out Boolean)
   --# global in UnitStore.State;
   --# derives Found,
   --#         The_File from For_Unit,
   --#                       UnitStore.State;
   is
   begin
      The_File := UnitStore.Get (The_Unit => For_Unit).The_File;
      Found    := not E_Strings.Is_Empty (E_Str => The_File);
   end Get_File;

   -------------------------------------------------------------

   procedure Get_Unit (In_File  : in     E_Strings.T;
                       The_Unit :    out Unit.Id;
                       Found    :    out Boolean)
   --# global in UnitStore.State;
   --# derives Found,
   --#         The_Unit from In_File,
   --#                       UnitStore.State;
   is
      Current_Unit : Unit.Object;
      Id           : Unit.Id;
      All_Units    : Units.Stack;
   begin
      Found     := False;
      The_Unit  := Unit.Null_Id;
      All_Units := UnitStore.Get_All_Units;
      while not Units.IsEmpty (All_Units) loop
         Units.Pop (TheStack => All_Units,
                    TheUnit  => Id);
         Current_Unit := UnitStore.Get (The_Unit => Id);
         if E_Strings.Eq_String (E_Str1 => In_File,
                                 E_Str2 => Current_Unit.The_File) then
            Found    := True;
            The_Unit := Current_Unit.The_Id;
            exit;
         end if;
      end loop;
   end Get_Unit;

   -------------------------------------------------------------

   function Get (The_Unit : Unit.Id) return Unit.Object
   --# global in UnitStore.State;
   is
   begin
      return UnitStore.Get (The_Unit => The_Unit);
   end Get;

   -------------------------------------------------------------

   function Parent (Of_Unit : Unit.Id) return Unit.Id
   --# global in UnitStore.State;
   is
      Parent_Unit : Unit.Id;
   begin
      case Of_Unit.The_Kind is

         when Unit.Package_Specification_Unit | Unit.Main_Program_Unit | Unit.Package_Body_Unit =>
            -- These units do not have parents.
            Parent_Unit := Unit.Null_Id;

         when Unit.Separate_Body_Unit =>

            Parent_Unit := UnitStore.Get_Body_Unit (With_Name => Unit.Prefix (Of_Unit.The_Name)).The_Id;

         when Unit.Child_Specification_Unit =>

            Parent_Unit := UnitStore.Get_Specification_Unit (With_Name => Unit.Prefix (Of_Unit.The_Name)).The_Id;

      end case;
      return Parent_Unit;
   end Parent;

   -------------------------------------------------------------

   function Package_Body (For_Unit : Unit.Id) return Unit.Id
   --# global in UnitStore.State;
   is
      Result : Unit.Id;
   begin
      case For_Unit.The_Kind is

         when Unit.Specification_Unit =>

            Result := Unit.Id'(The_Name => For_Unit.The_Name,
                               The_Kind => Unit.Package_Body_Unit);

         when Unit.Package_Body_Unit =>

            Result := For_Unit;

         when Unit.Separate_Body_Unit =>

            Result := For_Unit;
            while Result.The_Kind /= Unit.Package_Body_Unit loop
               Result := Parent (Of_Unit => Result);
            end loop;

         when Unit.Main_Program_Unit =>

            Result := Unit.Null_Id;

      end case;
      return Result;
   end Package_Body;

   -------------------------------------------------------------

   function Is_A_Component (This_Unit    : Unit.Id;
                            Of_This_Unit : Unit.Id) return Boolean
   --# global in UnitStore.State;
   --
   -- Returns True is This_Unit is a component Of_This_Unit according to
   -- the rules given below
   is
      Result      : Boolean := False;
      Parent_Unit : Unit.Id;
   begin
      case This_Unit.The_Kind is

         when Unit.Private_Child_Package_Specification_Unit =>

            -- If This_Unit is a private child then it is a component
            -- Of_This_Unit if Of_This_Unit is its immediate parent.
            Result := E_Strings.Eq_String (E_Str1 => Of_This_Unit.The_Name,
                                           E_Str2 => Parent (Of_Unit => This_Unit).The_Name);

         when Unit.Public_Child_Package_Specification_Unit =>

            -- If This_Unit is a public child then it is a component
            -- Of_This_Unit if there is exactly one private parent
            -- between it and Of_This_Unit and this private parent is an
            -- immediate child Of_This_Unit.
            Parent_Unit := Parent (Of_Unit => This_Unit);
            while Parent_Unit /= Unit.Null_Id loop
               if Parent_Unit.The_Kind = Unit.Private_Child_Package_Specification_Unit then
                  Result :=
                    E_Strings.Eq_String (E_Str1 => Of_This_Unit.The_Name,
                                         E_Str2 => Parent (Of_Unit => Parent_Unit).The_Name);
                  exit;
               end if;
               Parent_Unit := Parent (Of_Unit => Parent_Unit);
            end loop;

         when others =>
            null;

      end case;
      return Result;
   end Is_A_Component;

   -------------------------------------------------------------

   function Inherited_Units (For_Unit : Unit.Id) return Units.Stack
   --# global in UnitStore.State;
   is
      Result         : Units.Stack;
      The_Unit       : Unit.Object;
      Inherited_Unit : Unit.Object;
      It             : StringList.Iterator;
   begin
      Result   := Units.NullStack;
      The_Unit := UnitStore.Get (The_Unit => For_Unit);
      if The_Unit /= Unit.Null_Object then
         It := StringList.Get_First (In_List => The_Unit.The_Inherited_Units);
         while not StringList.Is_Null (It => It) loop
            Inherited_Unit := UnitStore.Get_Specification_Unit (With_Name => StringList.Value (It));
            if Inherited_Unit /= Unit.Null_Object then
               Units.Push (TheStack => Result,
                           TheUnit  => Inherited_Unit.The_Id);
            end if;
            It := StringList.Next (It);
         end loop;
      end if;
      return Result;
   end Inherited_Units;

   -------------------------------------------------------------

   function Withed_Units (For_Unit : Unit.Id) return Units.Stack
   --# global in UnitStore.State;
   is
      Result      : Units.Stack;
      The_Unit    : Unit.Object;
      Withed_Unit : Unit.Object;
      It          : StringList.Iterator;
   begin
      Result   := Units.NullStack;
      The_Unit := UnitStore.Get (The_Unit => For_Unit);
      if The_Unit /= Unit.Null_Object then
         It := StringList.Get_First (In_List => The_Unit.The_Withed_Units);
         while not StringList.Is_Null (It => It) loop
            Withed_Unit := UnitStore.Get_Specification_Unit (With_Name => StringList.Value (It));
            if Withed_Unit /= Unit.Null_Object then
               Units.Push (TheStack => Result,
                           TheUnit  => Withed_Unit.The_Id);
            end if;
            It := StringList.Next (It);
         end loop;
      end if;
      return Result;
   end Withed_Units;

   -------------------------------------------------------------

   function Withed_Components (For_Unit : Unit.Id) return Units.Stack
   --# global in UnitStore.State;
   is
      The_Withed_Units : Units.Stack;
      Result           : Units.Stack;
      Id               : Unit.Id;
      Parent_Unit      : Unit.Id;
   begin
      Result           := Units.NullStack;
      The_Withed_Units := Withed_Units (For_Unit => For_Unit);
      while not Units.IsEmpty (The_Withed_Units) loop
         Units.Pop (TheStack => The_Withed_Units,
                    TheUnit  => Id);

         Parent_Unit := For_Unit;
         while Parent_Unit /= Unit.Null_Id loop
            if Is_A_Component (This_Unit    => Id,
                               Of_This_Unit => Parent_Unit) then
               Units.Push (TheStack => Result,
                           TheUnit  => Id);
            end if;
            Parent_Unit := Parent (Of_Unit => Parent_Unit);
         end loop;
      end loop;
      return Result;
   end Withed_Components;

   -------------------------------------------------------------

   function Separate_Units (For_Unit : Unit.Id) return Units.Stack
   --# global in UnitStore.State;
   is
      All_Units    : Units.Stack;
      Id           : Unit.Id;
      Result       : Units.Stack;
      Current_Unit : Unit.Object;
   begin
      Result    := Units.NullStack;
      All_Units := UnitStore.Get_All_Units;
      while not Units.IsEmpty (All_Units) loop
         Units.Pop (TheStack => All_Units,
                    TheUnit  => Id);
         Current_Unit := UnitStore.Get (The_Unit => Id);
         if Current_Unit.The_Id.The_Kind = Unit.Separate_Body_Unit then
            if Unit.Are_Equal (L => Parent (Of_Unit => Current_Unit.The_Id),
                               R => For_Unit) then
               Units.Push (TheStack => Result,
                           TheUnit  => Current_Unit.The_Id);
            end if;
         end if;
      end loop;
      return Result;
   end Separate_Units;

   -------------------------------------------------------------

   function Required_Units (For_Unit : Unit.Id) return Units.Stack
   --# global in UnitStore.State;
   is
      Result                : Units.Stack;
      The_Withed_Components : Units.Stack;
      Id                    : Unit.Id;
   begin
      -- We'll need all the inherited units
      Result := Inherited_Units (For_Unit => For_Unit);

      if For_Unit.The_Kind = Unit.Separate_Body_Unit then

         -- .. we need any withed components as no inherit is required
         The_Withed_Components := Withed_Components (For_Unit => For_Unit);

         while not Units.IsEmpty (The_Withed_Components) loop
            Units.Pop (TheStack => The_Withed_Components,
                       TheUnit  => Id);
            Units.Push (TheStack => Result,
                        TheUnit  => Id);
         end loop;

         -- We'll also need the body if it's a separate ...
         Units.Push (TheStack => Result,
                     TheUnit  => Parent (Of_Unit => For_Unit));

      elsif For_Unit.The_Kind = Unit.Package_Body_Unit then

         -- .. we need any withed components as no inherit is required
         The_Withed_Components := Withed_Components (For_Unit => For_Unit);

         while not Units.IsEmpty (The_Withed_Components) loop
            Units.Pop (TheStack => The_Withed_Components,
                       TheUnit  => Id);
            Units.Push (TheStack => Result,
                        TheUnit  => Id);
         end loop;

         -- ... and the spec ...
         Units.Push (TheStack => Result,
                     TheUnit  => UnitStore.Get_Specification_Unit (With_Name => For_Unit.The_Name).The_Id);

      elsif For_Unit.The_Kind in Unit.Child_Specification_Unit then
         -- ... or the parent if it's a child package specification
         Units.Push (TheStack => Result,
                     TheUnit  => Parent (Of_Unit => For_Unit));
      end if;
      return Result;
   end Required_Units;

   -------------------------------------------------------------

   function Components (For_Unit : Unit.Id) return Units.Stack
   --# global in UnitStore.State;
   is
      Result    : Units.Stack;
      Id        : Unit.Id;
      All_Units : Units.Stack;
   begin
      Result    := Units.NullStack;
      All_Units := UnitStore.Get_All_Units;
      while not Units.IsEmpty (All_Units) loop
         Units.Pop (TheStack => All_Units,
                    TheUnit  => Id);
         if Is_A_Component (This_Unit    => Id,
                            Of_This_Unit => For_Unit) then
            Units.Push (TheStack => Result,
                        TheUnit  => Id);
         end if;
      end loop;
      return Result;
   end Components;

   -------------------------------------------------------------
   -- Find the root units. Slightly trickier than it may seem at
   -- first because we are not simply dealing with relationships
   -- between packages (eg package P requires package Q) but have
   -- to consider relationships between all compilation units.
   -- For example, q.adb requires q.ads. If q.adb isn't required
   -- by anything else (ie no separates) then it would seem to be
   -- a 'root' but we must not treat it as one because any
   -- package bodies are automatically added to the meta file
   -- after their corresponding specs. So the roots we want to
   -- find are:
   --  - any main programs;
   --  - any package specifications that are not required by
   --    other packages or main programs.
   function Find_Roots return  Units.Stack
   --# global in UnitStore.State;
   is
      All_Units : Units.Stack;
      The_Unit  : Unit.Id;
      Result    : Units.Stack;

      -- Return value indicates whether there are any other units
      -- that require the given unit.
      function Is_Required (This_Unit : Unit.Id) return Boolean
      --# global in UnitStore.State;
      is
         Other_Units    : Units.Stack;
         Req_Units      : Units.Stack;
         The_Req_Unit   : Unit.Id;
         The_Other_Unit : Unit.Id;
         Found          : Boolean := False;
      begin

         Other_Units := Get_All_Units;

         -- Check whether each other unit requires this unit.
         while not Found and not Units.IsEmpty (Other_Units) loop

            Units.Pop (TheStack => Other_Units,
                       TheUnit  => The_Other_Unit);

            -- Get the required units for the other unit
            Req_Units := Required_Units (For_Unit => The_Other_Unit);

            -- Check each other unit to see if it requires this unit.
            -- If any other unit requires it then it can't be a root.
            while not Found and not Units.IsEmpty (Req_Units) loop

               Units.Pop (TheStack => Req_Units,
                          TheUnit  => The_Req_Unit);

               -- Don't count a spec as 'required' if the only thing that needs it
               -- is its own body, otherwise the presence of a body will prevent a
               -- spec from being counted as a root.
               if The_Req_Unit = This_Unit and not (Package_Body (This_Unit) = The_Other_Unit) then
                  Found := True;
               end if;

            end loop;

         end loop;

         return Found;

      end Is_Required;

   begin -- Find_Roots

      All_Units := Get_All_Units;
      Result    := Units.NullStack;

      -- Check all the units we know about...
      while not Units.IsEmpty (All_Units) loop

         Units.Pop (TheStack => All_Units,
                    TheUnit  => The_Unit);

         -- If it's a main program, or a package spec that isn't
         -- required by any other units, then it must be a root.
         -- (Bodies always depend on their specs, and separates
         -- always depend on bodies, so they can never be roots.)
         if The_Unit.The_Kind = Unit.Main_Program_Unit
           or else ((The_Unit.The_Kind in Unit.Specification_Unit) and not Is_Required (This_Unit => The_Unit)) then

            Units.Push (TheStack => Result,
                        TheUnit  => The_Unit);

         end if;

      end loop;

      return Result;

   end Find_Roots;

end UnitManager;