OSDN Git Service

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