OSDN Git Service

2007-08-14 Geert Bosch <bosch@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / lib-load.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             L I B . L O A D                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2007, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Atree;    use Atree;
28 with Debug;    use Debug;
29 with Einfo;    use Einfo;
30 with Errout;   use Errout;
31 with Fname;    use Fname;
32 with Fname.UF; use Fname.UF;
33 with Nlists;   use Nlists;
34 with Nmake;    use Nmake;
35 with Opt;      use Opt;
36 with Osint;    use Osint;
37 with Osint.C;  use Osint.C;
38 with Output;   use Output;
39 with Par;
40 with Restrict; use Restrict;
41 with Scn;      use Scn;
42 with Sinfo;    use Sinfo;
43 with Sinput;   use Sinput;
44 with Sinput.L; use Sinput.L;
45 with Stand;    use Stand;
46 with Tbuild;   use Tbuild;
47 with Uname;    use Uname;
48
49 package body Lib.Load is
50
51    -----------------------
52    -- Local Subprograms --
53    -----------------------
54
55    function From_Limited_With_Chain return Boolean;
56    --  Check whether a possible circular dependence includes units that
57    --  have been loaded through limited_with clauses, in which case there
58    --  is no real circularity.
59
60    function Spec_Is_Irrelevant
61      (Spec_Unit : Unit_Number_Type;
62       Body_Unit : Unit_Number_Type) return Boolean;
63    --  The Spec_Unit and Body_Unit parameters are the unit numbers of the
64    --  spec file that corresponds to the main unit which is a body. This
65    --  function determines if the spec file is irrelevant and will be
66    --  overridden by the body as described in RM 10.1.4(4). See description
67    --  in "Special Handling of Subprogram Bodies" for further details.
68
69    procedure Write_Dependency_Chain;
70    --  This procedure is used to generate error message info lines that
71    --  trace the current dependency chain when a load error occurs.
72
73    ------------------------------
74    -- Change_Main_Unit_To_Spec --
75    ------------------------------
76
77    procedure Change_Main_Unit_To_Spec is
78       U : Unit_Record renames Units.Table (Main_Unit);
79       N : File_Name_Type;
80       X : Source_File_Index;
81
82    begin
83       --  Get name of unit body
84
85       Get_Name_String (U.Unit_File_Name);
86
87       --  Note: for the following we should really generalize and consult the
88       --  file name pattern data, but for now we just deal with the common
89       --  naming cases, which is probably good enough in practice ???
90
91       --  Change .adb to .ads
92
93       if Name_Len >= 5
94         and then Name_Buffer (Name_Len - 3 .. Name_Len) = ".adb"
95       then
96          Name_Buffer (Name_Len) := 's';
97
98       --  Change .2.ada to .1.ada (Rational convention)
99
100       elsif Name_Len >= 7
101         and then Name_Buffer (Name_Len - 5 .. Name_Len) = ".2.ada"
102       then
103          Name_Buffer (Name_Len - 4) := '1';
104
105       --  Change .ada to _.ada (DEC convention)
106
107       elsif Name_Len >= 5
108         and then Name_Buffer (Name_Len - 3 .. Name_Len) = ".ada"
109       then
110          Name_Buffer (Name_Len - 3 .. Name_Len + 1) := "_.ada";
111          Name_Len := Name_Len + 1;
112
113       --  No match, don't make the change
114
115       else
116          return;
117       end if;
118
119       --  Try loading the spec
120
121       N := Name_Find;
122       X := Load_Source_File (N);
123
124       --  No change if we did not find the spec
125
126       if X = No_Source_File then
127          return;
128       end if;
129
130       --  Otherwise modify Main_Unit entry to point to spec
131
132       U.Unit_File_Name := N;
133       U.Source_Index := X;
134    end Change_Main_Unit_To_Spec;
135
136    -------------------------------
137    -- Create_Dummy_Package_Unit --
138    -------------------------------
139
140    function Create_Dummy_Package_Unit
141      (With_Node : Node_Id;
142       Spec_Name : Unit_Name_Type) return Unit_Number_Type
143    is
144       Unum         : Unit_Number_Type;
145       Cunit_Entity : Entity_Id;
146       Cunit        : Node_Id;
147       Du_Name      : Node_Or_Entity_Id;
148       End_Lab      : Node_Id;
149       Save_CS      : constant Boolean := Get_Comes_From_Source_Default;
150
151    begin
152       --  The created dummy package unit does not come from source
153
154       Set_Comes_From_Source_Default (False);
155
156       --  Normal package
157
158       if Nkind (Name (With_Node)) = N_Identifier then
159          Cunit_Entity :=
160            Make_Defining_Identifier (No_Location,
161              Chars => Chars (Name (With_Node)));
162          Du_Name := Cunit_Entity;
163          End_Lab := New_Occurrence_Of (Cunit_Entity, No_Location);
164
165       --  Child package
166
167       else
168          Cunit_Entity :=
169            Make_Defining_Identifier (No_Location,
170              Chars => Chars (Selector_Name (Name (With_Node))));
171          Du_Name :=
172            Make_Defining_Program_Unit_Name (No_Location,
173              Name => New_Copy_Tree (Prefix (Name (With_Node))),
174              Defining_Identifier => Cunit_Entity);
175
176          Set_Is_Child_Unit (Cunit_Entity);
177
178          End_Lab :=
179            Make_Designator (No_Location,
180              Name => New_Copy_Tree (Prefix (Name (With_Node))),
181              Identifier => New_Occurrence_Of (Cunit_Entity, No_Location));
182       end if;
183
184       Set_Scope (Cunit_Entity, Standard_Standard);
185
186       Cunit :=
187         Make_Compilation_Unit (No_Location,
188           Context_Items => Empty_List,
189           Unit =>
190             Make_Package_Declaration (No_Location,
191               Specification =>
192                 Make_Package_Specification (No_Location,
193                   Defining_Unit_Name   => Du_Name,
194                   Visible_Declarations => Empty_List,
195                   End_Label            => End_Lab)),
196           Aux_Decls_Node =>
197             Make_Compilation_Unit_Aux (No_Location));
198
199       --  Mark the dummy package as analyzed to prevent analysis of this
200       --  (non-existent) unit in -gnatQ mode because at the moment the
201       --  structure and attributes of this dummy package does not allow
202       --  a normal analysis of this unit
203
204       Set_Analyzed (Cunit);
205
206       Units.Increment_Last;
207       Unum := Units.Last;
208
209       Units.Table (Unum) := (
210         Cunit           => Cunit,
211         Cunit_Entity    => Cunit_Entity,
212         Dependency_Num  => 0,
213         Dynamic_Elab    => False,
214         Error_Location  => Sloc (With_Node),
215         Expected_Unit   => Spec_Name,
216         Fatal_Error     => True,
217         Generate_Code   => False,
218         Has_RACW        => False,
219         Ident_String    => Empty,
220         Loading         => False,
221         Main_Priority   => Default_Main_Priority,
222         Munit_Index     => 0,
223         Serial_Number   => 0,
224         Source_Index    => No_Source_File,
225         Unit_File_Name  => Get_File_Name (Spec_Name, Subunit => False),
226         Unit_Name       => Spec_Name,
227         Version         => 0);
228
229       Set_Comes_From_Source_Default (Save_CS);
230       Set_Error_Posted (Cunit_Entity);
231       Set_Error_Posted (Cunit);
232       return Unum;
233    end Create_Dummy_Package_Unit;
234
235    -----------------------------
236    -- From_Limited_With_Chain --
237    -----------------------------
238
239    function From_Limited_With_Chain return Boolean is
240       Curr_Num : constant Unit_Number_Type :=
241                    Load_Stack.Table (Load_Stack.Last).Unit_Number;
242
243    begin
244       --  True if the current load operation is through a limited_with clause
245       --  and we are not within a loop of regular with_clauses.
246
247       for U in reverse Load_Stack.First .. Load_Stack.Last - 1 loop
248          if Load_Stack.Table (U).Unit_Number = Curr_Num then
249             return False;
250
251          elsif Present (Load_Stack.Table (U).With_Node)
252            and then Limited_Present (Load_Stack.Table (U).With_Node)
253          then
254             return True;
255          end if;
256       end loop;
257
258       return False;
259    end From_Limited_With_Chain;
260
261    ----------------
262    -- Initialize --
263    ----------------
264
265    procedure Initialize is
266    begin
267       Units.Init;
268       Load_Stack.Init;
269    end Initialize;
270
271    ------------------------
272    -- Initialize_Version --
273    ------------------------
274
275    procedure Initialize_Version (U : Unit_Number_Type) is
276    begin
277       Units.Table (U).Version := Source_Checksum (Source_Index (U));
278    end Initialize_Version;
279
280    ----------------------
281    -- Load_Main_Source --
282    ----------------------
283
284    procedure Load_Main_Source is
285       Fname   : File_Name_Type;
286       Version : Word := 0;
287
288    begin
289       Load_Stack.Increment_Last;
290       Load_Stack.Table (Load_Stack.Last) := (Main_Unit, Empty);
291
292       --  Initialize unit table entry for Main_Unit. Note that we don't know
293       --  the unit name yet, that gets filled in when the parser parses the
294       --  main unit, at which time a check is made that it matches the main
295       --  file name, and then the Unit_Name field is set. The Cunit and
296       --  Cunit_Entity fields also get filled in later by the parser.
297
298       Units.Increment_Last;
299       Fname := Next_Main_Source;
300
301       Units.Table (Main_Unit).Unit_File_Name := Fname;
302
303       if Fname /= No_File then
304          Main_Source_File := Load_Source_File (Fname);
305          Current_Error_Source_File := Main_Source_File;
306
307          if Main_Source_File /= No_Source_File then
308             Version := Source_Checksum (Main_Source_File);
309          end if;
310
311          Units.Table (Main_Unit) := (
312            Cunit           => Empty,
313            Cunit_Entity    => Empty,
314            Dependency_Num  => 0,
315            Dynamic_Elab    => False,
316            Error_Location  => No_Location,
317            Expected_Unit   => No_Unit_Name,
318            Fatal_Error     => False,
319            Generate_Code   => False,
320            Has_RACW        => False,
321            Ident_String    => Empty,
322            Loading         => True,
323            Main_Priority   => Default_Main_Priority,
324            Munit_Index     => 0,
325            Serial_Number   => 0,
326            Source_Index    => Main_Source_File,
327            Unit_File_Name  => Fname,
328            Unit_Name       => No_Unit_Name,
329            Version         => Version);
330       end if;
331    end Load_Main_Source;
332
333    ---------------
334    -- Load_Unit --
335    ---------------
336
337    function Load_Unit
338      (Load_Name         : Unit_Name_Type;
339       Required          : Boolean;
340       Error_Node        : Node_Id;
341       Subunit           : Boolean;
342       Corr_Body         : Unit_Number_Type := No_Unit;
343       Renamings         : Boolean          := False;
344       With_Node         : Node_Id          := Empty) return Unit_Number_Type
345    is
346       Calling_Unit : Unit_Number_Type;
347       Uname_Actual : Unit_Name_Type;
348       Unum         : Unit_Number_Type;
349       Unump        : Unit_Number_Type;
350       Fname        : File_Name_Type;
351       Src_Ind      : Source_File_Index;
352
353    --  Start of processing for Load_Unit
354
355    begin
356       --  If renamings are allowed and we have a child unit name, then we
357       --  must first load the parent to deal with finding the real name.
358
359       if Renamings and then Is_Child_Name (Load_Name) then
360          Unump :=
361            Load_Unit
362              (Load_Name  => Get_Parent_Spec_Name (Load_Name),
363               Required   => Required,
364               Subunit    => False,
365               Renamings  => True,
366               Error_Node => Error_Node);
367
368          if Unump = No_Unit then
369             return No_Unit;
370          end if;
371
372          --  If parent is a renaming, then we use the renamed package as
373          --  the actual parent for the subsequent load operation.
374
375          if Nkind (Unit (Cunit (Unump))) = N_Package_Renaming_Declaration then
376             Uname_Actual :=
377               New_Child
378                 (Load_Name, Get_Unit_Name (Name (Unit (Cunit (Unump)))));
379
380             --  Save the renaming entity, to establish its visibility when
381             --  installing the context. The implicit with is on this entity,
382             --  not on the package it renames.
383
384             if Nkind (Error_Node) = N_With_Clause
385               and then Nkind (Name (Error_Node)) = N_Selected_Component
386             then
387                declare
388                   Par : Node_Id := Name (Error_Node);
389
390                begin
391                   while Nkind (Par) = N_Selected_Component
392                     and then Chars (Selector_Name (Par)) /=
393                       Chars (Cunit_Entity (Unump))
394                   loop
395                      Par := Prefix (Par);
396                   end loop;
397
398                   --  Case of some intermediate parent is a renaming
399
400                   if Nkind (Par) = N_Selected_Component then
401                      Set_Entity (Selector_Name (Par), Cunit_Entity (Unump));
402
403                   --  Case where the ultimate parent is a renaming
404
405                   else
406                      Set_Entity (Par, Cunit_Entity (Unump));
407                   end if;
408                end;
409             end if;
410
411          --  If the parent is not a renaming, then get its name (this may
412          --  be different from the parent spec name obtained above because
413          --  of renamings higher up in the hierarchy).
414
415          else
416             Uname_Actual := New_Child (Load_Name, Unit_Name (Unump));
417          end if;
418
419       --  Here if unit to be loaded is not a child unit
420
421       else
422          Uname_Actual := Load_Name;
423       end if;
424
425       Fname := Get_File_Name (Uname_Actual, Subunit);
426
427       if Debug_Flag_L then
428          Write_Eol;
429          Write_Str ("*** Load request for unit: ");
430          Write_Unit_Name (Load_Name);
431
432          if Required then
433             Write_Str (" (Required = True)");
434          else
435             Write_Str (" (Required = False)");
436          end if;
437
438          Write_Eol;
439
440          if Uname_Actual /= Load_Name then
441             Write_Str ("*** Actual unit loaded: ");
442             Write_Unit_Name (Uname_Actual);
443          end if;
444       end if;
445
446       --  Capture error location if it is for the main unit. The idea is to
447       --  post errors on the main unit location, not the most recent unit.
448       --  Note: Unit_Name (Main_Unit) is not set if we are parsing gnat.adc.
449
450       if Present (Error_Node)
451         and then Unit_Name (Main_Unit) /= No_Unit_Name
452       then
453          --  It seems like In_Extended_Main_Source_Unit (Error_Node) would
454          --  do the trick here, but that's wrong, it is much too early to
455          --  call this routine. We are still in the parser, and the required
456          --  semantic information is not established yet. So we base the
457          --  judgment on unit names.
458
459          Get_External_Unit_Name_String (Unit_Name (Main_Unit));
460
461          declare
462             Main_Unit_Name : constant String := Name_Buffer (1 .. Name_Len);
463
464          begin
465             Get_External_Unit_Name_String
466               (Unit_Name (Get_Source_Unit (Error_Node)));
467
468             --  If the two names are identical, then for sure we are part
469             --  of the extended main unit
470
471             if Main_Unit_Name = Name_Buffer (1 .. Name_Len) then
472                Load_Msg_Sloc := Sloc (Error_Node);
473
474             --  If the load is called from a with_type clause, the error
475             --  node is correct.
476
477             --  Otherwise, check for the subunit case, and if so, consider
478             --  we have a match if one name is a prefix of the other name.
479
480             else
481                if Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
482                     or else
483                   Nkind (Unit (Cunit (Get_Source_Unit (Error_Node)))) =
484                                                                 N_Subunit
485                then
486                   Name_Len := Integer'Min (Name_Len, Main_Unit_Name'Length);
487
488                   if Name_Buffer (1 .. Name_Len)
489                         =
490                      Main_Unit_Name (1 .. Name_Len)
491                   then
492                      Load_Msg_Sloc := Sloc (Error_Node);
493                   end if;
494                end if;
495             end if;
496          end;
497       end if;
498
499       --  If we are generating error messages, then capture calling unit
500
501       if Present (Error_Node) then
502          Calling_Unit := Get_Source_Unit (Error_Node);
503       else
504          Calling_Unit := No_Unit;
505       end if;
506
507       --  See if we already have an entry for this unit
508
509       Unum := Main_Unit;
510
511       while Unum <= Units.Last loop
512          exit when Uname_Actual = Units.Table (Unum).Unit_Name;
513          Unum := Unum + 1;
514       end loop;
515
516       --  Whether or not the entry was found, Unum is now the right value,
517       --  since it is one more than Units.Last (i.e. the index of the new
518       --  entry we will create) in the not found case.
519
520       --  A special check is necessary in the unit not found case. If the unit
521       --  is not found, but the file in which it lives has already been loaded,
522       --  then we have the problem that the file does not contain the unit that
523       --  is needed. We simply treat this as a file not found condition.
524
525       --  We skip this test in multiple unit per file mode since in this
526       --  case we can have multiple units from the same source file.
527
528       if Unum > Units.Last and then Get_Unit_Index (Uname_Actual) = 0 then
529          for J in Units.First .. Units.Last loop
530             if Fname = Units.Table (J).Unit_File_Name then
531                if Debug_Flag_L then
532                   Write_Str ("  file does not contain unit, Unit_Number = ");
533                   Write_Int (Int (Unum));
534                   Write_Eol;
535                   Write_Eol;
536                end if;
537
538                if Present (Error_Node) then
539                   if Is_Predefined_File_Name (Fname) then
540                      Error_Msg_Unit_1 := Uname_Actual;
541                      Error_Msg
542                        ("$$ is not a language defined unit", Load_Msg_Sloc);
543                   else
544                      Error_Msg_File_1 := Fname;
545                      Error_Msg_Unit_1 := Uname_Actual;
546                      Error_Msg ("File{ does not contain unit$", Load_Msg_Sloc);
547                   end if;
548
549                   Write_Dependency_Chain;
550                   return No_Unit;
551
552                else
553                   return No_Unit;
554                end if;
555             end if;
556          end loop;
557       end if;
558
559       --  If we are proceeding with load, then make load stack entry,
560       --  and indicate the kind of with_clause responsible for the load.
561
562       Load_Stack.Increment_Last;
563       Load_Stack.Table (Load_Stack.Last) := (Unum, With_Node);
564
565       --  Case of entry already in table
566
567       if Unum <= Units.Last then
568
569          --  Here is where we check for a circular dependency, which is
570          --  an attempt to load a unit which is currently in the process
571          --  of being loaded. We do *not* care about a circular chain that
572          --  leads back to a body, because this kind of circular dependence
573          --  legitimately occurs (e.g. two package bodies that contain
574          --  inlined subprogram referenced by the other).
575
576          --  Ada 2005 (AI-50217): We also ignore limited_with clauses, because
577          --  their purpose is precisely to create legal circular structures.
578
579          if Loading (Unum)
580            and then (Is_Spec_Name (Units.Table (Unum).Unit_Name)
581                        or else Acts_As_Spec (Units.Table (Unum).Cunit))
582            and then (Nkind (Error_Node) /= N_With_Clause
583                        or else not Limited_Present (Error_Node))
584            and then not From_Limited_With_Chain
585          then
586             if Debug_Flag_L then
587                Write_Str ("  circular dependency encountered");
588                Write_Eol;
589             end if;
590
591             if Present (Error_Node) then
592                Error_Msg ("circular unit dependency", Load_Msg_Sloc);
593                Write_Dependency_Chain;
594             else
595                Load_Stack.Decrement_Last;
596             end if;
597
598             return No_Unit;
599          end if;
600
601          if Debug_Flag_L then
602             Write_Str ("  unit already in file table, Unit_Number = ");
603             Write_Int (Int (Unum));
604             Write_Eol;
605          end if;
606
607          Load_Stack.Decrement_Last;
608          return Unum;
609
610       --  Unit is not already in table, so try to open the file
611
612       else
613          if Debug_Flag_L then
614             Write_Str ("  attempt unit load, Unit_Number = ");
615             Write_Int (Int (Unum));
616             Write_Eol;
617          end if;
618
619          Src_Ind := Load_Source_File (Fname);
620
621          --  Make a partial entry in the file table, used even in the file not
622          --  found case to print the dependency chain including the last entry
623
624          Units.Increment_Last;
625          Units.Table (Unum).Unit_Name := Uname_Actual;
626
627          --  File was found
628
629          if Src_Ind /= No_Source_File then
630             Units.Table (Unum) := (
631               Cunit           => Empty,
632               Cunit_Entity    => Empty,
633               Dependency_Num  => 0,
634               Dynamic_Elab    => False,
635               Error_Location  => Sloc (Error_Node),
636               Expected_Unit   => Uname_Actual,
637               Fatal_Error     => False,
638               Generate_Code   => False,
639               Has_RACW        => False,
640               Ident_String    => Empty,
641               Loading         => True,
642               Main_Priority   => Default_Main_Priority,
643               Munit_Index     => 0,
644               Serial_Number   => 0,
645               Source_Index    => Src_Ind,
646               Unit_File_Name  => Fname,
647               Unit_Name       => Uname_Actual,
648               Version         => Source_Checksum (Src_Ind));
649
650             --  Parse the new unit
651
652             declare
653                Save_Index : constant Nat := Multiple_Unit_Index;
654             begin
655                Multiple_Unit_Index := Get_Unit_Index (Uname_Actual);
656                Units.Table (Unum).Munit_Index := Multiple_Unit_Index;
657                Initialize_Scanner (Unum, Source_Index (Unum));
658                Discard_List (Par (Configuration_Pragmas => False));
659                Multiple_Unit_Index := Save_Index;
660                Set_Loading (Unum, False);
661             end;
662
663             --  If spec is irrelevant, then post errors and quit
664
665             if Corr_Body /= No_Unit
666               and then Spec_Is_Irrelevant (Unum, Corr_Body)
667             then
668                Error_Msg_File_1 := Unit_File_Name (Corr_Body);
669                Error_Msg
670                  ("cannot compile subprogram in file {!", Load_Msg_Sloc);
671                Error_Msg_File_1 := Unit_File_Name (Unum);
672                Error_Msg
673                  ("\incorrect spec in file { must be removed first!",
674                   Load_Msg_Sloc);
675                return No_Unit;
676             end if;
677
678             --  If loaded unit had a fatal error, then caller inherits it!
679
680             if Units.Table (Unum).Fatal_Error
681               and then Present (Error_Node)
682             then
683                Units.Table (Calling_Unit).Fatal_Error := True;
684             end if;
685
686             --  Remove load stack entry and return the entry in the file table
687
688             Load_Stack.Decrement_Last;
689             return Unum;
690
691          --  Case of file not found
692
693          else
694             if Debug_Flag_L then
695                Write_Str ("  file was not found, load failed");
696                Write_Eol;
697             end if;
698
699             --  Generate message if unit required
700
701             if Required and then Present (Error_Node) then
702                if Is_Predefined_File_Name (Fname) then
703
704                   --  This is a predefined library unit which is not present
705                   --  in the run time. If a predefined unit is not available
706                   --  it may very likely be the case that there is also pragma
707                   --  Restriction forbidding its usage. This is typically the
708                   --  case when building a configurable run time, where the
709                   --  usage of certain run-time units units is restricted by
710                   --  means of both the corresponding pragma Restriction (such
711                   --  as No_Calendar), and by not including the unit. Hence,
712                   --  we check whether this predefined unit is forbidden, so
713                   --  that the message about the restriction violation is
714                   --  generated, if needed.
715
716                   Check_Restricted_Unit (Load_Name, Error_Node);
717
718                   Error_Msg_Unit_1 := Uname_Actual;
719                   Error_Msg
720                     ("$$ is not a predefined library unit", Load_Msg_Sloc);
721
722                else
723                   Error_Msg_File_1 := Fname;
724                   Error_Msg ("file{ not found", Load_Msg_Sloc);
725                end if;
726
727                Write_Dependency_Chain;
728
729                --  Remove unit from stack, to avoid cascaded errors on
730                --  subsequent missing files.
731
732                Load_Stack.Decrement_Last;
733                Units.Decrement_Last;
734
735             --  If unit not required, remove load stack entry and the junk
736             --  file table entry, and return No_Unit to indicate not found,
737
738             else
739                Load_Stack.Decrement_Last;
740                Units.Decrement_Last;
741             end if;
742
743             return No_Unit;
744          end if;
745       end if;
746    end Load_Unit;
747
748    ------------------------
749    -- Make_Instance_Unit --
750    ------------------------
751
752    --  If the unit is an instance, it appears as a package declaration, but
753    --  contains both declaration and body of the instance. The body becomes
754    --  the main unit of the compilation, and the declaration is inserted
755    --  at the end of the unit table. The main unit now has the name of a
756    --  body, which is constructed from the name of the original spec,
757    --  and is attached to the compilation node of the original unit. The
758    --  declaration has been attached to a new compilation unit node, and
759    --  code will have to be generated for it.
760
761    procedure Make_Instance_Unit (N : Node_Id) is
762       Sind : constant Source_File_Index := Source_Index (Main_Unit);
763    begin
764       Units.Increment_Last;
765       Units.Table (Units.Last)               := Units.Table (Main_Unit);
766       Units.Table (Units.Last).Cunit         := Library_Unit (N);
767       Units.Table (Units.Last).Generate_Code := True;
768       Units.Table (Main_Unit).Cunit          := N;
769       Units.Table (Main_Unit).Unit_Name      :=
770         Get_Body_Name (Unit_Name (Get_Cunit_Unit_Number (Library_Unit (N))));
771       Units.Table (Main_Unit).Version        := Source_Checksum (Sind);
772    end Make_Instance_Unit;
773
774    ------------------------
775    -- Spec_Is_Irrelevant --
776    ------------------------
777
778    function Spec_Is_Irrelevant
779      (Spec_Unit : Unit_Number_Type;
780       Body_Unit : Unit_Number_Type) return Boolean
781    is
782       Sunit : constant Node_Id := Cunit (Spec_Unit);
783       Bunit : constant Node_Id := Cunit (Body_Unit);
784
785    begin
786       --  The spec is irrelevant if the body is a subprogram body, and the
787       --  spec is other than a subprogram spec or generic subprogram spec.
788       --  Note that the names must be the same, we don't need to check that,
789       --  because we already know that from the fact that the file names are
790       --  the same.
791
792       return
793          Nkind (Unit (Bunit)) = N_Subprogram_Body
794            and then Nkind (Unit (Sunit)) /= N_Subprogram_Declaration
795            and then Nkind (Unit (Sunit)) /= N_Generic_Subprogram_Declaration;
796    end Spec_Is_Irrelevant;
797
798    --------------------
799    -- Version_Update --
800    --------------------
801
802    procedure Version_Update (U : Node_Id; From : Node_Id) is
803       Unum  : constant Unit_Number_Type := Get_Cunit_Unit_Number (U);
804       Fnum  : constant Unit_Number_Type := Get_Cunit_Unit_Number (From);
805    begin
806       if Source_Index (Fnum) /= No_Source_File then
807          Units.Table (Unum).Version :=
808            Units.Table (Unum).Version
809              xor
810               Source_Checksum (Source_Index (Fnum));
811       end if;
812    end Version_Update;
813
814    ----------------------------
815    -- Write_Dependency_Chain --
816    ----------------------------
817
818    procedure Write_Dependency_Chain is
819    begin
820       --  The dependency chain is only written if it is at least two entries
821       --  deep, otherwise it is trivial (the main unit depending on a unit
822       --  that it obviously directly depends on).
823
824       if Load_Stack.Last - 1 > Load_Stack.First then
825          for U in Load_Stack.First .. Load_Stack.Last - 1 loop
826             Error_Msg_Unit_1 :=
827               Unit_Name (Load_Stack.Table (U).Unit_Number);
828             Error_Msg_Unit_2 :=
829               Unit_Name (Load_Stack.Table (U + 1).Unit_Number);
830             Error_Msg ("$ depends on $!", Load_Msg_Sloc);
831          end loop;
832       end if;
833    end Write_Dependency_Chain;
834
835 end Lib.Load;