OSDN Git Service

* 1aexcept.adb, 1aexcept.ads, 1ic.ads, 1ssecsta.adb,
[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-2002, 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 2,  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 COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Atree;     use Atree;
28 with Casing;    use Casing;
29 with Csets;     use Csets;
30 with Debug;     use Debug;
31 with Einfo;     use Einfo;
32 with Elists;    use Elists;
33 with Fname;     use Fname;
34 with Fname.UF;  use Fname.UF;
35 with Lib;       use Lib;
36 with Lib.Load;  use Lib.Load;
37 with Namet;     use Namet;
38 with Nlists;    use Nlists;
39 with Nmake;     use Nmake;
40 with Output;    use Output;
41 with Opt;       use Opt;
42 with Restrict;  use Restrict;
43 with Sem;       use Sem;
44 with Sem_Ch7;   use Sem_Ch7;
45 with Sem_Util;  use Sem_Util;
46 with Sinfo;     use Sinfo;
47 with Stand;     use Stand;
48 with Snames;    use Snames;
49 with Tbuild;    use Tbuild;
50 with Uname;     use Uname;
51
52 package body Rtsfind is
53
54    ----------------
55    -- Unit table --
56    ----------------
57
58    --  The unit table has one entry for each unit included in the definition
59    --  of the type RTU_Id in the spec. The table entries are initialized in
60    --  Initialize to set the Entity field to Empty, indicating that the
61    --  corresponding unit has not yet been loaded. The fields are set when
62    --  a unit is loaded to contain the defining entity for the unit, the
63    --  unit name, and the unit number.
64
65    type RT_Unit_Table_Record is record
66       Entity : Entity_Id;
67       Uname  : Unit_Name_Type;
68       Unum   : Unit_Number_Type;
69       Withed : Boolean;
70    end record;
71
72    RT_Unit_Table : array (RTU_Id) of RT_Unit_Table_Record;
73
74    --------------------------
75    -- Runtime Entity Table --
76    --------------------------
77
78    --  There is one entry in the runtime entity table for each entity that is
79    --  included in the definition of the RE_Id type in the spec. The entries
80    --  are set by Initialize_Rtsfind to contain Empty, indicating that the
81    --  entity has not yet been located. Once the entity is located for the
82    --  first time, its ID is stored in this array, so that subsequent calls
83    --  for the same entity can be satisfied immediately.
84
85    RE_Table : array (RE_Id) of Entity_Id;
86
87    --------------------------
88    -- Generation of WITH's --
89    --------------------------
90
91    --  When a unit is implicitly loaded as a result of a call to RTE, it
92    --  is necessary to create an implicit with to ensure that the object
93    --  is correctly loaded by the binder. Such with statements are only
94    --  required when the request is from the extended main unit (if a
95    --  client needs a with, that will be taken care of when the client
96    --  is compiled.
97
98    --  We always attach the with to the main unit. This is not perfectly
99    --  accurate in terms of elaboration requirements, but it is close
100    --  enough, since the units that are accessed using rtsfind do not
101    --  have delicate elaboration requirements.
102
103    --  The flag Withed in the unit table record is initially set to False.
104    --  It is set True if a with has been generated for the main unit for
105    --  the corresponding unit.
106
107    -----------------------
108    -- Local Subprograms --
109    -----------------------
110
111    procedure Load_Fail (S : String; U_Id : RTU_Id; Ent_Name : String := "");
112    --  Internal procedure called if we can't find the entity or unit.
113    --  The parameter is a detailed error message that is to be given.
114    --  S is a reason for failing to compile the file. U_Id is the unit
115    --  id, and Ent_Name, if non-null, is the associated entity name.
116
117    function Get_Unit_Name (U_Id : RTU_Id) return Unit_Name_Type;
118    --  Retrieves the Unit Name given a unit id represented by its
119    --  enumaration value in RTU_Id.
120
121    procedure Load_RTU (U_Id : RTU_Id; Use_Setting : Boolean := False);
122    --  Load the unit whose Id is given if not already loaded. The unit is
123    --  loaded, analyzed, and added to the with list, and the entry in
124    --  RT_Unit_Table is updated to reflect the load. The second parameter
125    --  indicates the initial setting for the Is_Potentially_Use_Visible
126    --  flag of the entity for the loaded unit (if it is indeed loaded).
127    --  A value of False means nothing special need be done. A value of
128    --  True indicates that this flag must be set to True. It is needed
129    --  only in the Text_IO_Kludge procedure, which may materialize an
130    --  entity of Text_IO (or Wide_Text_IO) that was previously unknown.
131
132    function RE_Chars (E : RE_Id) return Name_Id;
133    --  Given a RE_Id value returns the Chars of the corresponding entity.
134
135    -------------------
136    -- Get_Unit_Name --
137    -------------------
138
139    function Get_Unit_Name (U_Id : RTU_Id) return Unit_Name_Type is
140       Uname_Chars : constant String := RTU_Id'Image (U_Id);
141
142    begin
143       Name_Len := Uname_Chars'Length;
144       Name_Buffer (1 .. Name_Len) := Uname_Chars;
145       Set_Casing (All_Lower_Case);
146
147       if U_Id in Ada_Child then
148          Name_Buffer (4) := '.';
149
150          if U_Id in Ada_Calendar_Child then
151             Name_Buffer (13) := '.';
152
153          elsif U_Id in Ada_Finalization_Child then
154             Name_Buffer (17) := '.';
155
156          elsif U_Id in Ada_Real_Time_Child then
157             Name_Buffer (14) := '.';
158
159          elsif U_Id in Ada_Streams_Child then
160             Name_Buffer (12) := '.';
161
162          elsif U_Id in Ada_Text_IO_Child then
163             Name_Buffer (12) := '.';
164
165          elsif U_Id in Ada_Wide_Text_IO_Child then
166             Name_Buffer (17) := '.';
167          end if;
168
169       elsif U_Id in Interfaces_Child then
170          Name_Buffer (11) := '.';
171
172       elsif U_Id in System_Child then
173          Name_Buffer (7) := '.';
174
175          if U_Id in System_Tasking_Child then
176             Name_Buffer (15) := '.';
177          end if;
178
179          if U_Id in System_Tasking_Restricted_Child then
180             Name_Buffer (26) := '.';
181          end if;
182
183          if U_Id in System_Tasking_Protected_Objects_Child then
184             Name_Buffer (33) := '.';
185          end if;
186
187          if U_Id in System_Tasking_Async_Delays_Child then
188             Name_Buffer (28) := '.';
189          end if;
190       end if;
191
192       --  Add %s at end for spec
193
194       Name_Buffer (Name_Len + 1) := '%';
195       Name_Buffer (Name_Len + 2) := 's';
196       Name_Len := Name_Len + 2;
197
198       return Name_Find;
199    end Get_Unit_Name;
200
201    ----------------
202    -- Initialize --
203    ----------------
204
205    procedure Initialize is
206    begin
207       --  Initialize the unit table
208
209       for J in RTU_Id loop
210          RT_Unit_Table (J).Entity := Empty;
211       end loop;
212
213       for J in RE_Id loop
214          RE_Table (J) := Empty;
215       end loop;
216    end Initialize;
217
218    ------------
219    -- Is_RTE --
220    ------------
221
222    function Is_RTE (Ent : Entity_Id; E : RE_Id) return Boolean is
223       E_Unit_Name   : Unit_Name_Type;
224       Ent_Unit_Name : Unit_Name_Type;
225
226       S  : Entity_Id;
227       E1 : Entity_Id;
228       E2 : Entity_Id;
229
230    begin
231       if No (Ent) then
232          return False;
233
234       --  If E has already a corresponding entity, check it directly,
235       --  going to full views if they exist to deal with the incomplete
236       --  and private type cases properly.
237
238       elsif Present (RE_Table (E)) then
239          E1 := Ent;
240
241          if Is_Type (E1) and then Present (Full_View (E1)) then
242             E1 := Full_View (E1);
243          end if;
244
245          E2 := RE_Table (E);
246
247          if Is_Type (E2) and then Present (Full_View (E2)) then
248             E2 := Full_View (E2);
249          end if;
250
251          return E1 = E2;
252       end if;
253
254       --  If the unit containing E is not loaded, we already know that
255       --  the entity we have cannot have come from this unit.
256
257       E_Unit_Name := Get_Unit_Name (RE_Unit_Table (E));
258
259       if not Is_Loaded (E_Unit_Name) then
260          return False;
261       end if;
262
263       --  Here the unit containing the entity is loaded. We have not made
264       --  an explicit call to RTE to get the entity in question, but we may
265       --  have obtained a reference to it indirectly from some other entity
266       --  in the same unit, or some other unit that references it.
267
268       --  Get the defining unit of the entity
269
270       S := Scope (Ent);
271
272       if Ekind (S) /= E_Package then
273          return False;
274       end if;
275
276       Ent_Unit_Name := Get_Unit_Name (Unit_Declaration_Node (S));
277
278       --  If the defining unit of the entity we are testing is not the
279       --  unit containing E, then they cannot possibly match.
280
281       if Ent_Unit_Name /= E_Unit_Name then
282          return False;
283       end if;
284
285       --  If the units match, then compare the names (remember that no
286       --  overloading is permitted in entities fetched using Rtsfind).
287
288       if RE_Chars (E) = Chars (Ent) then
289          RE_Table (E) := Ent;
290
291          --  If front-end inlining is enabled, we may be within a body that
292          --  contains inlined functions, which has not been retrieved through
293          --  rtsfind, and therefore is not yet recorded in the RT_Unit_Table.
294          --  Add the unit information now, it must be fully available.
295
296          declare
297             U : RT_Unit_Table_Record
298                   renames  RT_Unit_Table (RE_Unit_Table (E));
299          begin
300             if No (U.Entity) then
301                U.Entity := S;
302                U.Uname  := E_Unit_Name;
303                U.Unum   := Get_Source_Unit (S);
304             end if;
305          end;
306
307          return True;
308       else
309          return False;
310       end if;
311    end Is_RTE;
312
313    ----------------------------
314    -- Is_Text_IO_Kludge_Unit --
315    ----------------------------
316
317    function Is_Text_IO_Kludge_Unit (Nam : Node_Id) return Boolean is
318       Prf : Node_Id;
319       Sel : Node_Id;
320
321    begin
322       if Nkind (Nam) /= N_Expanded_Name then
323          return False;
324       end if;
325
326       Prf := Prefix (Nam);
327       Sel := Selector_Name (Nam);
328
329       if Nkind (Sel) /= N_Expanded_Name
330         or else Nkind (Prf) /= N_Identifier
331         or else Chars (Prf) /= Name_Ada
332       then
333          return False;
334       end if;
335
336       Prf := Prefix (Sel);
337       Sel := Selector_Name (Sel);
338
339       return
340         Nkind (Prf) = N_Identifier
341           and then
342         (Chars (Prf) = Name_Text_IO or else Chars (Prf) = Name_Wide_Text_IO)
343           and then
344         Nkind (Sel) = N_Identifier
345           and then
346         Chars (Sel) in Text_IO_Package_Name;
347
348    end Is_Text_IO_Kludge_Unit;
349
350    ---------------
351    -- Load_Fail --
352    ---------------
353
354    procedure Load_Fail (S : String; U_Id : RTU_Id; Ent_Name : String := "") is
355    begin
356       Set_Standard_Error;
357
358       Write_Str ("fatal error: run-time library configuration error");
359       Write_Eol;
360
361       if Ent_Name /= "" then
362          Write_Str ("cannot locate """);
363
364          --  Copy name skipping initial RE_ or RO_XX characters
365
366          if Ent_Name (1 .. 2) = "RE" then
367             for J in 4 .. Ent_Name'Length loop
368                Name_Buffer (J - 3) := Ent_Name (J);
369             end loop;
370          else
371             for J in 7 .. Ent_Name'Length loop
372                Name_Buffer (J - 6) := Ent_Name (J);
373             end loop;
374          end if;
375
376          Name_Len := Ent_Name'Length - 3;
377          Set_Casing (Mixed_Case);
378          Write_Str (Name_Buffer (1 .. Name_Len));
379          Write_Str (""" in file """);
380
381       else
382          Write_Str ("cannot load file """);
383       end if;
384
385       Write_Name
386         (Get_File_Name (RT_Unit_Table (U_Id).Uname, Subunit => False));
387       Write_Str (""" (");
388       Write_Str (S);
389       Write_Char (')');
390       Write_Eol;
391       Set_Standard_Output;
392       raise Unrecoverable_Error;
393    end Load_Fail;
394
395    --------------
396    -- Load_RTU --
397    --------------
398
399    procedure Load_RTU (U_Id : RTU_Id; Use_Setting : Boolean := False) is
400       Loaded   : Boolean;
401       U        : RT_Unit_Table_Record renames RT_Unit_Table (U_Id);
402       Priv_Par : Elist_Id := New_Elmt_List;
403       Lib_Unit : Node_Id;
404
405       procedure Save_Private_Visibility;
406       --  If the current unit is the body of child unit or the spec of a
407       --  private child unit, the private declarations of the parent (s)
408       --  are visible. If the unit to be loaded is another public sibling,
409       --  its compilation will affect the visibility of the common ancestors.
410       --  Indicate those that must be restored.
411
412       procedure Restore_Private_Visibility;
413       --  Restore the visibility of ancestors after compiling RTU.
414
415       --------------------------------
416       -- Restore_Private_Visibility --
417       --------------------------------
418
419       procedure Restore_Private_Visibility is
420          E_Par : Elmt_Id;
421
422       begin
423          E_Par := First_Elmt (Priv_Par);
424
425          while Present (E_Par) loop
426             if not In_Private_Part (Node (E_Par)) then
427                Install_Private_Declarations (Node (E_Par));
428             end if;
429
430             Next_Elmt (E_Par);
431          end loop;
432       end Restore_Private_Visibility;
433
434       -----------------------------
435       -- Save_Private_Visibility --
436       -----------------------------
437
438       procedure Save_Private_Visibility is
439          Par : Entity_Id;
440
441       begin
442          Par := Scope (Current_Scope);
443
444          while Present (Par)
445            and then Par /= Standard_Standard
446          loop
447             if Ekind (Par) = E_Package
448               and then Is_Compilation_Unit (Par)
449               and then In_Private_Part (Par)
450             then
451                Append_Elmt (Par, Priv_Par);
452             end if;
453
454             Par := Scope (Par);
455          end loop;
456       end Save_Private_Visibility;
457
458    --  Start of processing for Load_RTU
459
460    begin
461       --  Nothing to do if unit is already loaded
462
463       if Present (U.Entity) then
464          return;
465       end if;
466
467       --  Otherwise we need to load the unit, First build unit name
468       --  from the enumeration literal name in type RTU_Id.
469
470       U.Uname  := Get_Unit_Name (U_Id);
471       U.Withed := False;
472       Loaded   := Is_Loaded (U.Uname);
473
474       --  Now do the load call, note that setting Error_Node to Empty is
475       --  a signal to Load_Unit that we will regard a failure to find the
476       --  file as a fatal error, and that it should not output any kind
477       --  of diagnostics, since we will take care of it here.
478
479       U.Unum :=
480         Load_Unit
481           (Load_Name  => U.Uname,
482            Required   => False,
483            Subunit    => False,
484            Error_Node => Empty);
485
486       if U.Unum = No_Unit then
487          Load_Fail ("unit not found", U_Id);
488
489       elsif Fatal_Error (U.Unum) then
490          Load_Fail ("parser errors", U_Id);
491       end if;
492
493       --  Make sure that the unit is analyzed
494
495       declare
496          Was_Analyzed : Boolean := Analyzed (Cunit (Current_Sem_Unit));
497
498       begin
499          --  Pretend that the current unit is analysed, in case it is
500          --  System or some such. This allows us to put some declarations,
501          --  such as exceptions and packed arrays of Boolean, into System
502          --  even though expanding them requires System...
503
504          --  This is a bit odd but works fine. If the RTS unit does not depend
505          --  in any way on the current unit, then it never gets back into the
506          --  current unit's tree, and the change we make to the current unit
507          --  tree is never noticed by anyone (it is undone in a moment). That
508          --  is the normal situation.
509
510          --  If the RTS Unit *does* depend on the current unit, for instance,
511          --  when you are compiling System, then you had better have finished
512          --  Analyzing the part of System that is depended on before you try
513          --  to load the RTS Unit. This means having the System ordered in an
514          --  appropriate manner.
515
516          Set_Analyzed (Cunit (Current_Sem_Unit), True);
517
518          if not Analyzed (Cunit (U.Unum)) then
519
520             Save_Private_Visibility;
521             Semantics (Cunit (U.Unum));
522             Restore_Private_Visibility;
523
524             if Fatal_Error (U.Unum) then
525                Load_Fail ("semantic errors", U_Id);
526             end if;
527          end if;
528
529          --  Undo the pretence
530
531          Set_Analyzed (Cunit (Current_Sem_Unit), Was_Analyzed);
532       end;
533
534       Lib_Unit := Unit (Cunit (U.Unum));
535       U.Entity := Defining_Entity (Lib_Unit);
536
537       if Use_Setting then
538          Set_Is_Potentially_Use_Visible (U.Entity, True);
539       end if;
540    end Load_RTU;
541
542    --------------
543    -- RE_Chars --
544    --------------
545
546    function RE_Chars (E : RE_Id) return Name_Id is
547       RE_Name_Chars : constant String := RE_Id'Image (E);
548
549    begin
550       --  Copy name skipping initial RE_ or RO_XX characters
551
552       if RE_Name_Chars (1 .. 2) = "RE" then
553          for J in 4 .. RE_Name_Chars'Last loop
554             Name_Buffer (J - 3) := Fold_Lower (RE_Name_Chars (J));
555          end loop;
556
557          Name_Len := RE_Name_Chars'Length - 3;
558
559       else
560          for J in 7 .. RE_Name_Chars'Last loop
561             Name_Buffer (J - 6) := Fold_Lower (RE_Name_Chars (J));
562          end loop;
563
564          Name_Len := RE_Name_Chars'Length - 6;
565       end if;
566
567       return Name_Find;
568    end RE_Chars;
569
570    ---------
571    -- RTE --
572    ---------
573
574    function RTE (E : RE_Id) return Entity_Id is
575       U_Id : constant RTU_Id := RE_Unit_Table (E);
576       U    : RT_Unit_Table_Record renames RT_Unit_Table (U_Id);
577
578       Lib_Unit : Node_Id;
579       Pkg_Ent  : Entity_Id;
580       Ename    : Name_Id;
581
582       Ravenscar : constant Boolean := Restricted_Profile;
583
584       --  The following flag is used to disable front-end inlining when RTE
585       --  is invoked. This prevents the analysis of other runtime bodies when
586       --  a particular spec is loaded through Rtsfind. This is both efficient,
587       --  and it prevents spurious visibility conflicts between use-visible
588       --  user entities, and entities in run-time packages.
589
590       --  In No_Run_Time mode, subprograms marked Inlined_Always must be
591       --  inlined, so in the case we retain the Front_End_Inlining mode.
592
593       Save_Front_End_Inlining : Boolean;
594
595       procedure Check_RPC;
596       --  Reject programs that make use of distribution features not supported
597       --  on the current target. On such targets (VMS, Vxworks, others?) we
598       --  only provide a minimal body for System.Rpc that only supplies an
599       --  implementation of partition_id.
600
601       function Find_Local_Entity (E : RE_Id) return Entity_Id;
602       --  This function is used when entity E is in this compilation's main
603       --  unit. It gets the value from the already compiled declaration.
604
605       function Make_Unit_Name (N : Node_Id) return Node_Id;
606       --  If the unit is a child unit, build fully qualified name for use
607       --  in with_clause.
608
609       ---------------
610       -- Check_RPC --
611       ---------------
612
613       procedure Check_RPC is
614          Body_Name    : Unit_Name_Type;
615          Unum         : Unit_Number_Type;
616
617       begin
618          --  Bypass this check if debug flag -gnatdR set
619
620          if Debug_Flag_RR then
621             return;
622          end if;
623
624          --  Otherwise we need the check if we are going after one of
625          --  the critical entities in System.RPC in stubs mode.
626
627          if (Distribution_Stub_Mode = Generate_Receiver_Stub_Body
628                       or else
629                         Distribution_Stub_Mode = Generate_Caller_Stub_Body)
630            and then (E = RE_Do_Rpc
631                        or else E = RE_Do_Apc
632                        or else E = RE_Params_Stream_Type
633                        or else E = RE_RPC_Receiver)
634          then
635             --  Load body of System.Rpc, and abort if this is the body that is
636             --  provided by GNAT, for which these features are not supported
637             --  on current target. We identify the gnat body by the presence
638             --  of a local entity called Gnat in the first declaration.
639
640             Lib_Unit := Unit (Cunit (U.Unum));
641             Body_Name := Get_Body_Name (Get_Unit_Name (Lib_Unit));
642             Unum :=
643               Load_Unit
644                 (Load_Name  => Body_Name,
645                  Required   => False,
646                  Subunit    => False,
647                  Error_Node => Empty,
648                  Renamings  => True);
649
650             if Unum /= No_Unit then
651                declare
652                   Decls : List_Id := Declarations (Unit (Cunit (Unum)));
653
654                begin
655                   if Present (Decls)
656                     and then Nkind (First (Decls)) = N_Object_Declaration
657                     and then
658                       Chars (Defining_Identifier (First (Decls))) = Name_Gnat
659                   then
660                      Set_Standard_Error;
661                      Write_Str ("distribution feature not supported");
662                      Write_Eol;
663                      raise Unrecoverable_Error;
664                   end if;
665                end;
666             end if;
667          end if;
668       end Check_RPC;
669
670       ------------------------
671       -- Find_System_Entity --
672       ------------------------
673
674       function Find_Local_Entity (E : RE_Id) return Entity_Id is
675          RE_Str : String renames RE_Id'Image (E);
676          Ent    : Entity_Id;
677
678          Save_Nam : constant String := Name_Buffer (1 .. Name_Len);
679          --  Save name buffer and length over call
680
681       begin
682          Name_Len := Natural'Max (0, RE_Str'Length - 3);
683          Name_Buffer (1 .. Name_Len) :=
684            RE_Str (RE_Str'First + 3 .. RE_Str'Last);
685
686          Ent := Entity_Id (Get_Name_Table_Info (Name_Find));
687
688          Name_Len := Save_Nam'Length;
689          Name_Buffer (1 .. Name_Len) := Save_Nam;
690
691          return Ent;
692       end Find_Local_Entity;
693
694       --------------------
695       -- Make_Unit_Name --
696       --------------------
697
698       function Make_Unit_Name (N : Node_Id) return Node_Id is
699          Nam  : Node_Id;
700          Scop : Entity_Id;
701
702       begin
703          Nam  := New_Reference_To (U.Entity, Standard_Location);
704          Scop := Scope (U.Entity);
705
706          if Nkind (N) = N_Defining_Program_Unit_Name then
707             while Scop /= Standard_Standard loop
708                Nam :=
709                  Make_Expanded_Name (Standard_Location,
710                    Chars  => Chars (U.Entity),
711                    Prefix => New_Reference_To (Scop, Standard_Location),
712                    Selector_Name => Nam);
713                Set_Entity (Nam, U.Entity);
714
715                Scop := Scope (Scop);
716             end loop;
717          end if;
718
719          return Nam;
720       end Make_Unit_Name;
721
722    --  Start of processing for RTE
723
724    begin
725
726       --  Check violation of no run time and ravenscar mode
727
728       if No_Run_Time
729         and then not OK_To_Use_In_No_Run_Time_Mode (U_Id)
730       then
731          if not Ravenscar
732            or else not OK_To_Use_In_Ravenscar_Mode (U_Id)
733          then
734             Disallow_In_No_Run_Time_Mode (Current_Error_Node);
735             return Empty;
736          end if;
737       end if;
738
739       --  Doing a rtsfind in system.ads is special, as we cannot do this
740       --  when compiling System itself. So if we are compiling system then
741       --  we should already have acquired and processed the declaration
742       --  of the entity. The test is to see if this compilation's main unit
743       --  is System. If so, return the value from the already compiled
744       --  declaration and otherwise do a regular find.
745
746       --  Not pleasant, but these kinds of annoying recursion when
747       --  writing an Ada compiler in Ada have to be broken somewhere!
748
749       if Present (Main_Unit_Entity)
750         and then Chars (Main_Unit_Entity) = Name_System
751         and then Analyzed (Main_Unit_Entity)
752         and then not Is_Child_Unit (Main_Unit_Entity)
753       then
754          return Find_Local_Entity (E);
755       end if;
756
757       Save_Front_End_Inlining := Front_End_Inlining;
758       Front_End_Inlining := No_Run_Time;
759
760       --  Load unit if unit not previously loaded
761
762       if No (RE_Table (E)) then
763          Load_RTU (U_Id);
764          Lib_Unit := Unit (Cunit (U.Unum));
765
766          --  In the subprogram case, we are all done, the entity we want
767          --  is the entity for the subprogram itself. Note that we do not
768          --  bother to check that it is the entity that was requested.
769          --  the only way that could fail to be the case is if runtime is
770          --  hopelessly misconfigured, and it isn't worth testing for this.
771
772          if Nkind (Lib_Unit) = N_Subprogram_Declaration then
773             RE_Table (E) := U.Entity;
774
775          --  Otherwise we must have the package case, and here we have to
776          --  search the package entity chain for the entity we want. The
777          --  entity we want must be present in this chain, or we have a
778          --  misconfigured runtime.
779
780          else
781             pragma Assert (Nkind (Lib_Unit) = N_Package_Declaration);
782             Ename := RE_Chars (E);
783
784             Pkg_Ent := First_Entity (U.Entity);
785
786             while Present (Pkg_Ent) loop
787                if Ename = Chars (Pkg_Ent) then
788                   RE_Table (E) := Pkg_Ent;
789                   Check_RPC;
790                   goto Found;
791                end if;
792
793                Next_Entity (Pkg_Ent);
794             end loop;
795
796             --  If we didn't find the unit we want, something is wrong
797             --  although in no run time mode, we already gave a suitable
798             --  message, and so we simply return Empty, and the caller must
799             --  be prepared to handle this if the RTE call is otherwise
800             --  possible in high integrity mode.
801
802             if No_Run_Time
803               and then not OK_To_Use_In_No_Run_Time_Mode (U_Id)
804             then
805                Front_End_Inlining := Save_Front_End_Inlining;
806                return Empty;
807
808             else
809                Load_Fail ("entity not in package", U_Id,  RE_Id'Image (E));
810                raise Program_Error;
811             end if;
812          end if;
813       end if;
814
815       --  See if we have to generate a with for this entity. We generate
816       --  a with if the current unit is part of the extended main code
817       --  unit, and if we have not already added the with. The with is
818       --  added to the appropriate unit (the current one).
819
820    <<Found>>
821       if (not U.Withed)
822         and then
823           In_Extended_Main_Code_Unit (Cunit_Entity (Current_Sem_Unit))
824       then
825          U.Withed := True;
826
827          declare
828             Withn    : Node_Id;
829             Lib_Unit : Node_Id;
830
831          begin
832             Lib_Unit := Unit (Cunit (U.Unum));
833             Withn :=
834               Make_With_Clause (Standard_Location,
835                 Name =>
836                   Make_Unit_Name
837                     (Defining_Unit_Name (Specification (Lib_Unit))));
838             Set_Library_Unit          (Withn, Cunit (U.Unum));
839             Set_Corresponding_Spec    (Withn, U.Entity);
840             Set_First_Name            (Withn, True);
841             Set_Implicit_With         (Withn, True);
842
843             Mark_Rewrite_Insertion (Withn);
844             Append (Withn, Context_Items (Cunit (Current_Sem_Unit)));
845          end;
846       end if;
847
848       Front_End_Inlining := Save_Front_End_Inlining;
849       return RE_Table (E);
850    end RTE;
851
852    --------------------
853    -- Text_IO_Kludge --
854    --------------------
855
856    procedure Text_IO_Kludge (Nam : Node_Id) is
857       Chrs : Name_Id;
858
859       type Name_Map_Type is array (Text_IO_Package_Name) of RTU_Id;
860
861       Name_Map : Name_Map_Type := Name_Map_Type'(
862         Name_Decimal_IO     => Ada_Text_IO_Decimal_IO,
863         Name_Enumeration_IO => Ada_Text_IO_Enumeration_IO,
864         Name_Fixed_IO       => Ada_Text_IO_Fixed_IO,
865         Name_Float_IO       => Ada_Text_IO_Float_IO,
866         Name_Integer_IO     => Ada_Text_IO_Integer_IO,
867         Name_Modular_IO     => Ada_Text_IO_Modular_IO);
868
869       Wide_Name_Map : Name_Map_Type := Name_Map_Type'(
870         Name_Decimal_IO     => Ada_Wide_Text_IO_Decimal_IO,
871         Name_Enumeration_IO => Ada_Wide_Text_IO_Enumeration_IO,
872         Name_Fixed_IO       => Ada_Wide_Text_IO_Fixed_IO,
873         Name_Float_IO       => Ada_Wide_Text_IO_Float_IO,
874         Name_Integer_IO     => Ada_Wide_Text_IO_Integer_IO,
875         Name_Modular_IO     => Ada_Wide_Text_IO_Modular_IO);
876
877    begin
878       --  Nothing to do if name is not identifier or a selected component
879       --  whose selector_name is not an identifier.
880
881       if Nkind (Nam) = N_Identifier then
882          Chrs := Chars (Nam);
883
884       elsif Nkind (Nam) = N_Selected_Component
885         and then Nkind (Selector_Name (Nam)) = N_Identifier
886       then
887          Chrs := Chars (Selector_Name (Nam));
888
889       else
890          return;
891       end if;
892
893       --  Nothing to do if name is not one of the Text_IO subpackages
894       --  Otherwise look through loaded units, and if we find Text_IO
895       --  or Wide_Text_IO already loaded, then load the proper child.
896
897       if Chrs in Text_IO_Package_Name then
898          for U in Main_Unit .. Last_Unit loop
899             Get_Name_String (Unit_File_Name (U));
900
901             if Name_Len = 12 then
902
903                --  Here is where we do the loads if we find one of the
904                --  units Ada.Text_IO or Ada.Wide_Text_IO. An interesting
905                --  detail is that these units may already be used (i.e.
906                --  their In_Use flags may be set). Normally when the In_Use
907                --  flag is set, the Is_Potentially_Use_Visible flag of all
908                --  entities in the package is set, but the new entity we
909                --  are mysteriously adding was not there to have its flag
910                --  set at the time. So that's why we pass the extra parameter
911                --  to RTU_Find, to make sure the flag does get set now.
912                --  Given that those generic packages are in fact child units,
913                --  we must indicate that they are visible.
914
915                if Name_Buffer (1 .. 12) = "a-textio.ads" then
916                   Load_RTU (Name_Map (Chrs), In_Use (Cunit_Entity (U)));
917                   Set_Is_Visible_Child_Unit
918                     (RT_Unit_Table (Name_Map (Chrs)).Entity);
919
920                elsif Name_Buffer (1 .. 12) = "a-witeio.ads" then
921                   Load_RTU (Wide_Name_Map (Chrs), In_Use (Cunit_Entity (U)));
922                   Set_Is_Visible_Child_Unit
923                     (RT_Unit_Table (Wide_Name_Map (Chrs)).Entity);
924                end if;
925             end if;
926          end loop;
927       end if;
928    end Text_IO_Kludge;
929
930 end Rtsfind;