OSDN Git Service

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