OSDN Git Service

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