OSDN Git Service

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