OSDN Git Service

2007-04-20 Ed Schonberg <schonberg@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 (Lim : Boolean) 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 (Lim : Boolean) return Boolean is
240    begin
241       --  True if the current load operation is through a limited_with clause
242
243       if Lim then
244          return True;
245
246       --  Examine the Load_Stack to locate any previous Limited_with clause
247
248       elsif Load_Stack.Last - 1 > Load_Stack.First then
249          for U in Load_Stack.First .. Load_Stack.Last - 1 loop
250             if Load_Stack.Table (U).From_Limited_With then
251                return True;
252             end if;
253          end loop;
254       end if;
255
256       return False;
257    end From_Limited_With_Chain;
258
259    ----------------
260    -- Initialize --
261    ----------------
262
263    procedure Initialize is
264    begin
265       Units.Init;
266       Load_Stack.Init;
267    end Initialize;
268
269    ------------------------
270    -- Initialize_Version --
271    ------------------------
272
273    procedure Initialize_Version (U : Unit_Number_Type) is
274    begin
275       Units.Table (U).Version := Source_Checksum (Source_Index (U));
276    end Initialize_Version;
277
278    ----------------------
279    -- Load_Main_Source --
280    ----------------------
281
282    procedure Load_Main_Source is
283       Fname   : File_Name_Type;
284       Version : Word := 0;
285
286    begin
287       Load_Stack.Increment_Last;
288       Load_Stack.Table (Load_Stack.Last) := (Main_Unit, False);
289
290       --  Initialize unit table entry for Main_Unit. Note that we don't know
291       --  the unit name yet, that gets filled in when the parser parses the
292       --  main unit, at which time a check is made that it matches the main
293       --  file name, and then the Unit_Name field is set. The Cunit and
294       --  Cunit_Entity fields also get filled in later by the parser.
295
296       Units.Increment_Last;
297       Fname := Next_Main_Source;
298
299       Units.Table (Main_Unit).Unit_File_Name := Fname;
300
301       if Fname /= No_File then
302          Main_Source_File := Load_Source_File (Fname);
303          Current_Error_Source_File := Main_Source_File;
304
305          if Main_Source_File /= No_Source_File then
306             Version := Source_Checksum (Main_Source_File);
307          end if;
308
309          Units.Table (Main_Unit) := (
310            Cunit           => Empty,
311            Cunit_Entity    => Empty,
312            Dependency_Num  => 0,
313            Dynamic_Elab    => False,
314            Error_Location  => No_Location,
315            Expected_Unit   => No_Unit_Name,
316            Fatal_Error     => False,
317            Generate_Code   => False,
318            Has_RACW        => False,
319            Ident_String    => Empty,
320            Loading         => True,
321            Main_Priority   => Default_Main_Priority,
322            Munit_Index     => 0,
323            Serial_Number   => 0,
324            Source_Index    => Main_Source_File,
325            Unit_File_Name  => Fname,
326            Unit_Name       => No_Unit_Name,
327            Version         => Version);
328       end if;
329    end Load_Main_Source;
330
331    ---------------
332    -- Load_Unit --
333    ---------------
334
335    function Load_Unit
336      (Load_Name         : Unit_Name_Type;
337       Required          : Boolean;
338       Error_Node        : Node_Id;
339       Subunit           : Boolean;
340       Corr_Body         : Unit_Number_Type := No_Unit;
341       Renamings         : Boolean          := False;
342       From_Limited_With : Boolean          := False) return Unit_Number_Type
343    is
344       Calling_Unit : Unit_Number_Type;
345       Uname_Actual : Unit_Name_Type;
346       Unum         : Unit_Number_Type;
347       Unump        : Unit_Number_Type;
348       Fname        : File_Name_Type;
349       Src_Ind      : Source_File_Index;
350
351    --  Start of processing for Load_Unit
352
353    begin
354       --  If renamings are allowed and we have a child unit name, then we
355       --  must first load the parent to deal with finding the real name.
356
357       if Renamings and then Is_Child_Name (Load_Name) then
358          Unump :=
359            Load_Unit
360              (Load_Name  => Get_Parent_Spec_Name (Load_Name),
361               Required   => Required,
362               Subunit    => False,
363               Renamings  => True,
364               Error_Node => Error_Node);
365
366          if Unump = No_Unit then
367             return No_Unit;
368          end if;
369
370          --  If parent is a renaming, then we use the renamed package as
371          --  the actual parent for the subsequent load operation.
372
373          if Nkind (Unit (Cunit (Unump))) = N_Package_Renaming_Declaration then
374             Uname_Actual :=
375               New_Child
376                 (Load_Name, Get_Unit_Name (Name (Unit (Cunit (Unump)))));
377
378             --  Save the renaming entity, to establish its visibility when
379             --  installing the context. The implicit with is on this entity,
380             --  not on the package it renames.
381
382             if Nkind (Error_Node) = N_With_Clause
383               and then Nkind (Name (Error_Node)) = N_Selected_Component
384             then
385                declare
386                   Par : Node_Id := Name (Error_Node);
387
388                begin
389                   while Nkind (Par) = N_Selected_Component
390                     and then Chars (Selector_Name (Par)) /=
391                       Chars (Cunit_Entity (Unump))
392                   loop
393                      Par := Prefix (Par);
394                   end loop;
395
396                   --  Case of some intermediate parent is a renaming
397
398                   if Nkind (Par) = N_Selected_Component then
399                      Set_Entity (Selector_Name (Par), Cunit_Entity (Unump));
400
401                   --  Case where the ultimate parent is a renaming
402
403                   else
404                      Set_Entity (Par, Cunit_Entity (Unump));
405                   end if;
406                end;
407             end if;
408
409          --  If the parent is not a renaming, then get its name (this may
410          --  be different from the parent spec name obtained above because
411          --  of renamings higher up in the hierarchy).
412
413          else
414             Uname_Actual := New_Child (Load_Name, Unit_Name (Unump));
415          end if;
416
417       --  Here if unit to be loaded is not a child unit
418
419       else
420          Uname_Actual := Load_Name;
421       end if;
422
423       Fname := Get_File_Name (Uname_Actual, Subunit);
424
425       if Debug_Flag_L then
426          Write_Eol;
427          Write_Str ("*** Load request for unit: ");
428          Write_Unit_Name (Load_Name);
429
430          if Required then
431             Write_Str (" (Required = True)");
432          else
433             Write_Str (" (Required = False)");
434          end if;
435
436          Write_Eol;
437
438          if Uname_Actual /= Load_Name then
439             Write_Str ("*** Actual unit loaded: ");
440             Write_Unit_Name (Uname_Actual);
441          end if;
442       end if;
443
444       --  Capture error location if it is for the main unit. The idea is to
445       --  post errors on the main unit location, not the most recent unit.
446       --  Note: Unit_Name (Main_Unit) is not set if we are parsing gnat.adc.
447
448       if Present (Error_Node)
449         and then Unit_Name (Main_Unit) /= No_Unit_Name
450       then
451          --  It seems like In_Extended_Main_Source_Unit (Error_Node) would
452          --  do the trick here, but that's wrong, it is much too early to
453          --  call this routine. We are still in the parser, and the required
454          --  semantic information is not established yet. So we base the
455          --  judgment on unit names.
456
457          Get_External_Unit_Name_String (Unit_Name (Main_Unit));
458
459          declare
460             Main_Unit_Name : constant String := Name_Buffer (1 .. Name_Len);
461
462          begin
463             Get_External_Unit_Name_String
464               (Unit_Name (Get_Source_Unit (Error_Node)));
465
466             --  If the two names are identical, then for sure we are part
467             --  of the extended main unit
468
469             if Main_Unit_Name = Name_Buffer (1 .. Name_Len) then
470                Load_Msg_Sloc := Sloc (Error_Node);
471
472             --  If the load is called from a with_type clause, the error
473             --  node is correct.
474
475             --  Otherwise, check for the subunit case, and if so, consider
476             --  we have a match if one name is a prefix of the other name.
477
478             else
479                if Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
480                     or else
481                   Nkind (Unit (Cunit (Get_Source_Unit (Error_Node)))) =
482                                                                 N_Subunit
483                then
484                   Name_Len := Integer'Min (Name_Len, Main_Unit_Name'Length);
485
486                   if Name_Buffer (1 .. Name_Len)
487                         =
488                      Main_Unit_Name (1 .. Name_Len)
489                   then
490                      Load_Msg_Sloc := Sloc (Error_Node);
491                   end if;
492                end if;
493             end if;
494          end;
495       end if;
496
497       --  If we are generating error messages, then capture calling unit
498
499       if Present (Error_Node) then
500          Calling_Unit := Get_Source_Unit (Error_Node);
501       else
502          Calling_Unit := No_Unit;
503       end if;
504
505       --  See if we already have an entry for this unit
506
507       Unum := Main_Unit;
508
509       while Unum <= Units.Last loop
510          exit when Uname_Actual = Units.Table (Unum).Unit_Name;
511          Unum := Unum + 1;
512       end loop;
513
514       --  Whether or not the entry was found, Unum is now the right value,
515       --  since it is one more than Units.Last (i.e. the index of the new
516       --  entry we will create) in the not found case.
517
518       --  A special check is necessary in the unit not found case. If the unit
519       --  is not found, but the file in which it lives has already been loaded,
520       --  then we have the problem that the file does not contain the unit that
521       --  is needed. We simply treat this as a file not found condition.
522
523       --  We skip this test in multiple unit per file mode since in this
524       --  case we can have multiple units from the same source file.
525
526       if Unum > Units.Last and then Get_Unit_Index (Uname_Actual) = 0 then
527          for J in Units.First .. Units.Last loop
528             if Fname = Units.Table (J).Unit_File_Name then
529                if Debug_Flag_L then
530                   Write_Str ("  file does not contain unit, Unit_Number = ");
531                   Write_Int (Int (Unum));
532                   Write_Eol;
533                   Write_Eol;
534                end if;
535
536                if Present (Error_Node) then
537                   if Is_Predefined_File_Name (Fname) then
538                      Error_Msg_Unit_1 := Uname_Actual;
539                      Error_Msg
540                        ("$$ is not a language defined unit", Load_Msg_Sloc);
541                   else
542                      Error_Msg_File_1 := Fname;
543                      Error_Msg_Unit_1 := Uname_Actual;
544                      Error_Msg ("File{ does not contain unit$", Load_Msg_Sloc);
545                   end if;
546
547                   Write_Dependency_Chain;
548                   return No_Unit;
549
550                else
551                   return No_Unit;
552                end if;
553             end if;
554          end loop;
555       end if;
556
557       --  If we are proceeding with load, then make load stack entry,
558       --  and indicate the kind of with_clause responsible for the load.
559
560       Load_Stack.Increment_Last;
561       Load_Stack.Table (Load_Stack.Last) := (Unum, From_Limited_With);
562
563       --  Case of entry already in table
564
565       if Unum <= Units.Last then
566
567          --  Here is where we check for a circular dependency, which is
568          --  an attempt to load a unit which is currently in the process
569          --  of being loaded. We do *not* care about a circular chain that
570          --  leads back to a body, because this kind of circular dependence
571          --  legitimately occurs (e.g. two package bodies that contain
572          --  inlined subprogram referenced by the other).
573
574          --  Ada 2005 (AI-50217): We also ignore limited_with clauses, because
575          --  their purpose is precisely to create legal circular structures.
576
577          if Loading (Unum)
578            and then (Is_Spec_Name (Units.Table (Unum).Unit_Name)
579                        or else Acts_As_Spec (Units.Table (Unum).Cunit))
580            and then (Nkind (Error_Node) /= N_With_Clause
581                        or else not Limited_Present (Error_Node))
582            and then not From_Limited_With_Chain (From_Limited_With)
583          then
584             if Debug_Flag_L then
585                Write_Str ("  circular dependency encountered");
586                Write_Eol;
587             end if;
588
589             if Present (Error_Node) then
590                Error_Msg ("circular unit dependency", Load_Msg_Sloc);
591                Write_Dependency_Chain;
592             else
593                Load_Stack.Decrement_Last;
594             end if;
595
596             return No_Unit;
597          end if;
598
599          if Debug_Flag_L then
600             Write_Str ("  unit already in file table, Unit_Number = ");
601             Write_Int (Int (Unum));
602             Write_Eol;
603          end if;
604
605          Load_Stack.Decrement_Last;
606          return Unum;
607
608       --  Unit is not already in table, so try to open the file
609
610       else
611          if Debug_Flag_L then
612             Write_Str ("  attempt unit load, Unit_Number = ");
613             Write_Int (Int (Unum));
614             Write_Eol;
615          end if;
616
617          Src_Ind := Load_Source_File (Fname);
618
619          --  Make a partial entry in the file table, used even in the file not
620          --  found case to print the dependency chain including the last entry
621
622          Units.Increment_Last;
623          Units.Table (Unum).Unit_Name := Uname_Actual;
624
625          --  File was found
626
627          if Src_Ind /= No_Source_File then
628             Units.Table (Unum) := (
629               Cunit           => Empty,
630               Cunit_Entity    => Empty,
631               Dependency_Num  => 0,
632               Dynamic_Elab    => False,
633               Error_Location  => Sloc (Error_Node),
634               Expected_Unit   => Uname_Actual,
635               Fatal_Error     => False,
636               Generate_Code   => False,
637               Has_RACW        => False,
638               Ident_String    => Empty,
639               Loading         => True,
640               Main_Priority   => Default_Main_Priority,
641               Munit_Index     => 0,
642               Serial_Number   => 0,
643               Source_Index    => Src_Ind,
644               Unit_File_Name  => Fname,
645               Unit_Name       => Uname_Actual,
646               Version         => Source_Checksum (Src_Ind));
647
648             --  Parse the new unit
649
650             declare
651                Save_Index : constant Nat := Multiple_Unit_Index;
652             begin
653                Multiple_Unit_Index := Get_Unit_Index (Uname_Actual);
654                Units.Table (Unum).Munit_Index := Multiple_Unit_Index;
655                Initialize_Scanner (Unum, Source_Index (Unum));
656                Discard_List (Par (Configuration_Pragmas => False,
657                                   From_Limited_With     => From_Limited_With));
658                Multiple_Unit_Index := Save_Index;
659                Set_Loading (Unum, False);
660             end;
661
662             --  If spec is irrelevant, then post errors and quit
663
664             if Corr_Body /= No_Unit
665               and then Spec_Is_Irrelevant (Unum, Corr_Body)
666             then
667                Error_Msg_File_1 := Unit_File_Name (Corr_Body);
668                Error_Msg
669                  ("cannot compile subprogram in file {!", Load_Msg_Sloc);
670                Error_Msg_File_1 := Unit_File_Name (Unum);
671                Error_Msg
672                  ("\incorrect spec in file { must be removed first!",
673                   Load_Msg_Sloc);
674                return No_Unit;
675             end if;
676
677             --  If loaded unit had a fatal error, then caller inherits it!
678
679             if Units.Table (Unum).Fatal_Error
680               and then Present (Error_Node)
681             then
682                Units.Table (Calling_Unit).Fatal_Error := True;
683             end if;
684
685             --  Remove load stack entry and return the entry in the file table
686
687             Load_Stack.Decrement_Last;
688             return Unum;
689
690          --  Case of file not found
691
692          else
693             if Debug_Flag_L then
694                Write_Str ("  file was not found, load failed");
695                Write_Eol;
696             end if;
697
698             --  Generate message if unit required
699
700             if Required and then Present (Error_Node) then
701                if Is_Predefined_File_Name (Fname) then
702
703                   --  This is a predefined library unit which is not present
704                   --  in the run time. If a predefined unit is not available
705                   --  it may very likely be the case that there is also pragma
706                   --  Restriction forbidding its usage. This is typically the
707                   --  case when building a configurable run time, where the
708                   --  usage of certain run-time units units is restricted by
709                   --  means of both the corresponding pragma Restriction (such
710                   --  as No_Calendar), and by not including the unit. Hence,
711                   --  we check whether this predefined unit is forbidden, so
712                   --  that the message about the restriction violation is
713                   --  generated, if needed.
714
715                   Check_Restricted_Unit (Load_Name, Error_Node);
716
717                   Error_Msg_Unit_1 := Uname_Actual;
718                   Error_Msg
719                     ("$$ is not a predefined library unit", Load_Msg_Sloc);
720
721                else
722                   Error_Msg_File_1 := Fname;
723                   Error_Msg ("file{ not found", Load_Msg_Sloc);
724                end if;
725
726                Write_Dependency_Chain;
727
728                --  Remove unit from stack, to avoid cascaded errors on
729                --  subsequent missing files.
730
731                Load_Stack.Decrement_Last;
732                Units.Decrement_Last;
733
734             --  If unit not required, remove load stack entry and the junk
735             --  file table entry, and return No_Unit to indicate not found,
736
737             else
738                Load_Stack.Decrement_Last;
739                Units.Decrement_Last;
740             end if;
741
742             return No_Unit;
743          end if;
744       end if;
745    end Load_Unit;
746
747    ------------------------
748    -- Make_Instance_Unit --
749    ------------------------
750
751    --  If the unit is an instance, it appears as a package declaration, but
752    --  contains both declaration and body of the instance. The body becomes
753    --  the main unit of the compilation, and the declaration is inserted
754    --  at the end of the unit table. The main unit now has the name of a
755    --  body, which is constructed from the name of the original spec,
756    --  and is attached to the compilation node of the original unit. The
757    --  declaration has been attached to a new compilation unit node, and
758    --  code will have to be generated for it.
759
760    procedure Make_Instance_Unit (N : Node_Id) is
761       Sind : constant Source_File_Index := Source_Index (Main_Unit);
762    begin
763       Units.Increment_Last;
764       Units.Table (Units.Last)               := Units.Table (Main_Unit);
765       Units.Table (Units.Last).Cunit         := Library_Unit (N);
766       Units.Table (Units.Last).Generate_Code := True;
767       Units.Table (Main_Unit).Cunit          := N;
768       Units.Table (Main_Unit).Unit_Name      :=
769         Get_Body_Name (Unit_Name (Get_Cunit_Unit_Number (Library_Unit (N))));
770       Units.Table (Main_Unit).Version        := Source_Checksum (Sind);
771    end Make_Instance_Unit;
772
773    ------------------------
774    -- Spec_Is_Irrelevant --
775    ------------------------
776
777    function Spec_Is_Irrelevant
778      (Spec_Unit : Unit_Number_Type;
779       Body_Unit : Unit_Number_Type) return Boolean
780    is
781       Sunit : constant Node_Id := Cunit (Spec_Unit);
782       Bunit : constant Node_Id := Cunit (Body_Unit);
783
784    begin
785       --  The spec is irrelevant if the body is a subprogram body, and the
786       --  spec is other than a subprogram spec or generic subprogram spec.
787       --  Note that the names must be the same, we don't need to check that,
788       --  because we already know that from the fact that the file names are
789       --  the same.
790
791       return
792          Nkind (Unit (Bunit)) = N_Subprogram_Body
793            and then Nkind (Unit (Sunit)) /= N_Subprogram_Declaration
794            and then Nkind (Unit (Sunit)) /= N_Generic_Subprogram_Declaration;
795    end Spec_Is_Irrelevant;
796
797    --------------------
798    -- Version_Update --
799    --------------------
800
801    procedure Version_Update (U : Node_Id; From : Node_Id) is
802       Unum  : constant Unit_Number_Type := Get_Cunit_Unit_Number (U);
803       Fnum  : constant Unit_Number_Type := Get_Cunit_Unit_Number (From);
804    begin
805       if Source_Index (Fnum) /= No_Source_File then
806          Units.Table (Unum).Version :=
807            Units.Table (Unum).Version
808              xor
809               Source_Checksum (Source_Index (Fnum));
810       end if;
811    end Version_Update;
812
813    ----------------------------
814    -- Write_Dependency_Chain --
815    ----------------------------
816
817    procedure Write_Dependency_Chain is
818    begin
819       --  The dependency chain is only written if it is at least two entries
820       --  deep, otherwise it is trivial (the main unit depending on a unit
821       --  that it obviously directly depends on).
822
823       if Load_Stack.Last - 1 > Load_Stack.First then
824          for U in Load_Stack.First .. Load_Stack.Last - 1 loop
825             Error_Msg_Unit_1 :=
826               Unit_Name (Load_Stack.Table (U).Unit_Number);
827             Error_Msg_Unit_2 :=
828               Unit_Name (Load_Stack.Table (U + 1).Unit_Number);
829             Error_Msg ("$ depends on $!", Load_Msg_Sloc);
830          end loop;
831       end if;
832    end Write_Dependency_Chain;
833
834 end Lib.Load;