Daily bump.
[official-gcc.git] / gcc / ada / lib-load.adb
blobe05bde164b399fb77adf9f8f49e9275b9cbb43c5
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-2017, 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 Fname : constant File_Name_Type :=
149 Get_File_Name (Spec_Name, Subunit => False);
150 Pre_Name : constant Boolean :=
151 Is_Predefined_File_Name (Fname, Renamings_Included => False);
152 Ren_Name : constant Boolean :=
153 Is_Predefined_Renaming_File_Name (Fname);
154 GNAT_Name : constant Boolean :=
155 Is_GNAT_File_Name (Fname);
156 Save_CS : constant Boolean := Get_Comes_From_Source_Default;
158 begin
159 -- The created dummy package unit does not come from source
161 Set_Comes_From_Source_Default (False);
163 -- Normal package
165 if Nkind (Name (With_Node)) = N_Identifier then
166 Cunit_Entity :=
167 Make_Defining_Identifier (No_Location,
168 Chars => Chars (Name (With_Node)));
169 Du_Name := Cunit_Entity;
170 End_Lab := New_Occurrence_Of (Cunit_Entity, No_Location);
172 -- Child package
174 else
175 Cunit_Entity :=
176 Make_Defining_Identifier (No_Location,
177 Chars => Chars (Selector_Name (Name (With_Node))));
178 Du_Name :=
179 Make_Defining_Program_Unit_Name (No_Location,
180 Name => Copy_Separate_Tree (Prefix (Name (With_Node))),
181 Defining_Identifier => Cunit_Entity);
183 Set_Is_Child_Unit (Cunit_Entity);
185 End_Lab :=
186 Make_Designator (No_Location,
187 Name => Copy_Separate_Tree (Prefix (Name (With_Node))),
188 Identifier => New_Occurrence_Of (Cunit_Entity, No_Location));
189 end if;
191 Set_Scope (Cunit_Entity, Standard_Standard);
193 Cunit :=
194 Make_Compilation_Unit (No_Location,
195 Context_Items => Empty_List,
196 Unit =>
197 Make_Package_Declaration (No_Location,
198 Specification =>
199 Make_Package_Specification (No_Location,
200 Defining_Unit_Name => Du_Name,
201 Visible_Declarations => Empty_List,
202 End_Label => End_Lab)),
203 Aux_Decls_Node =>
204 Make_Compilation_Unit_Aux (No_Location));
206 -- Mark the dummy package as analyzed to prevent analysis of this
207 -- (non-existent) unit in -gnatQ mode because at the moment the
208 -- structure and attributes of this dummy package does not allow
209 -- a normal analysis of this unit
211 Set_Analyzed (Cunit);
213 Units.Increment_Last;
214 Unum := Units.Last;
216 Units.Table (Unum) :=
217 (Cunit => Cunit,
218 Cunit_Entity => Cunit_Entity,
219 Dependency_Num => 0,
220 Dynamic_Elab => False,
221 Error_Location => Sloc (With_Node),
222 Expected_Unit => Spec_Name,
223 Fatal_Error => Error_Detected,
224 Generate_Code => False,
225 Has_RACW => False,
226 Filler => False,
227 Ident_String => Empty,
229 Is_Predefined_Renaming => Ren_Name,
230 Is_Predefined_Unit => Pre_Name or Ren_Name,
231 Is_Internal_Unit => Pre_Name or Ren_Name or GNAT_Name,
232 Filler2 => False,
234 Loading => False,
235 Main_Priority => Default_Main_Priority,
236 Main_CPU => Default_Main_CPU,
237 Munit_Index => 0,
238 No_Elab_Code_All => False,
239 Serial_Number => 0,
240 Source_Index => No_Source_File,
241 Unit_File_Name => Fname,
242 Unit_Name => Spec_Name,
243 Version => 0,
244 OA_Setting => 'O');
246 Set_Comes_From_Source_Default (Save_CS);
247 Set_Error_Posted (Cunit_Entity);
248 Set_Error_Posted (Cunit);
249 return Unum;
250 end Create_Dummy_Package_Unit;
252 -----------------------------
253 -- From_Limited_With_Chain --
254 -----------------------------
256 function From_Limited_With_Chain return Boolean is
257 Curr_Num : constant Unit_Number_Type :=
258 Load_Stack.Table (Load_Stack.Last).Unit_Number;
260 begin
261 -- True if the current load operation is through a limited_with clause
262 -- and we are not within a loop of regular with_clauses.
264 for U in reverse Load_Stack.First .. Load_Stack.Last - 1 loop
265 if Load_Stack.Table (U).Unit_Number = Curr_Num then
266 return False;
268 elsif Present (Load_Stack.Table (U).With_Node)
269 and then Limited_Present (Load_Stack.Table (U).With_Node)
270 then
271 return True;
272 end if;
273 end loop;
275 return False;
276 end From_Limited_With_Chain;
278 ----------------
279 -- Initialize --
280 ----------------
282 procedure Initialize is
283 begin
284 Units.Init;
285 Load_Stack.Init;
286 end Initialize;
288 ------------------------
289 -- Initialize_Version --
290 ------------------------
292 procedure Initialize_Version (U : Unit_Number_Type) is
293 begin
294 Units.Table (U).Version := Source_Checksum (Source_Index (U));
295 end Initialize_Version;
297 ----------------------
298 -- Load_Main_Source --
299 ----------------------
301 procedure Load_Main_Source is
302 Fname : constant File_Name_Type := Next_Main_Source;
303 Pre_Name : constant Boolean :=
304 Is_Predefined_File_Name (Fname, Renamings_Included => False);
305 Ren_Name : constant Boolean :=
306 Is_Predefined_Renaming_File_Name (Fname);
307 GNAT_Name : constant Boolean :=
308 Is_GNAT_File_Name (Fname);
309 Version : Word := 0;
311 begin
312 Load_Stack.Increment_Last;
313 Load_Stack.Table (Load_Stack.Last) := (Main_Unit, Empty);
315 -- Initialize unit table entry for Main_Unit. Note that we don't know
316 -- the unit name yet, that gets filled in when the parser parses the
317 -- main unit, at which time a check is made that it matches the main
318 -- file name, and then the Unit_Name field is set. The Cunit and
319 -- Cunit_Entity fields also get filled in later by the parser.
321 Units.Increment_Last;
323 Units.Table (Main_Unit).Unit_File_Name := Fname;
325 if Fname /= No_File then
326 Main_Source_File := Load_Source_File (Fname);
327 Current_Error_Source_File := Main_Source_File;
329 if Main_Source_File /= No_Source_File then
330 Version := Source_Checksum (Main_Source_File);
331 end if;
333 Units.Table (Main_Unit) :=
334 (Cunit => Empty,
335 Cunit_Entity => Empty,
336 Dependency_Num => 0,
337 Dynamic_Elab => False,
338 Error_Location => No_Location,
339 Expected_Unit => No_Unit_Name,
340 Fatal_Error => None,
341 Generate_Code => False,
342 Has_RACW => False,
343 Filler => False,
344 Ident_String => Empty,
346 Is_Predefined_Renaming => Ren_Name,
347 Is_Predefined_Unit => Pre_Name or Ren_Name,
348 Is_Internal_Unit => Pre_Name or Ren_Name or GNAT_Name,
349 Filler2 => False,
351 Loading => True,
352 Main_Priority => Default_Main_Priority,
353 Main_CPU => Default_Main_CPU,
354 Munit_Index => 0,
355 No_Elab_Code_All => False,
356 Serial_Number => 0,
357 Source_Index => Main_Source_File,
358 Unit_File_Name => Fname,
359 Unit_Name => No_Unit_Name,
360 Version => Version,
361 OA_Setting => 'O');
362 end if;
363 end Load_Main_Source;
365 ---------------
366 -- Load_Unit --
367 ---------------
369 function Load_Unit
370 (Load_Name : Unit_Name_Type;
371 Required : Boolean;
372 Error_Node : Node_Id;
373 Subunit : Boolean;
374 Corr_Body : Unit_Number_Type := No_Unit;
375 Renamings : Boolean := False;
376 With_Node : Node_Id := Empty;
377 PMES : Boolean := False) return Unit_Number_Type
379 Calling_Unit : Unit_Number_Type;
380 Uname_Actual : Unit_Name_Type;
381 Unum : Unit_Number_Type;
382 Unump : Unit_Number_Type;
383 Fname : File_Name_Type;
384 Pre_Name : Boolean;
385 Ren_Name : Boolean;
386 GNAT_Name : Boolean;
387 Src_Ind : Source_File_Index;
388 Save_PMES : constant Boolean := Parsing_Main_Extended_Source;
390 Save_Cunit_Restrictions : constant Save_Cunit_Boolean_Restrictions :=
391 Cunit_Boolean_Restrictions_Save;
392 -- Save current restrictions for restore at end
394 begin
395 Parsing_Main_Extended_Source := PMES;
397 -- Initialize restrictions to config restrictions for unit to load if
398 -- it is part of the main extended source, otherwise reset them.
400 -- Note: it's a bit odd but PMES is False for subunits, which is why
401 -- we have the OR here. Should be investigated some time???
403 if PMES or Subunit then
404 Restore_Config_Cunit_Boolean_Restrictions;
405 else
406 Reset_Cunit_Boolean_Restrictions;
407 end if;
409 -- If renamings are allowed and we have a child unit name, then we
410 -- must first load the parent to deal with finding the real name.
411 -- Retain the with_clause that names the child, so that if it is
412 -- limited, the parent is loaded under the same condition.
414 if Renamings and then Is_Child_Name (Load_Name) then
415 Unump :=
416 Load_Unit
417 (Load_Name => Get_Parent_Spec_Name (Load_Name),
418 Required => Required,
419 Subunit => False,
420 Renamings => True,
421 Error_Node => Error_Node,
422 With_Node => With_Node);
424 if Unump = No_Unit then
425 Parsing_Main_Extended_Source := Save_PMES;
426 return No_Unit;
427 end if;
429 -- If parent is a renaming, then we use the renamed package as
430 -- the actual parent for the subsequent load operation.
432 if Nkind (Unit (Cunit (Unump))) = N_Package_Renaming_Declaration then
433 Uname_Actual :=
434 New_Child
435 (Load_Name, Get_Unit_Name (Name (Unit (Cunit (Unump)))));
437 -- If the load is for a with_clause, for visibility purposes both
438 -- the renamed entity and renaming one must be available in the
439 -- current unit: the renamed one in order to retrieve the child
440 -- unit, and the original one because it may be used as a prefix
441 -- in the body of the current unit. We add an explicit with_clause
442 -- for the original parent so that the renaming declaration is
443 -- properly loaded and analyzed.
445 if Present (With_Node) then
446 Insert_After (With_Node,
447 Make_With_Clause (Sloc (With_Node),
448 Name => Copy_Separate_Tree (Prefix (Name (With_Node)))));
449 end if;
451 -- Save the renaming entity, to establish its visibility when
452 -- installing the context. The implicit with is on this entity,
453 -- not on the package it renames. This is somewhat redundant given
454 -- the with_clause just created, but it simplifies subsequent
455 -- expansion of the current with_clause. Optimizable ???
457 if Nkind (Error_Node) = N_With_Clause
458 and then Nkind (Name (Error_Node)) = N_Selected_Component
459 then
460 declare
461 Par : Node_Id := Name (Error_Node);
463 begin
464 while Nkind (Par) = N_Selected_Component
465 and then Chars (Selector_Name (Par)) /=
466 Chars (Cunit_Entity (Unump))
467 loop
468 Par := Prefix (Par);
469 end loop;
471 -- Case of some intermediate parent is a renaming
473 if Nkind (Par) = N_Selected_Component then
474 Set_Entity (Selector_Name (Par), Cunit_Entity (Unump));
476 -- Case where the ultimate parent is a renaming
478 else
479 Set_Entity (Par, Cunit_Entity (Unump));
480 end if;
481 end;
482 end if;
484 -- If the parent is not a renaming, then get its name (this may
485 -- be different from the parent spec name obtained above because
486 -- of renamings higher up in the hierarchy).
488 else
489 Uname_Actual := New_Child (Load_Name, Unit_Name (Unump));
490 end if;
492 -- Here if unit to be loaded is not a child unit
494 else
495 Uname_Actual := Load_Name;
496 end if;
498 Fname := Get_File_Name (Uname_Actual, Subunit);
499 Pre_Name :=
500 Is_Predefined_File_Name (Fname, Renamings_Included => False);
501 Ren_Name := Is_Predefined_Renaming_File_Name (Fname);
502 GNAT_Name := Is_GNAT_File_Name (Fname);
504 if Debug_Flag_L then
505 Write_Eol;
506 Write_Str ("*** Load request for unit: ");
507 Write_Unit_Name (Load_Name);
509 if Required then
510 Write_Str (" (Required = True)");
511 else
512 Write_Str (" (Required = False)");
513 end if;
515 Write_Eol;
517 if Uname_Actual /= Load_Name then
518 Write_Str ("*** Actual unit loaded: ");
519 Write_Unit_Name (Uname_Actual);
520 end if;
521 end if;
523 -- Capture error location if it is for the main unit. The idea is to
524 -- post errors on the main unit location, not the most recent unit.
525 -- Note: Unit_Name (Main_Unit) is not set if we are parsing gnat.adc.
527 if Present (Error_Node)
528 and then Unit_Name (Main_Unit) /= No_Unit_Name
529 then
530 -- It seems like In_Extended_Main_Source_Unit (Error_Node) would
531 -- do the trick here, but that's wrong, it is much too early to
532 -- call this routine. We are still in the parser, and the required
533 -- semantic information is not established yet. So we base the
534 -- judgment on unit names.
536 Get_External_Unit_Name_String (Unit_Name (Main_Unit));
538 declare
539 Main_Unit_Name : constant String := Name_Buffer (1 .. Name_Len);
541 begin
542 Get_External_Unit_Name_String
543 (Unit_Name (Get_Source_Unit (Error_Node)));
545 -- If the two names are identical, then for sure we are part
546 -- of the extended main unit
548 if Main_Unit_Name = Name_Buffer (1 .. Name_Len) then
549 Load_Msg_Sloc := Sloc (Error_Node);
551 -- If the load is called from a with_type clause, the error
552 -- node is correct.
554 -- Otherwise, check for the subunit case, and if so, consider
555 -- we have a match if one name is a prefix of the other name.
557 else
558 if Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
559 or else
560 Nkind (Unit (Cunit (Get_Source_Unit (Error_Node)))) =
561 N_Subunit
562 then
563 Name_Len := Integer'Min (Name_Len, Main_Unit_Name'Length);
565 if Name_Buffer (1 .. Name_Len)
567 Main_Unit_Name (1 .. Name_Len)
568 then
569 Load_Msg_Sloc := Sloc (Error_Node);
570 end if;
571 end if;
572 end if;
573 end;
574 end if;
576 -- If we are generating error messages, then capture calling unit
578 if Present (Error_Node) then
579 Calling_Unit := Get_Source_Unit (Error_Node);
580 else
581 Calling_Unit := No_Unit;
582 end if;
584 -- See if we already have an entry for this unit
586 Unum := Main_Unit;
587 while Unum <= Units.Last loop
588 exit when Uname_Actual = Units.Table (Unum).Unit_Name;
589 Unum := Unum + 1;
590 end loop;
592 -- Whether or not the entry was found, Unum is now the right value,
593 -- since it is one more than Units.Last (i.e. the index of the new
594 -- entry we will create) in the not found case.
596 -- A special check is necessary in the unit not found case. If the unit
597 -- is not found, but the file in which it lives has already been loaded,
598 -- then we have the problem that the file does not contain the unit that
599 -- is needed. We simply treat this as a file not found condition.
601 -- We skip this test in multiple unit per file mode since in this
602 -- case we can have multiple units from the same source file.
604 if Unum > Units.Last and then Get_Unit_Index (Uname_Actual) = 0 then
605 for J in Units.First .. Units.Last loop
606 if Fname = Units.Table (J).Unit_File_Name then
607 if Debug_Flag_L then
608 Write_Str (" file does not contain unit, Unit_Number = ");
609 Write_Int (Int (Unum));
610 Write_Eol;
611 Write_Eol;
612 end if;
614 if Present (Error_Node) then
615 Get_Name_String (Fname);
617 if Is_Predefined_File_Name (Fname) then
618 Error_Msg_Unit_1 := Uname_Actual;
619 Error_Msg
620 ("$$ is not a language defined unit", Load_Msg_Sloc);
621 else
622 Error_Msg_File_1 := Fname;
623 Error_Msg_Unit_1 := Uname_Actual;
624 Error_Msg ("File{ does not contain unit$", Load_Msg_Sloc);
625 end if;
627 Write_Dependency_Chain;
628 Unum := No_Unit;
629 goto Done;
631 else
632 Unum := No_Unit;
633 goto Done;
634 end if;
635 end if;
636 end loop;
637 end if;
639 -- If we are proceeding with load, then make load stack entry,
640 -- and indicate the kind of with_clause responsible for the load.
642 Load_Stack.Increment_Last;
643 Load_Stack.Table (Load_Stack.Last) := (Unum, With_Node);
645 -- Case of entry already in table
647 if Unum <= Units.Last then
649 -- Here is where we check for a circular dependency, which is
650 -- an attempt to load a unit which is currently in the process
651 -- of being loaded. We do *not* care about a circular chain that
652 -- leads back to a body, because this kind of circular dependence
653 -- legitimately occurs (e.g. two package bodies that contain
654 -- inlined subprogram referenced by the other).
656 -- Ada 2005 (AI-50217): We also ignore limited_with clauses, because
657 -- their purpose is precisely to create legal circular structures.
659 if Loading (Unum)
660 and then (Is_Spec_Name (Units.Table (Unum).Unit_Name)
661 or else Acts_As_Spec (Units.Table (Unum).Cunit))
662 and then (Nkind (Error_Node) /= N_With_Clause
663 or else not Limited_Present (Error_Node))
664 and then not From_Limited_With_Chain
665 then
666 if Debug_Flag_L then
667 Write_Str (" circular dependency encountered");
668 Write_Eol;
669 end if;
671 if Present (Error_Node) then
672 Error_Msg ("circular unit dependency", Load_Msg_Sloc);
673 Write_Dependency_Chain;
674 else
675 Load_Stack.Decrement_Last;
676 end if;
678 Unum := No_Unit;
679 goto Done;
680 end if;
682 if Debug_Flag_L then
683 Write_Str (" unit already in file table, Unit_Number = ");
684 Write_Int (Int (Unum));
685 Write_Eol;
686 end if;
688 Load_Stack.Decrement_Last;
689 goto Done;
691 -- Unit is not already in table, so try to open the file
693 else
694 if Debug_Flag_L then
695 Write_Str (" attempt unit load, Unit_Number = ");
696 Write_Int (Int (Unum));
697 Write_Eol;
698 end if;
700 Src_Ind := Load_Source_File (Fname);
702 -- Make a partial entry in the file table, used even in the file not
703 -- found case to print the dependency chain including the last entry
705 Units.Increment_Last;
706 Units.Table (Unum).Unit_Name := Uname_Actual;
708 -- File was found
710 if Src_Ind /= No_Source_File then
711 Units.Table (Unum) :=
712 (Cunit => Empty,
713 Cunit_Entity => Empty,
714 Dependency_Num => 0,
715 Dynamic_Elab => False,
716 Error_Location => Sloc (Error_Node),
717 Expected_Unit => Uname_Actual,
718 Fatal_Error => None,
719 Generate_Code => False,
720 Has_RACW => False,
721 Filler => False,
722 Ident_String => Empty,
724 Is_Predefined_Renaming => Ren_Name,
725 Is_Predefined_Unit => Pre_Name or Ren_Name,
726 Is_Internal_Unit => Pre_Name or Ren_Name or GNAT_Name,
727 Filler2 => False,
729 Loading => True,
730 Main_Priority => Default_Main_Priority,
731 Main_CPU => Default_Main_CPU,
732 Munit_Index => 0,
733 No_Elab_Code_All => False,
734 Serial_Number => 0,
735 Source_Index => Src_Ind,
736 Unit_File_Name => Fname,
737 Unit_Name => Uname_Actual,
738 Version => Source_Checksum (Src_Ind),
739 OA_Setting => 'O');
741 -- Parse the new unit
743 declare
744 Save_Index : constant Nat := Multiple_Unit_Index;
745 Save_PMES : constant Boolean := Parsing_Main_Extended_Source;
747 begin
748 Multiple_Unit_Index := Get_Unit_Index (Uname_Actual);
749 Units.Table (Unum).Munit_Index := Multiple_Unit_Index;
750 Initialize_Scanner (Unum, Source_Index (Unum));
752 if Calling_Unit = Main_Unit and then Subunit then
753 Parsing_Main_Extended_Source := True;
754 end if;
756 Discard_List (Par (Configuration_Pragmas => False));
758 Parsing_Main_Extended_Source := Save_PMES;
760 Multiple_Unit_Index := Save_Index;
761 Set_Loading (Unum, False);
762 end;
764 -- If spec is irrelevant, then post errors and quit
766 if Corr_Body /= No_Unit
767 and then Spec_Is_Irrelevant (Unum, Corr_Body)
768 then
769 Error_Msg_File_1 := Unit_File_Name (Corr_Body);
770 Error_Msg
771 ("cannot compile subprogram in file {!", Load_Msg_Sloc);
772 Error_Msg_File_1 := Unit_File_Name (Unum);
773 Error_Msg
774 ("\incorrect spec in file { must be removed first!",
775 Load_Msg_Sloc);
776 Unum := No_Unit;
777 goto Done;
778 end if;
780 -- If loaded unit had an error, then caller inherits setting
782 if Present (Error_Node) then
783 case Units.Table (Unum).Fatal_Error is
785 -- Nothing to do if with'ed unit had no error
787 when None =>
788 null;
790 -- If with'ed unit had a detected fatal error, propagate it
792 when Error_Detected =>
793 Units.Table (Calling_Unit).Fatal_Error := Error_Detected;
795 -- If with'ed unit had an ignored error, then propagate it
796 -- but do not overide an existring setting.
798 when Error_Ignored =>
799 if Units.Table (Calling_Unit).Fatal_Error = None then
800 Units.Table (Calling_Unit).Fatal_Error :=
801 Error_Ignored;
802 end if;
803 end case;
804 end if;
806 -- Remove load stack entry and return the entry in the file table
808 Load_Stack.Decrement_Last;
810 -- All done, return unit number
812 goto Done;
814 -- Case of file not found
816 else
817 if Debug_Flag_L then
818 Write_Str (" file was not found, load failed");
819 Write_Eol;
820 end if;
822 -- Generate message if unit required
824 if Required then
825 Get_Name_String (Fname);
827 if Is_Predefined_File_Name (Fname) then
829 -- This is a predefined library unit which is not present
830 -- in the run time. If a predefined unit is not available
831 -- it may very likely be the case that there is also pragma
832 -- Restriction forbidding its usage. This is typically the
833 -- case when building a configurable run time, where the
834 -- usage of certain run-time units is restricted by means
835 -- of both the corresponding pragma Restriction (such as
836 -- No_Calendar), and by not including the unit. Hence, we
837 -- check whether this predefined unit is forbidden, so that
838 -- the message about the restriction violation is generated,
839 -- if needed.
841 if Present (Error_Node) then
842 Check_Restricted_Unit (Load_Name, Error_Node);
843 end if;
845 Error_Msg_Unit_1 := Uname_Actual;
846 Error_Msg -- CODEFIX
847 ("$$ is not a predefined library unit", Load_Msg_Sloc);
849 else
850 Error_Msg_File_1 := Fname;
851 Error_Msg ("file{ not found", Load_Msg_Sloc);
852 end if;
854 Write_Dependency_Chain;
856 -- Remove unit from stack, to avoid cascaded errors on
857 -- subsequent missing files.
859 Load_Stack.Decrement_Last;
860 Units.Decrement_Last;
862 -- If unit not required, remove load stack entry and the junk
863 -- file table entry, and return No_Unit to indicate not found,
865 else
866 Load_Stack.Decrement_Last;
867 Units.Decrement_Last;
868 end if;
870 Unum := No_Unit;
871 goto Done;
872 end if;
873 end if;
875 -- Here to exit, with result in Unum
877 <<Done>>
878 Parsing_Main_Extended_Source := Save_PMES;
879 Cunit_Boolean_Restrictions_Restore (Save_Cunit_Restrictions);
880 return Unum;
881 end Load_Unit;
883 --------------------------
884 -- Make_Child_Decl_Unit --
885 --------------------------
887 procedure Make_Child_Decl_Unit (N : Node_Id) is
888 Unit_Decl : constant Node_Id := Library_Unit (N);
890 begin
891 Units.Increment_Last;
892 Units.Table (Units.Last) := Units.Table (Get_Cunit_Unit_Number (N));
893 Units.Table (Units.Last).Unit_Name :=
894 Get_Spec_Name (Unit_Name (Get_Cunit_Unit_Number (N)));
895 Units.Table (Units.Last).Cunit := Unit_Decl;
896 Units.Table (Units.Last).Cunit_Entity :=
897 Defining_Identifier
898 (Defining_Unit_Name (Specification (Unit (Unit_Decl))));
900 -- The library unit created for of a child subprogram unit plays no
901 -- role in code generation and binding, so label it accordingly.
903 Units.Table (Units.Last).Generate_Code := False;
904 Set_Has_No_Elaboration_Code (Unit_Decl);
905 end Make_Child_Decl_Unit;
907 ------------------------
908 -- Make_Instance_Unit --
909 ------------------------
911 -- If the unit is an instance, it appears as a package declaration, but
912 -- contains both declaration and body of the instance. The body becomes
913 -- the main unit of the compilation, and the declaration is inserted
914 -- at the end of the unit table. The main unit now has the name of a
915 -- body, which is constructed from the name of the original spec,
916 -- and is attached to the compilation node of the original unit. The
917 -- declaration has been attached to a new compilation unit node, and
918 -- code will have to be generated for it.
920 procedure Make_Instance_Unit (N : Node_Id; In_Main : Boolean) is
921 Sind : constant Source_File_Index := Source_Index (Main_Unit);
923 begin
924 Units.Increment_Last;
926 if In_Main then
927 Units.Table (Units.Last) := Units.Table (Main_Unit);
928 Units.Table (Units.Last).Cunit := Library_Unit (N);
929 Units.Table (Units.Last).Generate_Code := True;
930 Units.Table (Main_Unit).Cunit := N;
931 Units.Table (Main_Unit).Unit_Name :=
932 Get_Body_Name
933 (Unit_Name (Get_Cunit_Unit_Number (Library_Unit (N))));
934 Units.Table (Main_Unit).Version := Source_Checksum (Sind);
936 else
937 -- Duplicate information from instance unit, for the body. The unit
938 -- node N has been rewritten as a body, but it was placed in the
939 -- units table when first loaded as a declaration.
941 Units.Table (Units.Last) := Units.Table (Get_Cunit_Unit_Number (N));
942 Units.Table (Units.Last).Cunit := Library_Unit (N);
943 end if;
944 end Make_Instance_Unit;
946 ------------------------
947 -- Spec_Is_Irrelevant --
948 ------------------------
950 function Spec_Is_Irrelevant
951 (Spec_Unit : Unit_Number_Type;
952 Body_Unit : Unit_Number_Type) return Boolean
954 Sunit : constant Node_Id := Cunit (Spec_Unit);
955 Bunit : constant Node_Id := Cunit (Body_Unit);
957 begin
958 -- The spec is irrelevant if the body is a subprogram body, and the spec
959 -- is other than a subprogram spec or generic subprogram spec. Note that
960 -- the names must be the same, we don't need to check that, because we
961 -- already know that from the fact that the file names are the same.
963 return
964 Nkind (Unit (Bunit)) = N_Subprogram_Body
965 and then Nkind (Unit (Sunit)) /= N_Subprogram_Declaration
966 and then Nkind (Unit (Sunit)) /= N_Generic_Subprogram_Declaration;
967 end Spec_Is_Irrelevant;
969 --------------------
970 -- Version_Update --
971 --------------------
973 procedure Version_Update (U : Node_Id; From : Node_Id) is
974 Unum : constant Unit_Number_Type := Get_Cunit_Unit_Number (U);
975 Fnum : constant Unit_Number_Type := Get_Cunit_Unit_Number (From);
976 begin
977 if Source_Index (Fnum) /= No_Source_File then
978 Units.Table (Unum).Version :=
979 Units.Table (Unum).Version
981 Source_Checksum (Source_Index (Fnum));
982 end if;
983 end Version_Update;
985 ----------------------------
986 -- Write_Dependency_Chain --
987 ----------------------------
989 procedure Write_Dependency_Chain is
990 begin
991 -- The dependency chain is only written if it is at least two entries
992 -- deep, otherwise it is trivial (the main unit depending on a unit
993 -- that it obviously directly depends on).
995 if Load_Stack.Last - 1 > Load_Stack.First then
996 for U in Load_Stack.First .. Load_Stack.Last - 1 loop
997 Error_Msg_Unit_1 :=
998 Unit_Name (Load_Stack.Table (U).Unit_Number);
999 Error_Msg_Unit_2 :=
1000 Unit_Name (Load_Stack.Table (U + 1).Unit_Number);
1001 Error_Msg ("$ depends on $!", Load_Msg_Sloc);
1002 end loop;
1003 end if;
1004 end Write_Dependency_Chain;
1006 end Lib.Load;