OSDN Git Service

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