2014-12-12 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ada / lib-load.adb
blob34b20cc780b73c64b110f2b1d6aab3f9d0c9aa2f
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-2014, 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 ------------------------------------------------------------------------------
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;
48 package body Lib.Load is
50 -----------------------
51 -- Local Subprograms --
52 -----------------------
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.
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.
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.
72 ------------------------------
73 -- Change_Main_Unit_To_Spec --
74 ------------------------------
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;
81 begin
82 -- Get name of unit body
84 Get_Name_String (U.Unit_File_Name);
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 ???
90 -- Change .adb to .ads
92 if Name_Len >= 5
93 and then Name_Buffer (Name_Len - 3 .. Name_Len) = ".adb"
94 then
95 Name_Buffer (Name_Len) := 's';
97 -- Change .2.ada to .1.ada (Rational convention)
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';
104 -- Change .ada to _.ada (DEC convention)
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;
112 -- No match, don't make the change
114 else
115 return;
116 end if;
118 -- Try loading the spec
120 N := Name_Find;
121 X := Load_Source_File (N);
123 -- No change if we did not find the spec
125 if X = No_Source_File then
126 return;
127 end if;
129 -- Otherwise modify Main_Unit entry to point to spec
131 U.Unit_File_Name := N;
132 U.Source_Index := X;
133 end Change_Main_Unit_To_Spec;
135 -------------------------------
136 -- Create_Dummy_Package_Unit --
137 -------------------------------
139 function Create_Dummy_Package_Unit
140 (With_Node : Node_Id;
141 Spec_Name : Unit_Name_Type) return Unit_Number_Type
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;
150 begin
151 -- The created dummy package unit does not come from source
153 Set_Comes_From_Source_Default (False);
155 -- Normal package
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);
164 -- Child package
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);
175 Set_Is_Child_Unit (Cunit_Entity);
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;
183 Set_Scope (Cunit_Entity, Standard_Standard);
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));
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
203 Set_Analyzed (Cunit);
205 Units.Increment_Last;
206 Unum := Units.Last;
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_RACW => False,
218 Filler => False,
219 Ident_String => Empty,
220 Loading => False,
221 Main_Priority => Default_Main_Priority,
222 Main_CPU => Default_Main_CPU,
223 Munit_Index => 0,
224 No_Elab_Code_All => False,
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 SPARK_Mode_Pragma => Empty);
233 Set_Comes_From_Source_Default (Save_CS);
234 Set_Error_Posted (Cunit_Entity);
235 Set_Error_Posted (Cunit);
236 return Unum;
237 end Create_Dummy_Package_Unit;
239 -----------------------------
240 -- From_Limited_With_Chain --
241 -----------------------------
243 function From_Limited_With_Chain return Boolean is
244 Curr_Num : constant Unit_Number_Type :=
245 Load_Stack.Table (Load_Stack.Last).Unit_Number;
247 begin
248 -- True if the current load operation is through a limited_with clause
249 -- and we are not within a loop of regular with_clauses.
251 for U in reverse Load_Stack.First .. Load_Stack.Last - 1 loop
252 if Load_Stack.Table (U).Unit_Number = Curr_Num then
253 return False;
255 elsif Present (Load_Stack.Table (U).With_Node)
256 and then Limited_Present (Load_Stack.Table (U).With_Node)
257 then
258 return True;
259 end if;
260 end loop;
262 return False;
263 end From_Limited_With_Chain;
265 ----------------
266 -- Initialize --
267 ----------------
269 procedure Initialize is
270 begin
271 Units.Init;
272 Load_Stack.Init;
273 end Initialize;
275 ------------------------
276 -- Initialize_Version --
277 ------------------------
279 procedure Initialize_Version (U : Unit_Number_Type) is
280 begin
281 Units.Table (U).Version := Source_Checksum (Source_Index (U));
282 end Initialize_Version;
284 ----------------------
285 -- Load_Main_Source --
286 ----------------------
288 procedure Load_Main_Source is
289 Fname : File_Name_Type;
290 Version : Word := 0;
292 begin
293 Load_Stack.Increment_Last;
294 Load_Stack.Table (Load_Stack.Last) := (Main_Unit, Empty);
296 -- Initialize unit table entry for Main_Unit. Note that we don't know
297 -- the unit name yet, that gets filled in when the parser parses the
298 -- main unit, at which time a check is made that it matches the main
299 -- file name, and then the Unit_Name field is set. The Cunit and
300 -- Cunit_Entity fields also get filled in later by the parser.
302 Units.Increment_Last;
303 Fname := Next_Main_Source;
305 Units.Table (Main_Unit).Unit_File_Name := Fname;
307 if Fname /= No_File then
308 Main_Source_File := Load_Source_File (Fname);
309 Current_Error_Source_File := Main_Source_File;
311 if Main_Source_File /= No_Source_File then
312 Version := Source_Checksum (Main_Source_File);
313 end if;
315 Units.Table (Main_Unit) := (
316 Cunit => Empty,
317 Cunit_Entity => Empty,
318 Dependency_Num => 0,
319 Dynamic_Elab => False,
320 Error_Location => No_Location,
321 Expected_Unit => No_Unit_Name,
322 Fatal_Error => False,
323 Generate_Code => False,
324 Has_RACW => False,
325 Filler => 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 No_Elab_Code_All => False,
332 Serial_Number => 0,
333 Source_Index => Main_Source_File,
334 Unit_File_Name => Fname,
335 Unit_Name => No_Unit_Name,
336 Version => Version,
337 OA_Setting => 'O',
338 SPARK_Mode_Pragma => Empty);
339 end if;
340 end Load_Main_Source;
342 ---------------
343 -- Load_Unit --
344 ---------------
346 function Load_Unit
347 (Load_Name : Unit_Name_Type;
348 Required : Boolean;
349 Error_Node : Node_Id;
350 Subunit : Boolean;
351 Corr_Body : Unit_Number_Type := No_Unit;
352 Renamings : Boolean := False;
353 With_Node : Node_Id := Empty;
354 PMES : Boolean := False) return Unit_Number_Type
356 Calling_Unit : Unit_Number_Type;
357 Uname_Actual : Unit_Name_Type;
358 Unum : Unit_Number_Type;
359 Unump : Unit_Number_Type;
360 Fname : File_Name_Type;
361 Src_Ind : Source_File_Index;
362 Save_PMES : constant Boolean := Parsing_Main_Extended_Source;
364 Save_Cunit_Restrictions : constant Save_Cunit_Boolean_Restrictions :=
365 Cunit_Boolean_Restrictions_Save;
366 -- Save current restrictions for restore at end
368 begin
369 Parsing_Main_Extended_Source := PMES;
371 -- Initialize restrictions to config restrictions for unit to load if
372 -- it is part of the main extended source, otherwise reset them.
374 -- Note: it's a bit odd but PMES is False for subunits, which is why
375 -- we have the OR here. Should be investigated some time???
377 if PMES or Subunit then
378 Restore_Config_Cunit_Boolean_Restrictions;
379 else
380 Reset_Cunit_Boolean_Restrictions;
381 end if;
383 -- If renamings are allowed and we have a child unit name, then we
384 -- must first load the parent to deal with finding the real name.
385 -- Retain the with_clause that names the child, so that if it is
386 -- limited, the parent is loaded under the same condition.
388 if Renamings and then Is_Child_Name (Load_Name) then
389 Unump :=
390 Load_Unit
391 (Load_Name => Get_Parent_Spec_Name (Load_Name),
392 Required => Required,
393 Subunit => False,
394 Renamings => True,
395 Error_Node => Error_Node,
396 With_Node => With_Node);
398 if Unump = No_Unit then
399 Parsing_Main_Extended_Source := Save_PMES;
400 return No_Unit;
401 end if;
403 -- If parent is a renaming, then we use the renamed package as
404 -- the actual parent for the subsequent load operation.
406 if Nkind (Unit (Cunit (Unump))) = N_Package_Renaming_Declaration then
407 Uname_Actual :=
408 New_Child
409 (Load_Name, Get_Unit_Name (Name (Unit (Cunit (Unump)))));
411 -- If the load is for a with_clause, for visibility purposes both
412 -- the renamed entity and renaming one must be available in the
413 -- current unit: the renamed one in order to retrieve the child
414 -- unit, and the original one because it may be used as a prefix
415 -- in the body of the current unit. We add an explicit with_clause
416 -- for the original parent so that the renaming declaration is
417 -- properly loaded and analyzed.
419 if Present (With_Node) then
420 Insert_After (With_Node,
421 Make_With_Clause (Sloc (With_Node),
422 Name => Copy_Separate_Tree (Prefix (Name (With_Node)))));
423 end if;
425 -- Save the renaming entity, to establish its visibility when
426 -- installing the context. The implicit with is on this entity,
427 -- not on the package it renames. This is somewhat redundant given
428 -- the with_clause just created, but it simplifies subsequent
429 -- expansion of the current with_clause. Optimizable ???
431 if Nkind (Error_Node) = N_With_Clause
432 and then Nkind (Name (Error_Node)) = N_Selected_Component
433 then
434 declare
435 Par : Node_Id := Name (Error_Node);
437 begin
438 while Nkind (Par) = N_Selected_Component
439 and then Chars (Selector_Name (Par)) /=
440 Chars (Cunit_Entity (Unump))
441 loop
442 Par := Prefix (Par);
443 end loop;
445 -- Case of some intermediate parent is a renaming
447 if Nkind (Par) = N_Selected_Component then
448 Set_Entity (Selector_Name (Par), Cunit_Entity (Unump));
450 -- Case where the ultimate parent is a renaming
452 else
453 Set_Entity (Par, Cunit_Entity (Unump));
454 end if;
455 end;
456 end if;
458 -- If the parent is not a renaming, then get its name (this may
459 -- be different from the parent spec name obtained above because
460 -- of renamings higher up in the hierarchy).
462 else
463 Uname_Actual := New_Child (Load_Name, Unit_Name (Unump));
464 end if;
466 -- Here if unit to be loaded is not a child unit
468 else
469 Uname_Actual := Load_Name;
470 end if;
472 Fname := Get_File_Name (Uname_Actual, Subunit);
474 if Debug_Flag_L then
475 Write_Eol;
476 Write_Str ("*** Load request for unit: ");
477 Write_Unit_Name (Load_Name);
479 if Required then
480 Write_Str (" (Required = True)");
481 else
482 Write_Str (" (Required = False)");
483 end if;
485 Write_Eol;
487 if Uname_Actual /= Load_Name then
488 Write_Str ("*** Actual unit loaded: ");
489 Write_Unit_Name (Uname_Actual);
490 end if;
491 end if;
493 -- Capture error location if it is for the main unit. The idea is to
494 -- post errors on the main unit location, not the most recent unit.
495 -- Note: Unit_Name (Main_Unit) is not set if we are parsing gnat.adc.
497 if Present (Error_Node)
498 and then Unit_Name (Main_Unit) /= No_Unit_Name
499 then
500 -- It seems like In_Extended_Main_Source_Unit (Error_Node) would
501 -- do the trick here, but that's wrong, it is much too early to
502 -- call this routine. We are still in the parser, and the required
503 -- semantic information is not established yet. So we base the
504 -- judgment on unit names.
506 Get_External_Unit_Name_String (Unit_Name (Main_Unit));
508 declare
509 Main_Unit_Name : constant String := Name_Buffer (1 .. Name_Len);
511 begin
512 Get_External_Unit_Name_String
513 (Unit_Name (Get_Source_Unit (Error_Node)));
515 -- If the two names are identical, then for sure we are part
516 -- of the extended main unit
518 if Main_Unit_Name = Name_Buffer (1 .. Name_Len) then
519 Load_Msg_Sloc := Sloc (Error_Node);
521 -- If the load is called from a with_type clause, the error
522 -- node is correct.
524 -- Otherwise, check for the subunit case, and if so, consider
525 -- we have a match if one name is a prefix of the other name.
527 else
528 if Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
529 or else
530 Nkind (Unit (Cunit (Get_Source_Unit (Error_Node)))) =
531 N_Subunit
532 then
533 Name_Len := Integer'Min (Name_Len, Main_Unit_Name'Length);
535 if Name_Buffer (1 .. Name_Len)
537 Main_Unit_Name (1 .. Name_Len)
538 then
539 Load_Msg_Sloc := Sloc (Error_Node);
540 end if;
541 end if;
542 end if;
543 end;
544 end if;
546 -- If we are generating error messages, then capture calling unit
548 if Present (Error_Node) then
549 Calling_Unit := Get_Source_Unit (Error_Node);
550 else
551 Calling_Unit := No_Unit;
552 end if;
554 -- See if we already have an entry for this unit
556 Unum := Main_Unit;
557 while Unum <= Units.Last loop
558 exit when Uname_Actual = Units.Table (Unum).Unit_Name;
559 Unum := Unum + 1;
560 end loop;
562 -- Whether or not the entry was found, Unum is now the right value,
563 -- since it is one more than Units.Last (i.e. the index of the new
564 -- entry we will create) in the not found case.
566 -- A special check is necessary in the unit not found case. If the unit
567 -- is not found, but the file in which it lives has already been loaded,
568 -- then we have the problem that the file does not contain the unit that
569 -- is needed. We simply treat this as a file not found condition.
571 -- We skip this test in multiple unit per file mode since in this
572 -- case we can have multiple units from the same source file.
574 if Unum > Units.Last and then Get_Unit_Index (Uname_Actual) = 0 then
575 for J in Units.First .. Units.Last loop
576 if Fname = Units.Table (J).Unit_File_Name then
577 if Debug_Flag_L then
578 Write_Str (" file does not contain unit, Unit_Number = ");
579 Write_Int (Int (Unum));
580 Write_Eol;
581 Write_Eol;
582 end if;
584 if Present (Error_Node) then
585 if Is_Predefined_File_Name (Fname) then
586 Error_Msg_Unit_1 := Uname_Actual;
587 Error_Msg
588 ("$$ is not a language defined unit", Load_Msg_Sloc);
589 else
590 Error_Msg_File_1 := Fname;
591 Error_Msg_Unit_1 := Uname_Actual;
592 Error_Msg ("File{ does not contain unit$", Load_Msg_Sloc);
593 end if;
595 Write_Dependency_Chain;
596 Unum := No_Unit;
597 goto Done;
599 else
600 Unum := No_Unit;
601 goto Done;
602 end if;
603 end if;
604 end loop;
605 end if;
607 -- If we are proceeding with load, then make load stack entry,
608 -- and indicate the kind of with_clause responsible for the load.
610 Load_Stack.Increment_Last;
611 Load_Stack.Table (Load_Stack.Last) := (Unum, With_Node);
613 -- Case of entry already in table
615 if Unum <= Units.Last then
617 -- Here is where we check for a circular dependency, which is
618 -- an attempt to load a unit which is currently in the process
619 -- of being loaded. We do *not* care about a circular chain that
620 -- leads back to a body, because this kind of circular dependence
621 -- legitimately occurs (e.g. two package bodies that contain
622 -- inlined subprogram referenced by the other).
624 -- Ada 2005 (AI-50217): We also ignore limited_with clauses, because
625 -- their purpose is precisely to create legal circular structures.
627 if Loading (Unum)
628 and then (Is_Spec_Name (Units.Table (Unum).Unit_Name)
629 or else Acts_As_Spec (Units.Table (Unum).Cunit))
630 and then (Nkind (Error_Node) /= N_With_Clause
631 or else not Limited_Present (Error_Node))
632 and then not From_Limited_With_Chain
633 then
634 if Debug_Flag_L then
635 Write_Str (" circular dependency encountered");
636 Write_Eol;
637 end if;
639 if Present (Error_Node) then
640 Error_Msg ("circular unit dependency", Load_Msg_Sloc);
641 Write_Dependency_Chain;
642 else
643 Load_Stack.Decrement_Last;
644 end if;
646 Unum := No_Unit;
647 goto Done;
648 end if;
650 if Debug_Flag_L then
651 Write_Str (" unit already in file table, Unit_Number = ");
652 Write_Int (Int (Unum));
653 Write_Eol;
654 end if;
656 Load_Stack.Decrement_Last;
657 goto Done;
659 -- Unit is not already in table, so try to open the file
661 else
662 if Debug_Flag_L then
663 Write_Str (" attempt unit load, Unit_Number = ");
664 Write_Int (Int (Unum));
665 Write_Eol;
666 end if;
668 Src_Ind := Load_Source_File (Fname);
670 -- Make a partial entry in the file table, used even in the file not
671 -- found case to print the dependency chain including the last entry
673 Units.Increment_Last;
674 Units.Table (Unum).Unit_Name := Uname_Actual;
676 -- File was found
678 if Src_Ind /= No_Source_File then
679 Units.Table (Unum) := (
680 Cunit => Empty,
681 Cunit_Entity => Empty,
682 Dependency_Num => 0,
683 Dynamic_Elab => False,
684 Error_Location => Sloc (Error_Node),
685 Expected_Unit => Uname_Actual,
686 Fatal_Error => False,
687 Generate_Code => False,
688 Has_RACW => False,
689 Filler => False,
690 Ident_String => Empty,
691 Loading => True,
692 Main_Priority => Default_Main_Priority,
693 Main_CPU => Default_Main_CPU,
694 Munit_Index => 0,
695 No_Elab_Code_All => False,
696 Serial_Number => 0,
697 Source_Index => Src_Ind,
698 Unit_File_Name => Fname,
699 Unit_Name => Uname_Actual,
700 Version => Source_Checksum (Src_Ind),
701 OA_Setting => 'O',
702 SPARK_Mode_Pragma => Empty);
704 -- Parse the new unit
706 declare
707 Save_Index : constant Nat := Multiple_Unit_Index;
708 Save_PMES : constant Boolean := Parsing_Main_Extended_Source;
710 begin
711 Multiple_Unit_Index := Get_Unit_Index (Uname_Actual);
712 Units.Table (Unum).Munit_Index := Multiple_Unit_Index;
713 Initialize_Scanner (Unum, Source_Index (Unum));
715 if Calling_Unit = Main_Unit and then Subunit then
716 Parsing_Main_Extended_Source := True;
717 end if;
719 Discard_List (Par (Configuration_Pragmas => False));
721 Parsing_Main_Extended_Source := Save_PMES;
723 Multiple_Unit_Index := Save_Index;
724 Set_Loading (Unum, False);
725 end;
727 -- If spec is irrelevant, then post errors and quit
729 if Corr_Body /= No_Unit
730 and then Spec_Is_Irrelevant (Unum, Corr_Body)
731 then
732 Error_Msg_File_1 := Unit_File_Name (Corr_Body);
733 Error_Msg
734 ("cannot compile subprogram in file {!", Load_Msg_Sloc);
735 Error_Msg_File_1 := Unit_File_Name (Unum);
736 Error_Msg
737 ("\incorrect spec in file { must be removed first!",
738 Load_Msg_Sloc);
739 Unum := No_Unit;
740 goto Done;
741 end if;
743 -- If loaded unit had a fatal error, then caller inherits it
745 if Units.Table (Unum).Fatal_Error
746 and then Present (Error_Node)
747 then
748 Units.Table (Calling_Unit).Fatal_Error := True;
749 end if;
751 -- Remove load stack entry and return the entry in the file table
753 Load_Stack.Decrement_Last;
755 -- All done, return unit number
757 goto Done;
759 -- Case of file not found
761 else
762 if Debug_Flag_L then
763 Write_Str (" file was not found, load failed");
764 Write_Eol;
765 end if;
767 -- Generate message if unit required
769 if Required and then Present (Error_Node) then
770 if Is_Predefined_File_Name (Fname) then
772 -- This is a predefined library unit which is not present
773 -- in the run time. If a predefined unit is not available
774 -- it may very likely be the case that there is also pragma
775 -- Restriction forbidding its usage. This is typically the
776 -- case when building a configurable run time, where the
777 -- usage of certain run-time units is restricted by means
778 -- of both the corresponding pragma Restriction (such as
779 -- No_Calendar), and by not including the unit. Hence, we
780 -- check whether this predefined unit is forbidden, so that
781 -- the message about the restriction violation is generated,
782 -- if needed.
784 Check_Restricted_Unit (Load_Name, Error_Node);
786 Error_Msg_Unit_1 := Uname_Actual;
787 Error_Msg -- CODEFIX
788 ("$$ is not a predefined library unit", Load_Msg_Sloc);
790 else
791 Error_Msg_File_1 := Fname;
792 Error_Msg ("file{ not found", Load_Msg_Sloc);
793 end if;
795 Write_Dependency_Chain;
797 -- Remove unit from stack, to avoid cascaded errors on
798 -- subsequent missing files.
800 Load_Stack.Decrement_Last;
801 Units.Decrement_Last;
803 -- If unit not required, remove load stack entry and the junk
804 -- file table entry, and return No_Unit to indicate not found,
806 else
807 Load_Stack.Decrement_Last;
808 Units.Decrement_Last;
809 end if;
811 Unum := No_Unit;
812 goto Done;
813 end if;
814 end if;
816 -- Here to exit, with result in Unum
818 <<Done>>
819 Parsing_Main_Extended_Source := Save_PMES;
820 Cunit_Boolean_Restrictions_Restore (Save_Cunit_Restrictions);
821 return Unum;
822 end Load_Unit;
824 --------------------------
825 -- Make_Child_Decl_Unit --
826 --------------------------
828 procedure Make_Child_Decl_Unit (N : Node_Id) is
829 Unit_Decl : constant Node_Id := Library_Unit (N);
831 begin
832 Units.Increment_Last;
833 Units.Table (Units.Last) := Units.Table (Get_Cunit_Unit_Number (N));
834 Units.Table (Units.Last).Unit_Name :=
835 Get_Spec_Name (Unit_Name (Get_Cunit_Unit_Number (N)));
836 Units.Table (Units.Last).Cunit := Unit_Decl;
837 Units.Table (Units.Last).Cunit_Entity :=
838 Defining_Identifier
839 (Defining_Unit_Name (Specification (Unit (Unit_Decl))));
841 -- The library unit created for of a child subprogram unit plays no
842 -- role in code generation and binding, so label it accordingly.
844 Units.Table (Units.Last).Generate_Code := False;
845 Set_Has_No_Elaboration_Code (Unit_Decl);
846 end Make_Child_Decl_Unit;
848 ------------------------
849 -- Make_Instance_Unit --
850 ------------------------
852 -- If the unit is an instance, it appears as a package declaration, but
853 -- contains both declaration and body of the instance. The body becomes
854 -- the main unit of the compilation, and the declaration is inserted
855 -- at the end of the unit table. The main unit now has the name of a
856 -- body, which is constructed from the name of the original spec,
857 -- and is attached to the compilation node of the original unit. The
858 -- declaration has been attached to a new compilation unit node, and
859 -- code will have to be generated for it.
861 procedure Make_Instance_Unit (N : Node_Id; In_Main : Boolean) is
862 Sind : constant Source_File_Index := Source_Index (Main_Unit);
864 begin
865 Units.Increment_Last;
867 if In_Main then
868 Units.Table (Units.Last) := Units.Table (Main_Unit);
869 Units.Table (Units.Last).Cunit := Library_Unit (N);
870 Units.Table (Units.Last).Generate_Code := True;
871 Units.Table (Main_Unit).Cunit := N;
872 Units.Table (Main_Unit).Unit_Name :=
873 Get_Body_Name
874 (Unit_Name (Get_Cunit_Unit_Number (Library_Unit (N))));
875 Units.Table (Main_Unit).Version := Source_Checksum (Sind);
877 else
878 -- Duplicate information from instance unit, for the body. The unit
879 -- node N has been rewritten as a body, but it was placed in the
880 -- units table when first loaded as a declaration.
882 Units.Table (Units.Last) := Units.Table (Get_Cunit_Unit_Number (N));
883 Units.Table (Units.Last).Cunit := Library_Unit (N);
884 end if;
885 end Make_Instance_Unit;
887 ------------------------
888 -- Spec_Is_Irrelevant --
889 ------------------------
891 function Spec_Is_Irrelevant
892 (Spec_Unit : Unit_Number_Type;
893 Body_Unit : Unit_Number_Type) return Boolean
895 Sunit : constant Node_Id := Cunit (Spec_Unit);
896 Bunit : constant Node_Id := Cunit (Body_Unit);
898 begin
899 -- The spec is irrelevant if the body is a subprogram body, and the spec
900 -- is other than a subprogram spec or generic subprogram spec. Note that
901 -- the names must be the same, we don't need to check that, because we
902 -- already know that from the fact that the file names are the same.
904 return
905 Nkind (Unit (Bunit)) = N_Subprogram_Body
906 and then Nkind (Unit (Sunit)) /= N_Subprogram_Declaration
907 and then Nkind (Unit (Sunit)) /= N_Generic_Subprogram_Declaration;
908 end Spec_Is_Irrelevant;
910 --------------------
911 -- Version_Update --
912 --------------------
914 procedure Version_Update (U : Node_Id; From : Node_Id) is
915 Unum : constant Unit_Number_Type := Get_Cunit_Unit_Number (U);
916 Fnum : constant Unit_Number_Type := Get_Cunit_Unit_Number (From);
917 begin
918 if Source_Index (Fnum) /= No_Source_File then
919 Units.Table (Unum).Version :=
920 Units.Table (Unum).Version
922 Source_Checksum (Source_Index (Fnum));
923 end if;
924 end Version_Update;
926 ----------------------------
927 -- Write_Dependency_Chain --
928 ----------------------------
930 procedure Write_Dependency_Chain is
931 begin
932 -- The dependency chain is only written if it is at least two entries
933 -- deep, otherwise it is trivial (the main unit depending on a unit
934 -- that it obviously directly depends on).
936 if Load_Stack.Last - 1 > Load_Stack.First then
937 for U in Load_Stack.First .. Load_Stack.Last - 1 loop
938 Error_Msg_Unit_1 :=
939 Unit_Name (Load_Stack.Table (U).Unit_Number);
940 Error_Msg_Unit_2 :=
941 Unit_Name (Load_Stack.Table (U + 1).Unit_Number);
942 Error_Msg ("$ depends on $!", Load_Msg_Sloc);
943 end loop;
944 end if;
945 end Write_Dependency_Chain;
947 end Lib.Load;