OSDN Git Service

9944bbf713b6bae5d27ec2aade3953a68e59add2
[pf3gnuchains/gcc-fork.git] / gcc / ada / rtsfind.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              R T S F I N D                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2008, 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 Casing;   use Casing;
28 with Csets;    use Csets;
29 with Debug;    use Debug;
30 with Einfo;    use Einfo;
31 with Elists;   use Elists;
32 with Errout;   use Errout;
33 with Exp_Dist; use Exp_Dist;
34 with Fname;    use Fname;
35 with Fname.UF; use Fname.UF;
36 with Lib;      use Lib;
37 with Lib.Load; use Lib.Load;
38 with Namet;    use Namet;
39 with Nlists;   use Nlists;
40 with Nmake;    use Nmake;
41 with Output;   use Output;
42 with Opt;      use Opt;
43 with Restrict; use Restrict;
44 with Sem;      use Sem;
45 with Sem_Ch7;  use Sem_Ch7;
46 with Sem_Dist; use Sem_Dist;
47 with Sem_Util; use Sem_Util;
48 with Sinfo;    use Sinfo;
49 with Stand;    use Stand;
50 with Snames;   use Snames;
51 with Tbuild;   use Tbuild;
52 with Uname;    use Uname;
53
54 package body Rtsfind is
55
56    RTE_Available_Call : Boolean := False;
57    --  Set True during call to RTE from RTE_Available (or from call to
58    --  RTE_Record_Component from RTE_Record_Component_Available). Tells
59    --  the called subprogram to set RTE_Is_Available to False rather than
60    --  generating an error message.
61
62    RTE_Is_Available : Boolean;
63    --  Set True by RTE_Available on entry. When RTE_Available_Call is set
64    --  True, set False if RTE would otherwise generate an error message.
65
66    ----------------
67    -- Unit table --
68    ----------------
69
70    --  The unit table has one entry for each unit included in the definition
71    --  of the type RTU_Id in the spec. The table entries are initialized in
72    --  Initialize to set the Entity field to Empty, indicating that the
73    --  corresponding unit has not yet been loaded. The fields are set when
74    --  a unit is loaded to contain the defining entity for the unit, the
75    --  unit name, and the unit number.
76
77    --  Note that a unit can be loaded either by a call to find an entity
78    --  within the unit (e.g. RTE), or by an explicit with of the unit. In
79    --  the latter case it is critical to make a call to Set_RTU_Loaded to
80    --  ensure that the entry in this table reflects the load.
81
82    --  Withed is True if an implicit with_clause has been added from some unit
83    --  other than the main unit to this unit. Withed_By_Main is the same,
84    --  except from the main unit.
85
86    type RT_Unit_Table_Record is record
87       Entity         : Entity_Id;
88       Uname          : Unit_Name_Type;
89       Unum           : Unit_Number_Type;
90       Withed         : Boolean;
91       Withed_By_Main : Boolean;
92    end record;
93
94    RT_Unit_Table : array (RTU_Id) of RT_Unit_Table_Record;
95
96    --------------------------
97    -- Runtime Entity Table --
98    --------------------------
99
100    --  There is one entry in the runtime entity table for each entity that is
101    --  included in the definition of the RE_Id type in the spec. The entries
102    --  are set by Initialize_Rtsfind to contain Empty, indicating that the
103    --  entity has not yet been located. Once the entity is located for the
104    --  first time, its ID is stored in this array, so that subsequent calls
105    --  for the same entity can be satisfied immediately.
106
107    --  NOTE: In order to avoid conflicts between record components and subprgs
108    --        that have the same name (i.e. subprogram External_Tag and
109    --        component External_Tag of package Ada.Tags) this table is not used
110    --        with Record_Components.
111
112    RE_Table : array (RE_Id) of Entity_Id;
113
114    --------------------------------
115    -- Generation of with_clauses --
116    --------------------------------
117
118    --  When a unit is implicitly loaded as a result of a call to RTE, it is
119    --  necessary to create one or two implicit with_clauses. We add such
120    --  with_clauses to the extended main unit if needed, and also to whatever
121    --  unit first needs them, which is not necessarily the main unit. The
122    --  former ensures that the object is correctly loaded by the binder. The
123    --  latter is necessary for SofCheck Inspector.
124
125    --  The flags Withed and Withed_By_Main in the unit table record are used to
126    --  avoid duplicates.
127
128    -----------------------
129    -- Local Subprograms --
130    -----------------------
131
132    function Check_CRT (E : RE_Id; Eid : Entity_Id) return Entity_Id;
133    --  Check entity Eid to ensure that configurable run-time restrictions are
134    --  met. May generate an error message (if RTE_Available_Call is false) and
135    --  raise RE_Not_Available if entity E does not exist (e.g. Eid is Empty).
136    --  Above documentation not clear ???
137
138    procedure Entity_Not_Defined (Id : RE_Id);
139    --  Outputs error messages for an entity that is not defined in the run-time
140    --  library (the form of the error message is tailored for no run time or
141    --  configurable run time mode as required).
142
143    function Get_Unit_Name (U_Id : RTU_Id) return Unit_Name_Type;
144    --  Retrieves the Unit Name given a unit id represented by its enumeration
145    --  value in RTU_Id.
146
147    procedure Load_Fail (S : String; U_Id : RTU_Id; Id : RE_Id);
148    --  Internal procedure called if we can't successfully locate or process a
149    --  run-time unit. The parameters give information about the error message
150    --  to be given. S is a reason for failing to compile the file and U_Id is
151    --  the unit id. RE_Id is the RE_Id originally passed to RTE. The message in
152    --  S is one of the following:
153    --
154    --     "not found"
155    --     "had parser errors"
156    --     "had semantic errors"
157    --
158    --  The "not found" case is treated specially in that it is considered
159    --  a normal situation in configurable run-time mode (and the message in
160    --  this case is suppressed unless we are operating in All_Errors_Mode).
161
162    procedure Load_RTU
163      (U_Id        : RTU_Id;
164       Id          : RE_Id   := RE_Null;
165       Use_Setting : Boolean := False);
166    --  Load the unit whose Id is given if not already loaded. The unit is
167    --  loaded, analyzed, and added to the WITH list, and the entry in
168    --  RT_Unit_Table is updated to reflect the load. Use_Setting is used to
169    --  indicate the initial setting for the Is_Potentially_Use_Visible flag of
170    --  the entity for the loaded unit (if it is indeed loaded). A value of
171    --  False means nothing special need be done. A value of True indicates that
172    --  this flag must be set to True. It is needed only in the Text_IO_Kludge
173    --  procedure, which may materialize an entity of Text_IO (or
174    --  [Wide_]Wide_Text_IO) that was previously unknown. Id is the RE_Id value
175    --  of the entity which was originally requested. Id is used only for error
176    --  message detail, and if it is RE_Null, then the attempt to output the
177    --  entity name is ignored.
178
179    function Make_Unit_Name (E : RE_Id; N : Node_Id) return Node_Id;
180    --  If the unit is a child unit, build fully qualified name for use in
181    --  With_Clause.
182
183    procedure Maybe_Add_With (E : RE_Id; U : in out RT_Unit_Table_Record);
184    --  If necessary, add an implicit with_clause from the current unit to the
185    --  one represented by E and U.
186
187    procedure Output_Entity_Name (Id : RE_Id; Msg : String);
188    --  Output continuation error message giving qualified name of entity
189    --  corresponding to Id, appending the string given by Msg. This call
190    --  is only effective in All_Errors mode.
191
192    function RE_Chars (E : RE_Id) return Name_Id;
193    --  Given a RE_Id value returns the Chars of the corresponding entity
194
195    procedure RTE_Error_Msg (Msg : String);
196    --  Generates a message by calling Error_Msg_N specifying Current_Error_Node
197    --  as the node location using the given Msg text. Special processing in the
198    --  case where RTE_Available_Call is set. In this case, no message is output
199    --  and instead RTE_Is_Available is set to False. Note that this can only be
200    --  used if you are sure that the message comes directly or indirectly from
201    --  a call to the RTE function.
202
203    ---------------
204    -- Check_CRT --
205    ---------------
206
207    function Check_CRT (E : RE_Id; Eid : Entity_Id) return Entity_Id is
208       U_Id : constant RTU_Id := RE_Unit_Table (E);
209
210    begin
211       if No (Eid) then
212          if RTE_Available_Call then
213             RTE_Is_Available := False;
214          else
215             Entity_Not_Defined (E);
216          end if;
217
218          raise RE_Not_Available;
219
220       --  Entity is available
221
222       else
223          --  If in No_Run_Time mode and entity is not in one of the
224          --  specially permitted units, raise the exception.
225
226          if No_Run_Time_Mode
227            and then not OK_No_Run_Time_Unit (U_Id)
228          then
229             Entity_Not_Defined (E);
230             raise RE_Not_Available;
231          end if;
232
233          --  Otherwise entity is accessible
234
235          return Eid;
236       end if;
237    end Check_CRT;
238
239    ------------------------
240    -- Entity_Not_Defined --
241    ------------------------
242
243    procedure Entity_Not_Defined (Id : RE_Id) is
244    begin
245       if No_Run_Time_Mode then
246
247          --  If the error occurs when compiling the body of a predefined
248          --  unit for inlining purposes, the body must be illegal in this
249          --  mode, and there is no point in continuing.
250
251          if Is_Predefined_File_Name
252            (Unit_File_Name (Get_Source_Unit (Sloc (Current_Error_Node))))
253          then
254             Error_Msg_N
255               ("construct not allowed in no run time mode!",
256                  Current_Error_Node);
257             raise Unrecoverable_Error;
258
259          else
260             RTE_Error_Msg ("|construct not allowed in no run time mode");
261          end if;
262
263       elsif Configurable_Run_Time_Mode then
264          RTE_Error_Msg ("|construct not allowed in this configuration>");
265       else
266          RTE_Error_Msg ("run-time configuration error");
267       end if;
268
269       Output_Entity_Name (Id, "not defined");
270    end Entity_Not_Defined;
271
272    -------------------
273    -- Get_Unit_Name --
274    -------------------
275
276    function Get_Unit_Name (U_Id : RTU_Id) return Unit_Name_Type is
277       Uname_Chars : constant String := RTU_Id'Image (U_Id);
278
279    begin
280       Name_Len := Uname_Chars'Length;
281       Name_Buffer (1 .. Name_Len) := Uname_Chars;
282       Set_Casing (All_Lower_Case);
283
284       if U_Id in Ada_Child then
285          Name_Buffer (4) := '.';
286
287          if U_Id in Ada_Calendar_Child then
288             Name_Buffer (13) := '.';
289
290          elsif U_Id in Ada_Dispatching_Child then
291             Name_Buffer (16) := '.';
292
293          elsif U_Id in Ada_Finalization_Child then
294             Name_Buffer (17) := '.';
295
296          elsif U_Id in Ada_Interrupts_Child then
297             Name_Buffer (15) := '.';
298
299          elsif U_Id in Ada_Real_Time_Child then
300             Name_Buffer (14) := '.';
301
302          elsif U_Id in Ada_Streams_Child then
303             Name_Buffer (12) := '.';
304
305          elsif U_Id in Ada_Text_IO_Child then
306             Name_Buffer (12) := '.';
307
308          elsif U_Id in Ada_Wide_Text_IO_Child then
309             Name_Buffer (17) := '.';
310
311          elsif U_Id in Ada_Wide_Wide_Text_IO_Child then
312             Name_Buffer (22) := '.';
313          end if;
314
315       elsif U_Id in Interfaces_Child then
316          Name_Buffer (11) := '.';
317
318       elsif U_Id in System_Child then
319          Name_Buffer (7) := '.';
320
321          if U_Id in System_Strings_Child then
322             Name_Buffer (15) := '.';
323          end if;
324
325          if U_Id in System_Tasking_Child then
326             Name_Buffer (15) := '.';
327          end if;
328
329          if U_Id in System_Tasking_Restricted_Child then
330             Name_Buffer (26) := '.';
331          end if;
332
333          if U_Id in System_Tasking_Protected_Objects_Child then
334             Name_Buffer (33) := '.';
335          end if;
336
337          if U_Id in System_Tasking_Async_Delays_Child then
338             Name_Buffer (28) := '.';
339          end if;
340       end if;
341
342       --  Add %s at end for spec
343
344       Name_Buffer (Name_Len + 1) := '%';
345       Name_Buffer (Name_Len + 2) := 's';
346       Name_Len := Name_Len + 2;
347
348       return Name_Find;
349    end Get_Unit_Name;
350
351    ----------------
352    -- Initialize --
353    ----------------
354
355    procedure Initialize is
356    begin
357       --  Initialize the unit table
358
359       for J in RTU_Id loop
360          RT_Unit_Table (J).Entity := Empty;
361       end loop;
362
363       for J in RE_Id loop
364          RE_Table (J) := Empty;
365       end loop;
366
367       RTE_Is_Available := False;
368    end Initialize;
369
370    ------------
371    -- Is_RTE --
372    ------------
373
374    function Is_RTE (Ent : Entity_Id; E : RE_Id) return Boolean is
375       E_Unit_Name   : Unit_Name_Type;
376       Ent_Unit_Name : Unit_Name_Type;
377
378       S  : Entity_Id;
379       E1 : Entity_Id;
380       E2 : Entity_Id;
381
382    begin
383       if No (Ent) then
384          return False;
385
386       --  If E has already a corresponding entity, check it directly,
387       --  going to full views if they exist to deal with the incomplete
388       --  and private type cases properly.
389
390       elsif Present (RE_Table (E)) then
391          E1 := Ent;
392
393          if Is_Type (E1) and then Present (Full_View (E1)) then
394             E1 := Full_View (E1);
395          end if;
396
397          E2 := RE_Table (E);
398
399          if Is_Type (E2) and then Present (Full_View (E2)) then
400             E2 := Full_View (E2);
401          end if;
402
403          return E1 = E2;
404       end if;
405
406       --  If the unit containing E is not loaded, we already know that
407       --  the entity we have cannot have come from this unit.
408
409       E_Unit_Name := Get_Unit_Name (RE_Unit_Table (E));
410
411       if not Is_Loaded (E_Unit_Name) then
412          return False;
413       end if;
414
415       --  Here the unit containing the entity is loaded. We have not made
416       --  an explicit call to RTE to get the entity in question, but we may
417       --  have obtained a reference to it indirectly from some other entity
418       --  in the same unit, or some other unit that references it.
419
420       --  Get the defining unit of the entity
421
422       S := Scope (Ent);
423
424       if Ekind (S) /= E_Package then
425          return False;
426       end if;
427
428       Ent_Unit_Name := Get_Unit_Name (Unit_Declaration_Node (S));
429
430       --  If the defining unit of the entity we are testing is not the
431       --  unit containing E, then they cannot possibly match.
432
433       if Ent_Unit_Name /= E_Unit_Name then
434          return False;
435       end if;
436
437       --  If the units match, then compare the names (remember that no
438       --  overloading is permitted in entities fetched using Rtsfind).
439
440       if RE_Chars (E) = Chars (Ent) then
441          RE_Table (E) := Ent;
442
443          --  If front-end inlining is enabled, we may be within a body that
444          --  contains inlined functions, which has not been retrieved through
445          --  rtsfind, and therefore is not yet recorded in the RT_Unit_Table.
446          --  Add the unit information now, it must be fully available.
447
448          declare
449             U : RT_Unit_Table_Record
450                   renames  RT_Unit_Table (RE_Unit_Table (E));
451          begin
452             if No (U.Entity) then
453                U.Entity := S;
454                U.Uname  := E_Unit_Name;
455                U.Unum   := Get_Source_Unit (S);
456             end if;
457          end;
458
459          return True;
460       else
461          return False;
462       end if;
463    end Is_RTE;
464
465    ------------
466    -- Is_RTU --
467    ------------
468
469    function Is_RTU (Ent : Entity_Id;  U : RTU_Id) return Boolean is
470       E : constant Entity_Id := RT_Unit_Table (U).Entity;
471    begin
472       return Present (E) and then E = Ent;
473    end Is_RTU;
474
475    ----------------------------
476    -- Is_Text_IO_Kludge_Unit --
477    ----------------------------
478
479    function Is_Text_IO_Kludge_Unit (Nam : Node_Id) return Boolean is
480       Prf : Node_Id;
481       Sel : Node_Id;
482
483    begin
484       if Nkind (Nam) /= N_Expanded_Name then
485          return False;
486       end if;
487
488       Prf := Prefix (Nam);
489       Sel := Selector_Name (Nam);
490
491       if Nkind (Sel) /= N_Expanded_Name
492         or else Nkind (Prf) /= N_Identifier
493         or else Chars (Prf) /= Name_Ada
494       then
495          return False;
496       end if;
497
498       Prf := Prefix (Sel);
499       Sel := Selector_Name (Sel);
500
501       return
502         Nkind (Prf) = N_Identifier
503           and then
504            (Chars (Prf) = Name_Text_IO
505               or else
506             Chars (Prf) = Name_Wide_Text_IO
507               or else
508             Chars (Prf) = Name_Wide_Wide_Text_IO)
509           and then
510         Nkind (Sel) = N_Identifier
511           and then
512         Chars (Sel) in Text_IO_Package_Name;
513    end Is_Text_IO_Kludge_Unit;
514
515    ---------------
516    -- Load_Fail --
517    ---------------
518
519    procedure Load_Fail (S : String; U_Id : RTU_Id; Id : RE_Id) is
520       M : String (1 .. 100);
521       P : Natural := 0;
522
523    begin
524       --  Output header message
525
526       if Configurable_Run_Time_Mode then
527          RTE_Error_Msg ("construct not allowed in configurable run-time mode");
528       else
529          RTE_Error_Msg ("run-time library configuration error");
530       end if;
531
532       --  Output file name and reason string
533
534       if S /= "not found"
535         or else not Configurable_Run_Time_Mode
536         or else All_Errors_Mode
537       then
538          M (1 .. 6) := "\file ";
539          P := 6;
540
541          Get_Name_String
542            (Get_File_Name (RT_Unit_Table (U_Id).Uname, Subunit => False));
543          M (P + 1 .. P + Name_Len) := Name_Buffer (1 .. Name_Len);
544          P := P + Name_Len;
545
546          M (P + 1) := ' ';
547          P := P + 1;
548
549          M (P + 1 .. P + S'Length) := S;
550          P := P + S'Length;
551
552          RTE_Error_Msg (M (1 .. P));
553
554          --  Output entity name
555
556          Output_Entity_Name (Id, "not available");
557       end if;
558
559       --  In configurable run time mode, we raise RE_Not_Available, and the
560       --  caller is expected to deal gracefully with this. In the case of a
561       --  call to RTE_Available, this exception will be caught in Rtsfind,
562       --  and result in a returned value of False for the call.
563
564       if Configurable_Run_Time_Mode then
565          raise RE_Not_Available;
566
567       --  Here we have a load failure in normal full run time mode. See if we
568       --  are in the context of an RTE_Available call. If so, we just raise
569       --  RE_Not_Available. This can happen if a unit is unavailable, which
570       --  happens for example in the VM case, where the run-time is not
571       --  complete, but we do not regard it as a configurable run-time.
572       --  If the caller has done an explicit call to RTE_Available, then
573       --  clearly the caller is prepared to deal with a result of False.
574
575       elsif RTE_Available_Call then
576          RTE_Is_Available := False;
577          raise RE_Not_Available;
578
579       --  If we are not in the context of an RTE_Available call, we are really
580       --  trying to load an entity that is not there, and that should never
581       --  happen, so in this case we signal a fatal error.
582
583       else
584          raise Unrecoverable_Error;
585       end if;
586    end Load_Fail;
587
588    --------------
589    -- Load_RTU --
590    --------------
591
592    procedure Load_RTU
593      (U_Id        : RTU_Id;
594       Id          : RE_Id   := RE_Null;
595       Use_Setting : Boolean := False)
596    is
597       U        : RT_Unit_Table_Record renames RT_Unit_Table (U_Id);
598       Priv_Par : constant Elist_Id := New_Elmt_List;
599       Lib_Unit : Node_Id;
600
601       procedure Save_Private_Visibility;
602       --  If the current unit is the body of child unit or the spec of a
603       --  private child unit, the private declarations of the parent(s) are
604       --  visible. If the unit to be loaded is another public sibling, its
605       --  compilation will affect the visibility of the common ancestors.
606       --  Indicate those that must be restored.
607
608       procedure Restore_Private_Visibility;
609       --  Restore the visibility of ancestors after compiling RTU
610
611       --------------------------------
612       -- Restore_Private_Visibility --
613       --------------------------------
614
615       procedure Restore_Private_Visibility is
616          E_Par : Elmt_Id;
617
618       begin
619          E_Par := First_Elmt (Priv_Par);
620          while Present (E_Par) loop
621             if not In_Private_Part (Node (E_Par)) then
622                Install_Private_Declarations (Node (E_Par));
623             end if;
624
625             Next_Elmt (E_Par);
626          end loop;
627       end Restore_Private_Visibility;
628
629       -----------------------------
630       -- Save_Private_Visibility --
631       -----------------------------
632
633       procedure Save_Private_Visibility is
634          Par : Entity_Id;
635
636       begin
637          Par := Scope (Current_Scope);
638          while Present (Par)
639            and then Par /= Standard_Standard
640          loop
641             if Ekind (Par) = E_Package
642               and then Is_Compilation_Unit (Par)
643               and then In_Private_Part (Par)
644             then
645                Append_Elmt (Par, Priv_Par);
646             end if;
647
648             Par := Scope (Par);
649          end loop;
650       end Save_Private_Visibility;
651
652    --  Start of processing for Load_RTU
653
654    begin
655       --  Nothing to do if unit is already loaded
656
657       if Present (U.Entity) then
658          return;
659       end if;
660
661       --  Note if secondary stack is used
662
663       if U_Id = System_Secondary_Stack then
664          Opt.Sec_Stack_Used := True;
665       end if;
666
667       --  Otherwise we need to load the unit, First build unit name
668       --  from the enumeration literal name in type RTU_Id.
669
670       U.Uname          := Get_Unit_Name (U_Id);
671       U.Withed         := False;
672       U.Withed_By_Main := False;
673
674       --  Now do the load call, note that setting Error_Node to Empty is
675       --  a signal to Load_Unit that we will regard a failure to find the
676       --  file as a fatal error, and that it should not output any kind
677       --  of diagnostics, since we will take care of it here.
678
679       --  We save style checking switches and turn off style checking for
680       --  loading the unit, since we don't want any style checking!
681
682       declare
683          Save_Style_Check : constant Boolean := Style_Check;
684       begin
685          Style_Check := False;
686          U.Unum :=
687            Load_Unit
688              (Load_Name  => U.Uname,
689               Required   => False,
690               Subunit    => False,
691               Error_Node => Empty);
692          Style_Check := Save_Style_Check;
693       end;
694
695       --  Check for bad unit load
696
697       if U.Unum = No_Unit then
698          Load_Fail ("not found", U_Id, Id);
699       elsif Fatal_Error (U.Unum) then
700          Load_Fail ("had parser errors", U_Id, Id);
701       end if;
702
703       --  Make sure that the unit is analyzed
704
705       declare
706          Was_Analyzed : constant Boolean :=
707                           Analyzed (Cunit (Current_Sem_Unit));
708
709       begin
710          --  Pretend that the current unit is analyzed, in case it is System
711          --  or some such. This allows us to put some declarations, such as
712          --  exceptions and packed arrays of Boolean, into System even though
713          --  expanding them requires System...
714
715          --  This is a bit odd but works fine. If the RTS unit does not depend
716          --  in any way on the current unit, then it never gets back into the
717          --  current unit's tree, and the change we make to the current unit
718          --  tree is never noticed by anyone (it is undone in a moment). That
719          --  is the normal situation.
720
721          --  If the RTS Unit *does* depend on the current unit, for instance,
722          --  when you are compiling System, then you had better have finished
723          --  analyzing the part of System that is depended on before you try
724          --  to load the RTS Unit. This means having the System ordered in an
725          --  appropriate manner.
726
727          Set_Analyzed (Cunit (Current_Sem_Unit), True);
728
729          if not Analyzed (Cunit (U.Unum)) then
730
731             --  If the unit is already loaded through a limited_with_clause,
732             --  the relevant entities must already be available. We do not
733             --  want to load and analyze the unit because this would create
734             --  a real semantic dependence when the purpose of the limited_with
735             --  is precisely to avoid such.
736
737             if From_With_Type (Cunit_Entity (U.Unum)) then
738                null;
739
740             else
741                Save_Private_Visibility;
742                Semantics (Cunit (U.Unum));
743                Restore_Private_Visibility;
744
745                if Fatal_Error (U.Unum) then
746                   Load_Fail ("had semantic errors", U_Id, Id);
747                end if;
748             end if;
749          end if;
750
751          --  Undo the pretence
752
753          Set_Analyzed (Cunit (Current_Sem_Unit), Was_Analyzed);
754       end;
755
756       Lib_Unit := Unit (Cunit (U.Unum));
757       U.Entity := Defining_Entity (Lib_Unit);
758
759       if Use_Setting then
760          Set_Is_Potentially_Use_Visible (U.Entity, True);
761       end if;
762    end Load_RTU;
763
764    --------------------
765    -- Make_Unit_Name --
766    --------------------
767
768    function Make_Unit_Name (E : RE_Id; N : Node_Id) return Node_Id is
769       U_Id : constant RTU_Id := RE_Unit_Table (E);
770       U    : RT_Unit_Table_Record renames RT_Unit_Table (U_Id);
771       Nam  : Node_Id;
772       Scop : Entity_Id;
773
774    begin
775       Nam  := New_Reference_To (U.Entity, Standard_Location);
776       Scop := Scope (U.Entity);
777
778       if Nkind (N) = N_Defining_Program_Unit_Name then
779          while Scop /= Standard_Standard loop
780             Nam :=
781               Make_Expanded_Name (Standard_Location,
782                 Chars  => Chars (U.Entity),
783                 Prefix => New_Reference_To (Scop, Standard_Location),
784                 Selector_Name => Nam);
785             Set_Entity (Nam, U.Entity);
786
787             Scop := Scope (Scop);
788          end loop;
789       end if;
790
791       return Nam;
792    end Make_Unit_Name;
793
794    --------------------
795    -- Maybe_Add_With --
796    --------------------
797
798    procedure Maybe_Add_With (E : RE_Id; U : in out RT_Unit_Table_Record) is
799       Is_Main : constant Boolean :=
800                   In_Extended_Main_Code_Unit (Cunit_Entity (Current_Sem_Unit));
801
802    begin
803       --  We do not need to generate a with_clause for a call issued from
804       --  RTE_Component_Available.
805
806       if RTE_Available_Call then
807          return;
808       end if;
809
810       --  If the current unit is the main one, add the with_clause unless it's
811       --  already been done.
812
813       if Is_Main then
814          if U.Withed_By_Main then
815             return;
816          else
817             U.Withed_By_Main := True;
818          end if;
819
820       --  If the current unit is not the main one, add the with_clause unless
821       --  it's already been done for some non-main unit.
822
823       else
824          if U.Withed then
825             return;
826          else
827             U.Withed := True;
828          end if;
829       end if;
830
831       --  Here if we've decided to add the with_clause
832
833       declare
834          LibUnit : constant Node_Id := Unit (Cunit (U.Unum));
835          Withn   : constant Node_Id :=
836                      Make_With_Clause (Standard_Location,
837                        Name =>
838                          Make_Unit_Name
839                            (E, Defining_Unit_Name (Specification (LibUnit))));
840
841       begin
842          Set_Library_Unit       (Withn, Cunit (U.Unum));
843          Set_Corresponding_Spec (Withn, U.Entity);
844          Set_First_Name         (Withn, True);
845          Set_Implicit_With      (Withn, True);
846
847          Mark_Rewrite_Insertion (Withn);
848          Append (Withn, Context_Items (Cunit (Current_Sem_Unit)));
849          Check_Restriction_No_Dependence (Name (Withn), Current_Error_Node);
850       end;
851    end Maybe_Add_With;
852
853    ------------------------
854    -- Output_Entity_Name --
855    ------------------------
856
857    procedure Output_Entity_Name (Id : RE_Id; Msg : String) is
858       M : String (1 .. 2048);
859       P : Natural := 0;
860       --  M (1 .. P) is current message to be output
861
862       RE_Image : constant String := RE_Id'Image (Id);
863
864    begin
865       if Id = RE_Null or else not All_Errors_Mode then
866          return;
867       end if;
868
869       M (1 .. 9) := "\entity """;
870       P := 9;
871
872       --  Add unit name to message, excluding %s or %b at end
873
874       Get_Name_String (Get_Unit_Name (RE_Unit_Table (Id)));
875       Name_Len := Name_Len - 2;
876       Set_Casing (Mixed_Case);
877       M (P + 1 .. P + Name_Len) := Name_Buffer (1 .. Name_Len);
878       P := P + Name_Len;
879
880       --  Add a qualifying period
881
882       M (P + 1) := '.';
883       P := P + 1;
884
885       --  Add entity name and closing quote to message
886
887       Name_Len := RE_Image'Length - 3;
888       Name_Buffer (1 .. Name_Len) := RE_Image (4 .. RE_Image'Length);
889       Set_Casing (Mixed_Case);
890       M (P + 1 .. P + Name_Len) := Name_Buffer (1 .. Name_Len);
891       P := P + Name_Len;
892       M (P + 1) := '"';
893       P := P + 1;
894
895       --  Add message
896
897       M (P + 1) := ' ';
898       P := P + 1;
899       M (P + 1 .. P + Msg'Length) := Msg;
900       P := P + Msg'Length;
901
902       --  Output message at current error node location
903
904       RTE_Error_Msg (M (1 .. P));
905    end Output_Entity_Name;
906
907    --------------
908    -- RE_Chars --
909    --------------
910
911    function RE_Chars (E : RE_Id) return Name_Id is
912       RE_Name_Chars : constant String := RE_Id'Image (E);
913
914    begin
915       --  Copy name skipping initial RE_ or RO_XX characters
916
917       if RE_Name_Chars (1 .. 2) = "RE" then
918          for J in 4 .. RE_Name_Chars'Last loop
919             Name_Buffer (J - 3) := Fold_Lower (RE_Name_Chars (J));
920          end loop;
921
922          Name_Len := RE_Name_Chars'Length - 3;
923
924       else
925          for J in 7 .. RE_Name_Chars'Last loop
926             Name_Buffer (J - 6) := Fold_Lower (RE_Name_Chars (J));
927          end loop;
928
929          Name_Len := RE_Name_Chars'Length - 6;
930       end if;
931
932       return Name_Find;
933    end RE_Chars;
934
935    ---------
936    -- RTE --
937    ---------
938
939    function RTE (E : RE_Id) return Entity_Id is
940       U_Id : constant RTU_Id := RE_Unit_Table (E);
941       U    : RT_Unit_Table_Record renames RT_Unit_Table (U_Id);
942
943       Lib_Unit : Node_Id;
944       Pkg_Ent  : Entity_Id;
945       Ename    : Name_Id;
946
947       --  The following flag is used to disable front-end inlining when RTE
948       --  is invoked. This prevents the analysis of other runtime bodies when
949       --  a particular spec is loaded through Rtsfind. This is both efficient,
950       --  and it prevents spurious visibility conflicts between use-visible
951       --  user entities, and entities in run-time packages.
952
953       --  In configurable run-time mode, subprograms marked Inline_Always must
954       --  be inlined, so in the case we retain the Front_End_Inlining mode.
955
956       Save_Front_End_Inlining : Boolean;
957
958       procedure Check_RPC;
959       --  Reject programs that make use of distribution features not supported
960       --  on the current target. Also check that the PCS is compatible with
961       --  the code generator version. On such targets (VMS, Vxworks, others?)
962       --  we provide a minimal body for System.Rpc that only supplies an
963       --  implementation of Partition_Id.
964
965       function Find_Local_Entity (E : RE_Id) return Entity_Id;
966       --  This function is used when entity E is in this compilation's main
967       --  unit. It gets the value from the already compiled declaration.
968
969       ---------------
970       -- Check_RPC --
971       ---------------
972
973       procedure Check_RPC is
974       begin
975          --  Bypass this check if debug flag -gnatdR set
976
977          if Debug_Flag_RR then
978             return;
979          end if;
980
981          --  Otherwise we need the check if we are going after one of the
982          --  critical entities in System.RPC / System.Partition_Interface.
983
984          if E = RE_Do_Rpc
985               or else
986             E = RE_Do_Apc
987               or else
988             E = RE_Params_Stream_Type
989               or else
990             E = RE_Request_Access
991          then
992             --  If generating RCI stubs, check that we have a real PCS
993
994             if (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
995                   or else
996                 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
997               and then Get_PCS_Name = Name_No_DSA
998             then
999                Set_Standard_Error;
1000                Write_Str ("distribution feature not supported");
1001                Write_Eol;
1002                raise Unrecoverable_Error;
1003
1004             --  In all cases, check Exp_Dist and System.Partition_Interface
1005             --  consistency.
1006
1007             elsif Get_PCS_Version /=
1008                     Exp_Dist.PCS_Version_Number (Get_PCS_Name)
1009             then
1010                Set_Standard_Error;
1011                Write_Str ("PCS version mismatch: expander ");
1012                Write_Int (Exp_Dist.PCS_Version_Number (Get_PCS_Name));
1013                Write_Str (", PCS (");
1014                Write_Name (Get_PCS_Name);
1015                Write_Str (") ");
1016                Write_Int (Get_PCS_Version);
1017                Write_Eol;
1018                raise Unrecoverable_Error;
1019             end if;
1020          end if;
1021       end Check_RPC;
1022
1023       -----------------------
1024       -- Find_Local_Entity --
1025       -----------------------
1026
1027       function Find_Local_Entity (E : RE_Id) return Entity_Id is
1028          RE_Str : constant String := RE_Id'Image (E);
1029          Nam    : Name_Id;
1030          Ent    : Entity_Id;
1031
1032          Save_Nam : constant String := Name_Buffer (1 .. Name_Len);
1033          --  Save name buffer and length over call
1034
1035       begin
1036          Name_Len := Natural'Max (0, RE_Str'Length - 3);
1037          Name_Buffer (1 .. Name_Len) :=
1038            RE_Str (RE_Str'First + 3 .. RE_Str'Last);
1039
1040          Nam := Name_Find;
1041          Ent := Entity_Id (Get_Name_Table_Info (Nam));
1042
1043          Name_Len := Save_Nam'Length;
1044          Name_Buffer (1 .. Name_Len) := Save_Nam;
1045
1046          return Ent;
1047       end Find_Local_Entity;
1048
1049    --  Start of processing for RTE
1050
1051    begin
1052       --  Doing a rtsfind in system.ads is special, as we cannot do this
1053       --  when compiling System itself. So if we are compiling system then
1054       --  we should already have acquired and processed the declaration
1055       --  of the entity. The test is to see if this compilation's main unit
1056       --  is System. If so, return the value from the already compiled
1057       --  declaration and otherwise do a regular find.
1058
1059       --  Not pleasant, but these kinds of annoying recursion when
1060       --  writing an Ada compiler in Ada have to be broken somewhere!
1061
1062       if Present (Main_Unit_Entity)
1063         and then Chars (Main_Unit_Entity) = Name_System
1064         and then Analyzed (Main_Unit_Entity)
1065         and then not Is_Child_Unit (Main_Unit_Entity)
1066       then
1067          return Check_CRT (E, Find_Local_Entity (E));
1068       end if;
1069
1070       Save_Front_End_Inlining := Front_End_Inlining;
1071       Front_End_Inlining := Configurable_Run_Time_Mode;
1072
1073       --  Load unit if unit not previously loaded
1074
1075       if No (RE_Table (E)) then
1076          Load_RTU (U_Id, Id => E);
1077          Lib_Unit := Unit (Cunit (U.Unum));
1078
1079          --  In the subprogram case, we are all done, the entity we want
1080          --  is the entity for the subprogram itself. Note that we do not
1081          --  bother to check that it is the entity that was requested.
1082          --  the only way that could fail to be the case is if runtime is
1083          --  hopelessly misconfigured, and it isn't worth testing for this.
1084
1085          if Nkind (Lib_Unit) = N_Subprogram_Declaration then
1086             RE_Table (E) := U.Entity;
1087
1088          --  Otherwise we must have the package case. First check package
1089          --  entity itself (e.g. RTE_Name for System.Interrupts.Name)
1090
1091          else
1092             pragma Assert (Nkind (Lib_Unit) = N_Package_Declaration);
1093             Ename := RE_Chars (E);
1094
1095             --  First we search the package entity chain. If the package
1096             --  only has a limited view, scan the corresponding list of
1097             --  incomplete types.
1098
1099             if From_With_Type (U.Entity) then
1100                Pkg_Ent := First_Entity (Limited_View (U.Entity));
1101             else
1102                Pkg_Ent := First_Entity (U.Entity);
1103             end if;
1104
1105             while Present (Pkg_Ent) loop
1106                if Ename = Chars (Pkg_Ent) then
1107                   RE_Table (E) := Pkg_Ent;
1108                   Check_RPC;
1109                   goto Found;
1110                end if;
1111
1112                Next_Entity (Pkg_Ent);
1113             end loop;
1114
1115             --  If we did not find the entity in the package entity chain,
1116             --  then check if the package entity itself matches. Note that
1117             --  we do this check after searching the entity chain, since
1118             --  the rule is that in case of ambiguity, we prefer the entity
1119             --  defined within the package, rather than the package itself.
1120
1121             if Ename = Chars (U.Entity) then
1122                RE_Table (E) := U.Entity;
1123             end if;
1124
1125             --  If we didn't find the entity we want, something is wrong.
1126             --  We just leave RE_Table (E) set to Empty and the appropriate
1127             --  action will be taken by Check_CRT when we exit.
1128
1129          end if;
1130       end if;
1131
1132    <<Found>>
1133       Maybe_Add_With (E, U);
1134
1135       Front_End_Inlining := Save_Front_End_Inlining;
1136       return Check_CRT (E, RE_Table (E));
1137    end RTE;
1138
1139    -------------------
1140    -- RTE_Available --
1141    -------------------
1142
1143    function RTE_Available (E : RE_Id) return Boolean is
1144       Dummy : Entity_Id;
1145       pragma Warnings (Off, Dummy);
1146
1147       Result : Boolean;
1148
1149       Save_RTE_Available_Call : constant Boolean := RTE_Available_Call;
1150       Save_RTE_Is_Available   : constant Boolean := RTE_Is_Available;
1151       --  These are saved recursively because the call to load a unit
1152       --  caused by an upper level call may perform a recursive call
1153       --  to this routine during analysis of the corresponding unit.
1154
1155    begin
1156       RTE_Available_Call := True;
1157       RTE_Is_Available := True;
1158       Dummy := RTE (E);
1159       Result := RTE_Is_Available;
1160       RTE_Available_Call := Save_RTE_Available_Call;
1161       RTE_Is_Available   := Save_RTE_Is_Available;
1162       return Result;
1163
1164    exception
1165       when RE_Not_Available =>
1166          RTE_Available_Call := Save_RTE_Available_Call;
1167          RTE_Is_Available   := Save_RTE_Is_Available;
1168          return False;
1169    end RTE_Available;
1170
1171    --------------------------
1172    -- RTE_Record_Component --
1173    --------------------------
1174
1175    function RTE_Record_Component (E : RE_Id) return Entity_Id is
1176       U_Id     : constant RTU_Id := RE_Unit_Table (E);
1177       U        : RT_Unit_Table_Record renames RT_Unit_Table (U_Id);
1178       E1       : Entity_Id;
1179       Ename    : Name_Id;
1180       Found_E  : Entity_Id;
1181       Lib_Unit : Node_Id;
1182       Pkg_Ent  : Entity_Id;
1183
1184       --  The following flag is used to disable front-end inlining when
1185       --  RTE_Record_Component is invoked. This prevents the analysis of other
1186       --  runtime bodies when a particular spec is loaded through Rtsfind. This
1187       --  is both efficient, and it prevents spurious visibility conflicts
1188       --  between use-visible user entities, and entities in run-time packages.
1189
1190       --  In configurable run-time mode, subprograms marked Inline_Always must
1191       --  be inlined, so in the case we retain the Front_End_Inlining mode.
1192
1193       Save_Front_End_Inlining : Boolean;
1194
1195    begin
1196       --  Note: Contrary to subprogram RTE, there is no need to do any special
1197       --  management with package system.ads because it has no record type
1198       --  declarations.
1199
1200       Save_Front_End_Inlining := Front_End_Inlining;
1201       Front_End_Inlining      := Configurable_Run_Time_Mode;
1202
1203       --  Load unit if unit not previously loaded
1204
1205       if not Present (U.Entity) then
1206          Load_RTU (U_Id, Id => E);
1207       end if;
1208
1209       Lib_Unit := Unit (Cunit (U.Unum));
1210
1211       pragma Assert (Nkind (Lib_Unit) = N_Package_Declaration);
1212       Ename := RE_Chars (E);
1213
1214       --  Search the entity in the components of record type declarations
1215       --  found in the package entity chain.
1216
1217       Found_E := Empty;
1218       Pkg_Ent := First_Entity (U.Entity);
1219       Search : while Present (Pkg_Ent) loop
1220          if Is_Record_Type (Pkg_Ent) then
1221             E1 := First_Entity (Pkg_Ent);
1222             while Present (E1) loop
1223                if Ename = Chars (E1) then
1224                   pragma Assert (not Present (Found_E));
1225                   Found_E := E1;
1226                end if;
1227
1228                Next_Entity (E1);
1229             end loop;
1230          end if;
1231
1232          Next_Entity (Pkg_Ent);
1233       end loop Search;
1234
1235       --  If we didn't find the entity we want, something is wrong. The
1236       --  appropriate action will be taken by Check_CRT when we exit.
1237
1238       Maybe_Add_With (E, U);
1239
1240       Front_End_Inlining := Save_Front_End_Inlining;
1241       return Check_CRT (E, Found_E);
1242    end RTE_Record_Component;
1243
1244    ------------------------------------
1245    -- RTE_Record_Component_Available --
1246    ------------------------------------
1247
1248    function RTE_Record_Component_Available (E : RE_Id) return Boolean is
1249       Dummy : Entity_Id;
1250       pragma Warnings (Off, Dummy);
1251
1252       Result : Boolean;
1253
1254       Save_RTE_Available_Call : constant Boolean := RTE_Available_Call;
1255       Save_RTE_Is_Available   : constant Boolean := RTE_Is_Available;
1256       --  These are saved recursively because the call to load a unit
1257       --  caused by an upper level call may perform a recursive call
1258       --  to this routine during analysis of the corresponding unit.
1259
1260    begin
1261       RTE_Available_Call := True;
1262       RTE_Is_Available := True;
1263       Dummy := RTE_Record_Component (E);
1264       Result := RTE_Is_Available;
1265       RTE_Available_Call := Save_RTE_Available_Call;
1266       RTE_Is_Available   := Save_RTE_Is_Available;
1267       return Result;
1268
1269    exception
1270       when RE_Not_Available =>
1271          RTE_Available_Call := Save_RTE_Available_Call;
1272          RTE_Is_Available   := Save_RTE_Is_Available;
1273          return False;
1274    end RTE_Record_Component_Available;
1275
1276    -------------------
1277    -- RTE_Error_Msg --
1278    -------------------
1279
1280    procedure RTE_Error_Msg (Msg : String) is
1281    begin
1282       if RTE_Available_Call then
1283          RTE_Is_Available := False;
1284       else
1285          Error_Msg_N (Msg, Current_Error_Node);
1286
1287          --  Bump count of violations if we are in configurable run-time
1288          --  mode and this is not a continuation message.
1289
1290          if Configurable_Run_Time_Mode and then Msg (Msg'First) /= '\' then
1291             Configurable_Run_Time_Violations :=
1292               Configurable_Run_Time_Violations + 1;
1293          end if;
1294       end if;
1295    end RTE_Error_Msg;
1296
1297    ----------------
1298    -- RTU_Entity --
1299    ----------------
1300
1301    function RTU_Entity (U : RTU_Id) return Entity_Id is
1302    begin
1303       return RT_Unit_Table (U).Entity;
1304    end RTU_Entity;
1305
1306    ----------------
1307    -- RTU_Loaded --
1308    ----------------
1309
1310    function RTU_Loaded (U : RTU_Id) return Boolean is
1311    begin
1312       return Present (RT_Unit_Table (U).Entity);
1313    end RTU_Loaded;
1314
1315    --------------------
1316    -- Set_RTU_Loaded --
1317    --------------------
1318
1319    procedure Set_RTU_Loaded (N : Node_Id) is
1320       Loc   : constant Source_Ptr       := Sloc (N);
1321       Unum  : constant Unit_Number_Type := Get_Source_Unit (Loc);
1322       Uname : constant Unit_Name_Type   := Unit_Name (Unum);
1323       E     : constant Entity_Id        :=
1324                 Defining_Entity (Unit (Cunit (Unum)));
1325    begin
1326       pragma Assert (Is_Predefined_File_Name (Unit_File_Name (Unum)));
1327
1328       --  Loop through entries in RTU table looking for matching entry
1329
1330       for U_Id in RTU_Id'Range loop
1331
1332          --  Here we have a match
1333
1334          if Get_Unit_Name (U_Id) = Uname then
1335             declare
1336                U : RT_Unit_Table_Record renames RT_Unit_Table (U_Id);
1337                --  The RT_Unit_Table entry that may need updating
1338
1339             begin
1340                --  If entry is not set, set it now
1341
1342                if No (U.Entity) then
1343                   U := (Entity         => E,
1344                         Uname          => Get_Unit_Name (U_Id),
1345                         Unum           => Unum,
1346                         Withed         => False,
1347                         Withed_By_Main => False);
1348                end if;
1349
1350                return;
1351             end;
1352          end if;
1353       end loop;
1354    end Set_RTU_Loaded;
1355
1356    --------------------
1357    -- Text_IO_Kludge --
1358    --------------------
1359
1360    procedure Text_IO_Kludge (Nam : Node_Id) is
1361       Chrs : Name_Id;
1362
1363       type Name_Map_Type is array (Text_IO_Package_Name) of RTU_Id;
1364
1365       Name_Map : constant Name_Map_Type := Name_Map_Type'(
1366         Name_Decimal_IO     => Ada_Text_IO_Decimal_IO,
1367         Name_Enumeration_IO => Ada_Text_IO_Enumeration_IO,
1368         Name_Fixed_IO       => Ada_Text_IO_Fixed_IO,
1369         Name_Float_IO       => Ada_Text_IO_Float_IO,
1370         Name_Integer_IO     => Ada_Text_IO_Integer_IO,
1371         Name_Modular_IO     => Ada_Text_IO_Modular_IO);
1372
1373       Wide_Name_Map : constant Name_Map_Type := Name_Map_Type'(
1374         Name_Decimal_IO     => Ada_Wide_Text_IO_Decimal_IO,
1375         Name_Enumeration_IO => Ada_Wide_Text_IO_Enumeration_IO,
1376         Name_Fixed_IO       => Ada_Wide_Text_IO_Fixed_IO,
1377         Name_Float_IO       => Ada_Wide_Text_IO_Float_IO,
1378         Name_Integer_IO     => Ada_Wide_Text_IO_Integer_IO,
1379         Name_Modular_IO     => Ada_Wide_Text_IO_Modular_IO);
1380
1381       Wide_Wide_Name_Map : constant Name_Map_Type := Name_Map_Type'(
1382         Name_Decimal_IO     => Ada_Wide_Wide_Text_IO_Decimal_IO,
1383         Name_Enumeration_IO => Ada_Wide_Wide_Text_IO_Enumeration_IO,
1384         Name_Fixed_IO       => Ada_Wide_Wide_Text_IO_Fixed_IO,
1385         Name_Float_IO       => Ada_Wide_Wide_Text_IO_Float_IO,
1386         Name_Integer_IO     => Ada_Wide_Wide_Text_IO_Integer_IO,
1387         Name_Modular_IO     => Ada_Wide_Wide_Text_IO_Modular_IO);
1388
1389    begin
1390       --  Nothing to do if name is not identifier or a selected component
1391       --  whose selector_name is not an identifier.
1392
1393       if Nkind (Nam) = N_Identifier then
1394          Chrs := Chars (Nam);
1395
1396       elsif Nkind (Nam) = N_Selected_Component
1397         and then Nkind (Selector_Name (Nam)) = N_Identifier
1398       then
1399          Chrs := Chars (Selector_Name (Nam));
1400
1401       else
1402          return;
1403       end if;
1404
1405       --  Nothing to do if name is not one of the Text_IO subpackages
1406       --  Otherwise look through loaded units, and if we find Text_IO
1407       --  or [Wide_]Wide_Text_IO already loaded, then load the proper child.
1408
1409       if Chrs in Text_IO_Package_Name then
1410          for U in Main_Unit .. Last_Unit loop
1411             Get_Name_String (Unit_File_Name (U));
1412
1413             if Name_Len = 12 then
1414
1415                --  Here is where we do the loads if we find one of the units
1416                --  Ada.Text_IO or Ada.[Wide_]Wide_Text_IO. An interesting
1417                --  detail is that these units may already be used (i.e. their
1418                --  In_Use flags may be set). Normally when the In_Use flag is
1419                --  set, the Is_Potentially_Use_Visible flag of all entities in
1420                --  the package is set, but the new entity we are mysteriously
1421                --  adding was not there to have its flag set at the time. So
1422                --  that's why we pass the extra parameter to RTU_Find, to make
1423                --  sure the flag does get set now. Given that those generic
1424                --  packages are in fact child units, we must indicate that
1425                --  they are visible.
1426
1427                if Name_Buffer (1 .. 12) = "a-textio.ads" then
1428                   Load_RTU
1429                     (Name_Map (Chrs),
1430                      Use_Setting => In_Use (Cunit_Entity (U)));
1431                   Set_Is_Visible_Child_Unit
1432                     (RT_Unit_Table (Name_Map (Chrs)).Entity);
1433
1434                elsif Name_Buffer (1 .. 12) = "a-witeio.ads" then
1435                   Load_RTU
1436                     (Wide_Name_Map (Chrs),
1437                      Use_Setting => In_Use (Cunit_Entity (U)));
1438                   Set_Is_Visible_Child_Unit
1439                     (RT_Unit_Table (Wide_Name_Map (Chrs)).Entity);
1440
1441                elsif Name_Buffer (1 .. 12) = "a-ztexio.ads" then
1442                   Load_RTU
1443                     (Wide_Wide_Name_Map (Chrs),
1444                      Use_Setting => In_Use (Cunit_Entity (U)));
1445                   Set_Is_Visible_Child_Unit
1446                     (RT_Unit_Table (Wide_Wide_Name_Map (Chrs)).Entity);
1447                end if;
1448             end if;
1449          end loop;
1450       end if;
1451
1452    exception
1453       --  Generate error message if run-time unit not available
1454
1455       when RE_Not_Available =>
1456          Error_Msg_N ("& not available", Nam);
1457    end Text_IO_Kludge;
1458
1459 end Rtsfind;